3

I am writing my application foo, with frontend foo-gtk.

I want to install a default/systemwide config file, which I infer would go under /etc/foo.conf, and possible /etc/foo-gtk.conf for default gui settings.

I am far more confused about user specific config files.

Here are places I see config files for other applications:

  • ~/.foo.conf
  • ~/.foo/config
  • ~/.config/foo/config

The first has the advantage of being quick to get to if the user needs to edit it a lot, But the next two seem roughly equivalent.

I haven't found any formal spec on this so I'm curious, what is best practice for storing/locationof application Defualt, SystemWide, and Per-user config files?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
ThorSummoner
  • 4,312
  • 6
  • 30
  • 47
  • I don't think there is a best practice really. I think a ~/.foo directory is used when there are multiple config files. Otherwise ~/.foorc is probably reasonable. – Faheem Mitha May 21 '15 at 19:23
  • 1
    See also [Why do some applications use ~/.config/appname for their config data while others use ~/.appname?](http://unix.stackexchange.com/q/24347) – don_crissti Aug 01 '15 at 22:45

2 Answers2

2

The latest best practice is not to embed the user config file under a hidden user directory unless there will be more than one user specific file for the application. Thus, you should use only ~/.fooconfig (or ~/.foorc) for the user resource file for foo.

If foo has multiple user files, then it's ~/.foo/config for the configuration file along with any other files, all under ~/.foo/

Andrew
  • 1,175
  • 7
  • 8
  • 3
    Afaik, the latest best practice as per [FDO spec](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) is to use `$XDG_CONFIG_HOME` which if not set defaults to `$HOME/.config`. – don_crissti May 21 '15 at 16:02
  • 1
    XDG is creating a directory at `~/.config` - but this (appears) specific to XDG (desktop) applications - not "all" applications. `https://wiki.archlinux.org/index.php/Xdg_user_directories` IMHO XDG creating a generic `~/.config` directory (for instance, not `~/.xdg/` directory) is only best practice for XDG - but I would hazard to say it is not best practice for 'all' applications. – Andrew May 21 '15 at 16:09
  • 3
    Not sure what you mean by _"best practice for XDG"_... Do you mean GUI apps ? Not really... XDG specs were born in the X world, indeed, but that doesn't mean they only apply to GUI apps. "best practice" is subjective and OP said _"I haven't found any formal spec"_ hence my comment. Example of FDO/XDG compliant app (with daemon, cli and gui components) : [`transmission` config files](https://trac.transmissionbt.com/wiki/ConfigFiles). For the record, I have stuff under `.config` that belongs to pulse, imagemagick etc (none of these is a gui app). – don_crissti May 21 '15 at 16:56
  • 1
    So, to use ssh, I suppose `~/.config/.ssh/id_rsa` makes sense if you consider 'XDG' to be implementing 'best practices'. Regardless, I'm going with what ssh, git, kde, gnome and most applications do, use a .config file or a . directory under the user's home directory. This will preserve XDG's generic `~/.config` implementation for developers who trust their configs won't get trampled by files with the same name in a shared directory. – Andrew May 21 '15 at 18:01
2

The standard that has been defined some 10+ years ago, and recently adopted, is to use ~/.config/, ~/.cache/, ~/.local/share/ and a few other locations to store configuration, cache and data of the user in a standard way.

This is defined by environment variables such as $XDG_CONFIG_HOME, $XDG_CACHE_HOME, $XDG_DATA_HOME et al. They are still rarely pre-defined in most environments, but it might still be a good idea to test their existence, set the official defaults yourself if not already pre-set, and then use these specific variables within your own variables for configuration location; this way you can signal to any potential user, developer or source-code researcher that you're using these standard locations for configuration, cache and data (because otherwise, searching for ~/.config/ or $HOME/.config/ may or may not work depending on the programming language and the search mechanism).

if [ "x$XDG_CONFIG_HOME" = "x" ] ; then
    XDG_CONFIG_HOME="$HOME/.config"
fi
CONFIG_FILE="$XDG_CONFIG_HOME/myprog/myprog.conf"
if [ "x$XDG_CACHE_HOME" = "x" ] ; then
    XDG_CACHE_HOME="$HOME/.cache"
fi
CACHE_DIR="$XDG_CACHE_HOME/myprog/my-prog-cache-v1"

References:

cnst
  • 3,223
  • 2
  • 23
  • 44