Lee Byron

tilmac xdg

I like a tidy computer. I don’t have a desktop and I organize my config files within XDG base directories.

XDG base directories defines a standard way to layout a user directory that’s portable across environments and defines a set of environment variables that many programs will look for to determine where they read and write files. This is particularly nice for keeping your ~ home directory from becoming littered with various dot-file config scripts.

This is a growing standard on Linux but not the default on macOS. However it’s easy to set up!

Create (with sudo) the file /etc/zshenv1 (or ~/.zshenv for your user only) with the following contents:

# Define XDG Base directory environment variables
export XDG_BIN_HOME="$HOME/.local/bin"
export XDG_CACHE_HOME="$HOME/Library/Caches"
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_RUNTIME_DIR="$TMPDIR/runtime-$UID"
export XDG_STATE_HOME="$HOME/.local/state"
export PATH="$XDG_BIN_HOME:$PATH"

# Define paths for common programs not using XDG
export ZDOTDIR="$XDG_CONFIG_HOME/zsh"
export GNUPGHOME="$XDG_CONFIG_HOME/gnupg"
export NODE_REPL_HISTORY="$XDG_STATE_HOME/node/repl_history";
export NPM_CONFIG_CACHE="$XDG_CACHE_HOME/npm"
export NPM_CONFIG_TMP="$XDG_RUNTIME_DIR/npm"
export NPM_CONFIG_USERCONFIG="$XDG_CONFIG_HOME/npm/config"

Many programs will now automatically start using these directories, and existing config files can be moved into the ~/.cache/{program}/ folders.

If you’re writing distributing your own program which defines local configuration files, please do check if $XDG_CONFIG_HOME or $XDG_CONFIG_DIRS is set and if so prefer using XDG paths for your config files.

  1. Assuming you are using the default zsh as your shell. If using a different shell then sub in the equivalent environment config file.