Where should the XDG_CONFIG_HOME variable be defined?

44

19

The XDG specification talks about the XDG_CONFIG_HOME variable, but does not specify where it should be defined. Should we define it in /etc/X11/Xsession, or is it the window manager's config file that needs to define this?

I tried declaring it in /etc/environment as

XDG_CONFIG_HOME="$HOME/.config"

but that didn't work, as it seems that $HOME is not defined when /etc/environment is parsed.

The only documentation I could find online was for Gentoo, where it was declared in /etc/env.d/90xsession

I'm using Ubuntu. What would be the general solution for Debian based distros?

NOLFXceptMe

Posted 2011-12-08T05:26:13.987

Reputation: 682

Answers

25

In Arch Linux, this is defined by /etc/profile, using a /etc/profile.d script.

For Debian/Ubuntu, if there's a /etc/profile.d – create a similar script inside; if such a directory does not exist – edit /etc/profile itsef.

export XDG_CONFIG_HOME="$HOME/.config"

The /etc/environment file is parsed by pam_env, which treats it as simple name=value assignments. However, it also has /etc/security/pam_env.conf, which supports variable expansion and can be used for this purpose.

user1686

Posted 2011-12-08T05:26:13.987

Reputation: 283 655

3It's not in /etc/profile in Arch anymore, and is only once mentioned from /etc/profile.d/locale.sh. – phil pirozhkov – 2014-09-21T08:47:49.627

1Link is dead ... – ManuelSchneid3r – 2015-07-30T11:39:54.763

Link is dead, gist is unavailable, and Arch does not set it anymore: maybe because it makes no sense to set it to the default value. A cute kitten dies every time someone suggests so. – MestreLion – 2019-12-09T01:22:16.193

Gist is unavailable – hawk – 2013-11-22T12:32:35.123

57

You don't need to define it anywhere, unless you want to change the default.

XDG Base Directory Specification clearly says:

If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.

So it is redundant to define it to the default value. All compliant applications will already use $HOME/.config

But, if you do want to change the default in a Debian/Ubuntu system, the best place is:

  • For a system-wide change, affecting all users: /etc/profile
  • For your user only: ~/.profile

MestreLion

Posted 2011-12-08T05:26:13.987

Reputation: 1 489

1downvoter is welcome to explain what's wrong with the answer, and preferably help improve it – MestreLion – 2014-09-04T15:02:05.530

1This answer is perfectly fine, theoretically, but there is plenty software around, which is not XDG Base Directory Specification compliant and a quattuordecillion of bugs stemming from this ignorance. – ManuelSchneid3r – 2015-07-30T11:44:08.603

5@ManuelSchneid3r Very true, but for such software setting XDG_CONFIG_HOME will make little difference anyway, if any. – MestreLion – 2015-08-02T09:19:53.757

2There may be software that blindly uses XDG_CONFIG_HOME without following the spec, assuming that it's a standard Linux or even Unix feature rather than part of a third-party specification. – Eagle-Eye – 2016-12-03T21:54:57.783

@Eagle-Eye are you saying that we do have to define XDG_CONFIG_HOME etc therefore? – mcnesium – 2016-12-06T10:10:43.577

3@mcnesium I'm saying it probably can't hurt. Though if possible, it would be better to fix the application itself and send the fix upstream. – Eagle-Eye – 2016-12-06T19:03:02.840

5

I've found that it works best to set environment variables via PAM. For modern Linux distos, this means /etc/environment or $HOME/.pam_environment (see man pam_env). You can also set them in /etc/security/pam_env.conf using a special syntax. Here is how I set my XDG variables in /etc/security/pam_env.conf.

XDG_CACHE_HOME  DEFAULT=@{HOME}/.xdg/cache
XDG_CONFIG_HOME DEFAULT=@{HOME}/.xdg/config
XDG_DATA_HOME   DEFAULT=@{HOME}/.xdg/data
XDG_STATE_HOME  DEFAULT=@{HOME}/.xdg/state

Previously I would set these variables in /etc/profile.d/custom.sh. However, some applications start before that file is read. Switching to the PAM method solved the issue for multiple applications that behaved this way.

carlwgeorge

Posted 2011-12-08T05:26:13.987

Reputation: 289

5

Note: this does not follow the defaults in specified in the XDG Base Directory Specification

– Tom Hale – 2016-09-26T15:18:03.920

2@TomHale Of course not, the whole point of setting the variables is to override the defaults. If you are happy with the defaults, then you don't need to set these variables at all. – carlwgeorge – 2016-09-28T00:22:34.710

1Ubuntu 14.04 doesn't set the XDG directory variables by default - just because there is a specification doesn't mean that one's distro follows it. – Tom Hale – 2016-09-28T02:37:04.120

1@TomHale That doesn't matter because the XDG specification provides defaults for when the variables are not set, therefore it is not necessary to set the XDG variables. – Pauan – 2016-11-14T12:42:24.047

2It does matter if I want to export LESSHISTFILE="$XDG_CACHE_HOME"/less/history where less is not cognisant of the spec and the defaults are not yet set by my distro. – Tom Hale – 2016-11-14T12:52:42.530

2@TomHale the spec doesn't define what the operating system should set the variables to; it defines variables, that, if defined and non-empty, the application should use. And if the variable isn't defined or is empty, the application should use the default path referenced in the spec. So your short example isn't spec-compliant, since it doesn't first check that XDG_CACHE_HOME is defined and non-empty. It should instead be: export LESSHISTFILE="${XDG_CACHE_HOME:-$HOME/.cache}"/less/history – villapx – 2018-12-04T22:33:29.043

0

For Zsh users, define it in your .zshenv file

export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"

smac89

Posted 2011-12-08T05:26:13.987

Reputation: 121

1Downvoter care to explain yourself? – smac89 – 2019-05-02T19:40:58.623