85

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?

6 Answers 6

47

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.

1
  • 7
    It's not in /etc/profile in Arch anymore, and is only once mentioned from /etc/profile.d/locale.sh. Sep 21, 2014 at 8:47
108

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
12
  • 7
    This 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. Jul 30, 2015 at 11:44
  • 7
    @ManuelSchneid3r Very true, but for such software setting XDG_CONFIG_HOME will make little difference anyway, if any.
    – MestreLion
    Aug 2, 2015 at 9:19
  • 7
    There 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
    Dec 3, 2016 at 21:54
  • 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
    Dec 6, 2016 at 19:03
  • 3
    FWIW, byobu uses XDG_CONFIG_HOME but does not fallback to $HOME/.config. Think of programs that adopt XDG_CONFIG_HOME but want to stay backward-compatible. May 29, 2020 at 6:57
12

Elaborating on the points in the other answers regarding setting variables to the fallback values:

If one is fine with the fallback values stated in the specification, setting environment variables to duplicate the fallbacks (ie: setting $XDG_CONFIG_HOME=$HOME/.config) is not pointless or dumb. Not every program will follow the spec correctly, and having the variable set can make copying and pasting much easier.

For example, NVIDIA uses XDG_CACHE_HOME if set, otherwise improperly falls back to ~/.nv instead of ~/.cache. And you can easily wait years for something like that to be fixed.

Also, this page is a great spot to grab a bunch of environment variables to force certain programs to use your defined directories. Copying and pasting 20 of those and then setting tweaking the path is more work than having to set the XDG variables.

And to add something specific to the original question: this page has information about where to define variables.

1
  • 1
    seems like a fine partial answer to me. I usually lead with "partial:answer:to add to what has already been said in other answers" Jan 29, 2021 at 17:08
10

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.

8
  • 8
    Note: this does not follow the defaults in specified in the XDG Base Directory Specification
    – Tom Hale
    Sep 26, 2016 at 15:18
  • 6
    @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. Sep 28, 2016 at 0:22
  • 1
    Ubuntu 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
    Sep 28, 2016 at 2:37
  • 5
    It 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
    Nov 14, 2016 at 12:52
  • 7
    @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
    Dec 4, 2018 at 22:33
2

For Zsh users, define it in your .zshenv ~/.zprofile file

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

For Zsh, I prefer to use global zshenv directly. By specifying this default value to it - $HOME/.config, which it should know .... - using the bash substitution parameter ":="

cat >> /etc/zsh/zshenv <<- "gnark"
export ZDOTDIR="${XDG_CONFIG_HOME:=$HOME/.config}/zsh"
gnark

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .