about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLinus Heckemann <git@sphalerite.org>2018-10-25T11·00+0200
committerLinus Heckemann <git@sphalerite.org>2018-10-31T08·24+0100
commitf3b8173a93a3571046ef6765afe1a15efc1e0de6 (patch)
treeb2b5c865eae201be54e908b45ee4a5ed5d97394c
parent7a9ac91a43e1e05e9df9d1b9b4a2cf322d62bb1c (diff)
config: use all of XDG_CONFIG_DIRS
Previously, config would only be read from XDG_CONFIG_HOME. This change
allows reading config from additional directories, which enables e.g.
per-project binary caches or chroot stores with the help of direnv.
-rw-r--r--src/libstore/globals.cc6
-rw-r--r--src/libutil/util.cc9
-rw-r--r--src/libutil/util.hh3
3 files changed, 17 insertions, 1 deletions
diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc
index a9c07b23a6f3..1c2c08715a14 100644
--- a/src/libstore/globals.cc
+++ b/src/libstore/globals.cc
@@ -78,7 +78,11 @@ void loadConfFile()
        ~/.nix/nix.conf or the command line. */
     globalConfig.resetOverriden();
 
-    globalConfig.applyConfigFile(getConfigDir() + "/nix/nix.conf");
+    auto dirs = getConfigDirs();
+    // Iterate over them in reverse so that the ones appearing first in the path take priority
+    for (auto dir = dirs.rbegin(); dir != dirs.rend(); dir++) {
+        globalConfig.applyConfigFile(*dir + "/nix/nix.conf");
+    }
 }
 
 unsigned int Settings::getDefaultCores()
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 03f0be705c1d..259eaf0a0dd3 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -496,6 +496,15 @@ Path getConfigDir()
     return configDir;
 }
 
+std::vector<Path> getConfigDirs()
+{
+    Path configHome = getConfigDir();
+    string configDirs = getEnv("XDG_CONFIG_DIRS");
+    std::vector<Path> result = tokenizeString<std::vector<string>>(configDirs, ":");
+    result.insert(result.begin(), configHome);
+    return result;
+}
+
 
 Path getDataDir()
 {
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index fc25d27758c7..bda87bee433e 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -131,6 +131,9 @@ Path getCacheDir();
 /* Return $XDG_CONFIG_HOME or $HOME/.config. */
 Path getConfigDir();
 
+/* Return the directories to search for user configuration files */
+std::vector<Path> getConfigDirs();
+
 /* Return $XDG_DATA_HOME or $HOME/.local/share. */
 Path getDataDir();