aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorAlexander Bantyev <balsoft@balsoft.ru>2021-11-17 23:35:21 +0300
committerAlexander Bantyev <balsoft@balsoft.ru>2023-02-10 20:14:06 +0400
commit2384d360839e27edb3c928da858ec911415c8b4d (patch)
tree95e5360fc7016b9d171f4bad320486eda168bbb1 /src/libutil
parenta31d7d4e5e5eeeb7ca12ca798dc383045e5be1a1 (diff)
A setting to follow XDG Base Directory standard
XDG Base Directory is a standard for locations for storing various files. Nix has a few files which seem to fit in the standard, but currently use a custom location directly in the user's ~, polluting it: - ~/.nix-profile - ~/.nix-defexpr - ~/.nix-channels This commit adds a config option (use-xdg-base-directories) to follow the XDG spec and instead use the following locations: - $XDG_STATE_HOME/nix/profile - $XDG_STATE_HOME/nix/defexpr - $XDG_STATE_HOME/nix/channels If $XDG_STATE_HOME is not set, it is assumed to be ~/.local/state. Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com> Co-authored-by: Tim Fenney <kodekata@gmail.com> Co-authored-by: pasqui23 <pasqui23@users.noreply.github.com> Co-authored-by: Artturin <Artturin@artturin.com> Co-authored-by: John Ericson <Ericson2314@Yahoo.com>
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc13
-rw-r--r--src/libutil/util.hh6
2 files changed, 19 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 40faa4bf2..40a54b010 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -608,6 +608,19 @@ Path getDataDir()
return dataDir ? *dataDir : getHome() + "/.local/share";
}
+Path getStateDir()
+{
+ auto stateDir = getEnv("XDG_STATE_HOME");
+ return stateDir ? *stateDir : getHome() + "/.local/state";
+}
+
+Path createNixStateDir()
+{
+ Path dir = getStateDir() + "/nix";
+ createDirs(dir);
+ return dir;
+}
+
std::optional<Path> getSelfExe()
{
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 266da0ae3..4fadafaf2 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -158,6 +158,12 @@ Path getDataDir();
/* Return the path of the current executable. */
std::optional<Path> getSelfExe();
+/* Return $XDG_STATE_HOME or $HOME/.local/state. */
+Path getStateDir();
+
+/* Create the Nix state directory and return the path to it. */
+Path createNixStateDir();
+
/* Create a directory and all its parents, if necessary. Returns the
list of created directories, in order of creation. */
Paths createDirs(const Path & path);