created on | March 29, 2023 |
last modified on | December 24, 2023 |
XDG stands for X Desktop Group, a project to work on interoperability for free-software desktop environments for the X Window System (X11) and Wayland on Linux and other Unix-like operating systems. It eventually evolved into freedesktop.org. freedesktop.org produces specifications for interoperability, but it is not a formal standards body. Projects are not required to implement the specifications and there is no certification.
The XDG Base Directory Specification specifies a set of environment variables and configuration files that specify locations that user programs should use to write specific data types, such as configuration files, cache files, runtime files and user data like documents, music, images and so on. The specification also defines default values for unset environment variables and unset configuration values.
Linux distributions usually provide a system-wide XDG configuration in . The system-wide configuration can be overwritten on a per-user basis.
To me, the XGD Base Directory Specification is useful in two ways:
Those two points only apply as far as programs adhere to the XDG Base Directory Specification. I have compiled lists of programs that adhere and do not adhere to the XDG spec.
There are a bunch of programs to configure XDG and read its configuration (see below programs to manage XDG Base Directories). Personally, I think those programs are a bit of an overkill and I do well without them. The XDG Base Dirs can be configured by simply exporting the respecting environment vars in your . The image below illustrates the concept:
In the upper left hand in the image there is the home directory tree of the (fictional) user alice plus a directory within , which alice uses for her runtime data that is to be cleaned up periodically.
In the upper right hand is an excerpt of alice’s that contains some XDG Base Directories config vars.
On the lower right hand of the image is the system-wide XDG config file overwritten by the user specific config file in alice’s home dir.
The most straight-forward way to configure the XDG Base Directories:
That’s it.
Here is an example of a section in :
For the list of env vars for XDG Base Directories and env vars for user specific data dirs see the sections below.
All environment variables start with . For most environment variables the specification defines a default that should be used if the variable is not set or empty. The XDG Base Directory spec specifies the following directories:
config var | default | usage |
---|---|---|
XDG_CONFIG_HOME | $HOME/.config | user-specific configuration of application programs, i.e. text processors and spreadsheets like libre office, graphic programs like GIMP. |
XDG_DATA_HOME | $HOME/.local/share | user-specific data files. Unfortunately, the XDG Base Directories spec does not specify what kind of data. |
XDG_CACHE_HOME | $HOME/.cache | user-specific non-essential (cached) data. Data that speeds up operations but is not essential for functioning of the program, i.e. a web browser’s cache. |
XDG_STATE_HOME | $HOME/.local/state | user-specific state data; actions history (i.e. command history, recently used files) and application state (i.e. view layout, open files) |
XDG_RUNTIME_DIR | user-specific non-essential runtime files and other file objects such as sockets and named pipes. Data for program communication and synchronization purposes. It must be owned by the user and its Unix access mode MUST be 0700 | |
XDG_CONFIG_DIRS | /etc/xdg | preference-ordered set of base directories to search for configuration files in addition to the $XDG_CONFIG_HOME base directory, with the directories separated with a colon ‘:’. |
$HOME/.local/bin | user-specific executable files |
XDG Base Directory Specification states the following requirements for :
It is not clear to me what a full logout/login cycle is. I suspect the meaning full logout/login cycle here is a reboot. The spec states about this:
" The lifetime of the directory MUST be bound to the user being logged in. It MUST be created when the user first logs in and if the user fully logs out the directory MUST be removed. If the user logs in more than once he should get pointed to the same directory, and it is mandatory that the directory continues to exist from his first login to his last logout on the system, and not removed in between. Files in the directory MUST not survive reboot or a full logout/login cycle. "
XDG Base Directory Specification, Environment variables
Files in may be subject to periodic clean-up. Modification of file access time timestamp at least once every 6 hours of monotonic time or setting the ‘sticky’ bit on the file prevent the file from being subject to periodic clean-up.
The XDG Base Directory Specification does not specify a default directory for :
" If $XDG_RUNTIME_DIR is not set applications should fall back to a replacement directory with similar capabilities and print a warning message. "
XDG Base Directory Specification, Environment variables
The XDG Base Directory Specification expects user-specific executable files to be located in . There is no configuration value to specify the location for user-specific executables. All what the spec states about this is:
" There is a single base directory relative to which user-specific executable files may be written. [...] User-specific executable files may be stored in $HOME/.local/bin. Distributions should ensure this directory shows up in the UNIX $PATH environment variable, at an appropriate place. "
The env vars for data dirs in the user’s home dir are not specified in the XDG Base Directory spec. The system-wide config file lists the env vars, but without the suffix These env vars are, in alphabetical order:
config var |
---|
XDG_DESKTOP |
XDG_DOCUMENTS |
XDG_DOWNLOAD |
XDG_MUSIC |
XDG_PICTURES |
XDG_PUBLICSHARE |
XDG_TEMPLATES |
XDG_VIDEOS |
config file | usage |
---|---|
user-dirs.conf | Enables/disables update with . Specifies the file encoding for directory names. |
user-dirs.locale | Created on the first run of as a kind of marker that other programs can check to find out if the locale has changed. |
user-dirs.defaults | Default locations for specific data, i.e., template files, downloads, music, video. |
The system-wide config file can be overwritten with a user specific config file . This file has to be located in the dir configured with or .
config file | usage |
---|---|
user-dirs.dirs | contains the locations for specific data, i.e., template files, downloads, music, video. |
To get an overview of the files and programs involved, run which produces an output like shown below (programs for other topics like xdg-screensaver are omitted in the output):
Any configured directories that point to non-existing locations are reset by pointing them to the users home directory. It is assumed that this typically happens when the users removed the directory, because they don’t want to use it anymore.
Here is the XDG section in my :
The naming inconsistency between and (why not user-dirs.conf?), the default value for user-specific executables without a definition for the corresponding env var and the complete absence of all env vars for the user’s data file locations in the XDG Base Directories Specification are quite annoying. Nevertheless, the spec is quite useful and it’s application is straight forward once you have wrapped your head around the spec. For the latter, I hope this article is useful.
freedesktop.org, Specifications