4

I'm talking about files like ~/.foo found on a user's home dir. I'm working on a program that reads from such a file, and I'd also like to clean up my user root directory if I can.

Is there a POSIX-specified variable, such as ~/$(conf) where config like .emacs can be found?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
rath
  • 209
  • 1
  • 7
  • possible duplicate of [Why do some applications use ~/.config/appname for their config data while others use ~/.appname?](http://unix.stackexchange.com/questions/24347/why-do-some-applications-use-config-appname-for-their-config-data-while-other) – rath Apr 26 '14 at 08:57
  • Re duplicate: I used the word POSIX in the original title to mean any standard. – rath Apr 26 '14 at 08:58

2 Answers2

5

POSIX

Searching through the specification for the strings "user config" or "configuration files" turned up zero hits, so I would say no it doesn't specify this in any way.

FHS

Looking at the FHS - Filesystem Hierarchy Standard it had this bit:

User specific configuration files for applications are stored in the user's home directory in a file that starts with the '.' character (a "dot file"). If an application needs to create more than one dot file then they should be placed in a subdirectory with a name starting with a '.' character, (a "dot directory"). In this case the configuration files should not start with the '.' character. 11.

sysconf/getconf

Looking through the list of POSIX configuration constants present in <limits.h> is the only other place I can think of where something like this would be configured. Running the command getconf <var> will return these types of results.

For example:

$ getconf _POSIX_CHILD_MAX
1024

But looking through the list of definitions I don't see any pertaining to a user's home directory.

slm
  • 363,520
  • 117
  • 767
  • 871
3

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"

For example, maybe you use Golang in your build system, and want to cache the downloaded Go packages unique to your build system in a build-specific directory, then the following shell script could be used to set it all up:

if [ "x$XDG_CACHE_HOME" = "x" ] ; then
    XDG_CACHE_HOME="$HOME/.cache"
fi
GO_MOD_CACHE_DIR="$XDG_CACHE_HOME/myprog/go-mod-cache"
mkdir -p "$GO_MOD_CACHE_DIR"
if [ -w "$GO_MOD_CACHE_DIR/" ] ; then
    export GOMODCACHE="$GO_MOD_CACHE_DIR"
else
    printf "WARN: GOMODCACHE directory not writeable: %s\n" "$GO_MOD_CACHE_DIR"
fi

References:

cnst
  • 3,223
  • 2
  • 23
  • 44