iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪟

Windows: Implementing XDG Base Directory on Windows

に公開

Introduction

When setting up a development environment on Windows, Linux-based tools like Git are often used.
These tools usually save configuration files under /home/<username>.
The /home/<username> directory is a bit inconvenient. Since it contains folders used by Windows, such as Documents, you cannot perform version control using Git as it is.

In Linux, there is a growing movement to move configuration files out of $HOME according to a specification called the XDG Base Directory. Therefore, by placing configuration files on Windows in compliance with this specification, we can enable version control with Git.

More Details

About XDG Base Directory

The XDG Base Directory defines the directories for saving configuration files and other data for various tools. These directories are stored as absolute paths in environment variables starting with XDG_.
For more details on the XDG Base Directory, please see the XDG Base Directory Specification.
A summary in Japanese is also available on the XDG Base Directory - ArchLinux wiki.

XDG Environment Variables

On Windows, we use the following environment variables.

Environment Variable Description Default Setting[1] Example Config File Remarks
XDG_CONFIG_HOME Saves app-specific configuration files ~/.config ${XDG_CONFIG_HOME}/git/ignore
XDG_DATA_HOME Saves various app data files ~/.local/share ${XDG_DATA_HOME}/tig/history
XDG_CACHE_HOME Saves temporary data files (cache) created by apps ~/.cache ${XDG_CACHE_HOME}/vim/_viminfo
XDG_STATE_HOME Saves app state files ~/.local/state ${XDG_STATE_HOME}/lesshst

Config files for various tools are basically saved in the format of "${<XDG environment variable>}/<app name>/<config file>".

Configuring XDG

Setting up XDG Base Directory

Configure the four environment variables mentioned in XDG Environment Variables. Additionally, set the HOME environment variable.
On Windows, the directory indicated by the USERPROFILE environment variable is your home directory. Since Linux/Unix-based tools use HOME, this can sometimes accommodate tools that do not natively support "XDG Base Directory".
Of course, since some Windows-compatible tools look at USERPROFILE, this will not cover every single case.

Set the environment variables as follows:

Environment Variable Value Remarks
XDG_CONFIG_HOME C:\Users<username>.config
XDG_DATA_HOME C:\Users<username>.local\share
XDG_CACHE_HOME C:\Users<username>.local\cache
XDG_STATE_HOME C:\Users<username>.local\state
HOME C:\Users<username>.config (Match with XDG_CONFIG_HOME)

Henceforth, set the environment variables for various tools according to the XDG_... environment variables mentioned above.

Configuring Tools (XDG Supported)

For tools that already support the XDG Base Directory, you don't need to configure anything special.
Instead, you will need to move your configuration files to the directory specified by the XDG Base Directory.

The Windows package manager Scoop supports the XDG Base Directory.
If you set the XDG_... environment variables as described above, Scoop will save its settings in ${XDG_CONFIG_HOME}/scoop/config.json.

Configuring Tools (Via HOME)

In this article, the HOME environment variable is overwritten to point to the same directory as XDG_CONFIG_HOME.
Therefore, tools that place their configuration files under $HOME will automatically comply with the XDG Base Directory.

For example, when using Vim on Windows, configuration files are placed under $HOME/vimfiles/.
By setting HOME as described in this article, configuration files will be placed under $XDG_CONFIG_HOME/vimfiles/.

Configuring Tools (Via Environment Variables)

For other tools, you can specify the location of the configuration files by setting environment variables.
The coding activity tracking tool wakatime saves various configuration files directly under $USERPROFILE by default. This remains unchanged even if the XDG_... environment variables are set.
Set the WAKATIME_HOME environment variable to $XDG_DATA_HOME/wakatime and create the corresponding directory.
Thereafter, various configuration files and log files will be saved under $WAKATIME_HOME.

Conclusion

Saving configuration files for each tool under $XDG_CONFIG_HOME in this way makes revision management of configuration files much easier.
In fact, I manage my own configuration files on GitHub.

The support status and configuration methods for various tools are listed on the "XDG Base Directory - ArchLinux wiki", so please refer to it.

Happy Hacking!

References

脚注
  1. Limited to Linux. On Windows, configuration is required for each tool. ↩︎

GitHubで編集を提案

Discussion