Use envvars NIX_CACHE_HOME, NIX_CONFIG_HOME, NIX_DATA_HOME, NIX_STATE_HOME if defined (#11351)

This commit is contained in:
Noam Yorav-Raphael 2024-09-11 13:36:46 +03:00 committed by GitHub
parent c60e1be62c
commit 38bfbb297c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 118 additions and 48 deletions

View file

@ -0,0 +1,14 @@
---
synopsis: Use envvars NIX_CACHE_HOME, NIX_CONFIG_HOME, NIX_DATA_HOME, NIX_STATE_HOME if defined
prs: [11351]
---
Added new environment variables:
- `NIX_CACHE_HOME`
- `NIX_CONFIG_HOME`
- `NIX_DATA_HOME`
- `NIX_STATE_HOME`
Each, if defined, takes precedence over the corresponding [XDG environment variable](@docroot@/command-ref/env-common.md#xdg-base-directories).
This provides more fine-grained control over where Nix looks for files, and allows to have a stand-alone Nix environment, which only uses files in a specific directory, and doesn't interfere with the user environment.

View file

@ -138,6 +138,19 @@ The following environment variables are used to determine locations of various s
- [`XDG_STATE_HOME`]{#env-XDG_STATE_HOME} (default `~/.local/state`) - [`XDG_STATE_HOME`]{#env-XDG_STATE_HOME} (default `~/.local/state`)
- [`XDG_CACHE_HOME`]{#env-XDG_CACHE_HOME} (default `~/.cache`) - [`XDG_CACHE_HOME`]{#env-XDG_CACHE_HOME} (default `~/.cache`)
[XDG Base Directory Specification]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html [XDG Base Directory Specification]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
[`use-xdg-base-directories`]: @docroot@/command-ref/conf-file.md#conf-use-xdg-base-directories [`use-xdg-base-directories`]: @docroot@/command-ref/conf-file.md#conf-use-xdg-base-directories
In addition, setting the following environment variables overrides the XDG base directories:
- [`NIX_CONFIG_HOME`]{#env-NIX_CONFIG_HOME} (default `$XDG_CONFIG_HOME/nix`)
- [`NIX_STATE_HOME`]{#env-NIX_STATE_HOME} (default `$XDG_STATE_HOME/nix`)
- [`NIX_CACHE_HOME`]{#env-NIX_CACHE_HOME} (default `$XDG_CACHE_HOME/nix`)
When [`use-xdg-base-directories`] is enabled, the configuration directory is:
1. `$NIX_CONFIG_HOME`, if it is defined
2. Otherwise, `$XDG_CONFIG_HOME/nix`, if `XDG_CONFIG_HOME` is defined
3. Otherwise, `~/.config/nix`.
Likewise for the state and cache directories.

View file

@ -3,6 +3,9 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
# Set up the per-user profile. # Set up the per-user profile.
if [ -n "$NIX_STATE_HOME" ]; then
NIX_LINK="$NIX_STATE_HOME/profile"
else
NIX_LINK="$HOME/.nix-profile" NIX_LINK="$HOME/.nix-profile"
if [ -n "${XDG_STATE_HOME-}" ]; then if [ -n "${XDG_STATE_HOME-}" ]; then
NIX_LINK_NEW="$XDG_STATE_HOME/nix/profile" NIX_LINK_NEW="$XDG_STATE_HOME/nix/profile"
@ -27,6 +30,7 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
fi fi
NIX_LINK="$NIX_LINK_NEW" NIX_LINK="$NIX_LINK_NEW"
fi fi
fi
# Set up environment. # Set up environment.
# This part should be kept in sync with nixpkgs:nixos/modules/programs/environment.nix # This part should be kept in sync with nixpkgs:nixos/modules/programs/environment.nix

View file

@ -134,7 +134,7 @@ NixRepl::NixRepl(const LookupPath & lookupPath, nix::ref<Store> store, ref<EvalS
, getValues(getValues) , getValues(getValues)
, staticEnv(new StaticEnv(nullptr, state->staticBaseEnv.get())) , staticEnv(new StaticEnv(nullptr, state->staticBaseEnv.get()))
, runNixPtr{runNix} , runNixPtr{runNix}
, interacter(make_unique<ReadlineLikeInteracter>(getDataDir() + "/nix/repl-history")) , interacter(make_unique<ReadlineLikeInteracter>(getDataDir() + "/repl-history"))
{ {
} }

View file

@ -69,7 +69,7 @@ struct AttrDb
{ {
auto state(_state->lock()); auto state(_state->lock());
Path cacheDir = getCacheDir() + "/nix/eval-cache-v5"; Path cacheDir = getCacheDir() + "/eval-cache-v5";
createDirs(cacheDir); createDirs(cacheDir);
Path dbPath = cacheDir + "/" + fingerprint.to_string(HashFormat::Base16, false) + ".sqlite"; Path dbPath = cacheDir + "/" + fingerprint.to_string(HashFormat::Base16, false) + ".sqlite";

View file

@ -99,7 +99,7 @@ const std::string & EvalSettings::getCurrentSystem() const
Path getNixDefExpr() Path getNixDefExpr()
{ {
return settings.useXDGBaseDirectories return settings.useXDGBaseDirectories
? getStateDir() + "/nix/defexpr" ? getStateDir() + "/defexpr"
: getHome() + "/.nix-defexpr"; : getHome() + "/.nix-defexpr";
} }

View file

@ -36,7 +36,7 @@ struct CacheImpl : Cache
{ {
auto state(_state.lock()); auto state(_state.lock());
auto dbPath = getCacheDir() + "/nix/fetcher-cache-v2.sqlite"; auto dbPath = getCacheDir() + "/fetcher-cache-v2.sqlite";
createDirs(dirOf(dbPath)); createDirs(dirOf(dbPath));
state->db = SQLite(dbPath); state->db = SQLite(dbPath);

View file

@ -1083,7 +1083,7 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
ref<GitRepo> getTarballCache() ref<GitRepo> getTarballCache()
{ {
static auto repoDir = std::filesystem::path(getCacheDir()) / "nix" / "tarball-cache"; static auto repoDir = std::filesystem::path(getCacheDir()) / "tarball-cache";
return GitRepo::openRepo(repoDir, true, true); return GitRepo::openRepo(repoDir, true, true);
} }

View file

@ -44,7 +44,7 @@ bool isCacheFileWithinTtl(time_t now, const struct stat & st)
Path getCachePath(std::string_view key, bool shallow) Path getCachePath(std::string_view key, bool shallow)
{ {
return getCacheDir() return getCacheDir()
+ "/nix/gitv3/" + "/gitv3/"
+ hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) + hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false)
+ (shallow ? "-shallow" : ""); + (shallow ? "-shallow" : "");
} }

View file

@ -263,7 +263,7 @@ struct MercurialInputScheme : InputScheme
return makeResult(res->value, res->storePath); return makeResult(res->value, res->storePath);
} }
Path cacheDir = fmt("%s/nix/hg/%s", getCacheDir(), hashString(HashAlgorithm::SHA256, actualUrl).to_string(HashFormat::Nix32, false)); Path cacheDir = fmt("%s/hg/%s", getCacheDir(), hashString(HashAlgorithm::SHA256, actualUrl).to_string(HashFormat::Nix32, false));
/* If this is a commit hash that we already have, we don't /* If this is a commit hash that we already have, we don't
have to pull again. */ have to pull again. */

View file

@ -116,7 +116,7 @@ static std::shared_ptr<Registry> getSystemRegistry(const Settings & settings)
Path getUserRegistryPath() Path getUserRegistryPath()
{ {
return getConfigDir() + "/nix/registry.json"; return getConfigDir() + "/registry.json";
} }
std::shared_ptr<Registry> getUserRegistry(const Settings & settings) std::shared_ptr<Registry> getUserRegistry(const Settings & settings)
@ -159,7 +159,7 @@ static std::shared_ptr<Registry> getGlobalRegistry(const Settings & settings, re
if (!hasPrefix(path, "/")) { if (!hasPrefix(path, "/")) {
auto storePath = downloadFile(store, path, "flake-registry.json").storePath; auto storePath = downloadFile(store, path, "flake-registry.json").storePath;
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>()) if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
store2->addPermRoot(storePath, getCacheDir() + "/nix/flake-registry.json"); store2->addPermRoot(storePath, getCacheDir() + "/flake-registry.json");
path = store->toRealPath(storePath); path = store->toRealPath(storePath);
} }

View file

@ -12,7 +12,7 @@ typedef std::map<std::string, std::map<std::string, bool>> TrustedList;
Path trustedListPath() Path trustedListPath()
{ {
return getDataDir() + "/nix/trusted-settings.json"; return getDataDir() + "/trusted-settings.json";
} }
static TrustedList readTrustedList() static TrustedList readTrustedList()

View file

@ -135,7 +135,7 @@ std::vector<Path> getUserConfigFiles()
std::vector<Path> files; std::vector<Path> files;
auto dirs = getConfigDirs(); auto dirs = getConfigDirs();
for (auto & dir : dirs) { for (auto & dir : dirs) {
files.insert(files.end(), dir + "/nix/nix.conf"); files.insert(files.end(), dir + "/nix.conf");
} }
return files; return files;
} }

View file

@ -87,7 +87,7 @@ public:
Sync<State> _state; Sync<State> _state;
NarInfoDiskCacheImpl(Path dbPath = getCacheDir() + "/nix/binary-cache-v6.sqlite") NarInfoDiskCacheImpl(Path dbPath = getCacheDir() + "/binary-cache-v6.sqlite")
{ {
auto state(_state.lock()); auto state(_state.lock());

View file

@ -1315,7 +1315,7 @@ ref<Store> openStore(StoreReference && storeURI)
/* If /nix doesn't exist, there is no daemon socket, and /* If /nix doesn't exist, there is no daemon socket, and
we're not root, then automatically set up a chroot we're not root, then automatically set up a chroot
store in ~/.local/share/nix/root. */ store in ~/.local/share/nix/root. */
auto chrootStore = getDataDir() + "/nix/root"; auto chrootStore = getDataDir() + "/root";
if (!pathExists(chrootStore)) { if (!pathExists(chrootStore)) {
try { try {
createDirs(chrootStore); createDirs(chrootStore);

View file

@ -7,15 +7,33 @@ namespace nix {
Path getCacheDir() Path getCacheDir()
{ {
auto cacheDir = getEnv("XDG_CACHE_HOME"); auto dir = getEnv("NIX_CACHE_HOME");
return cacheDir ? *cacheDir : getHome() + "/.cache"; if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_CACHE_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
} else {
return getHome() + "/.cache/nix";
}
}
} }
Path getConfigDir() Path getConfigDir()
{ {
auto configDir = getEnv("XDG_CONFIG_HOME"); auto dir = getEnv("NIX_CONFIG_HOME");
return configDir ? *configDir : getHome() + "/.config"; if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_CONFIG_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
} else {
return getHome() + "/.config/nix";
}
}
} }
std::vector<Path> getConfigDirs() std::vector<Path> getConfigDirs()
@ -23,6 +41,9 @@ std::vector<Path> getConfigDirs()
Path configHome = getConfigDir(); Path configHome = getConfigDir();
auto configDirs = getEnv("XDG_CONFIG_DIRS").value_or("/etc/xdg"); auto configDirs = getEnv("XDG_CONFIG_DIRS").value_or("/etc/xdg");
std::vector<Path> result = tokenizeString<std::vector<std::string>>(configDirs, ":"); std::vector<Path> result = tokenizeString<std::vector<std::string>>(configDirs, ":");
for (auto& p : result) {
p += "/nix";
}
result.insert(result.begin(), configHome); result.insert(result.begin(), configHome);
return result; return result;
} }
@ -30,19 +51,37 @@ std::vector<Path> getConfigDirs()
Path getDataDir() Path getDataDir()
{ {
auto dataDir = getEnv("XDG_DATA_HOME"); auto dir = getEnv("NIX_DATA_HOME");
return dataDir ? *dataDir : getHome() + "/.local/share"; if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_DATA_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
} else {
return getHome() + "/.local/share/nix";
}
}
} }
Path getStateDir() Path getStateDir()
{ {
auto stateDir = getEnv("XDG_STATE_HOME"); auto dir = getEnv("NIX_STATE_HOME");
return stateDir ? *stateDir : getHome() + "/.local/state"; if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_STATE_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
} else {
return getHome() + "/.local/state/nix";
}
}
} }
Path createNixStateDir() Path createNixStateDir()
{ {
Path dir = getStateDir() + "/nix"; Path dir = getStateDir();
createDirs(dir); createDirs(dir);
return dir; return dir;
} }

View file

@ -24,12 +24,12 @@ Path getHomeOf(uid_t userId);
Path getHome(); Path getHome();
/** /**
* @return $XDG_CACHE_HOME or $HOME/.cache. * @return $NIX_CACHE_HOME or $XDG_CACHE_HOME/nix or $HOME/.cache/nix.
*/ */
Path getCacheDir(); Path getCacheDir();
/** /**
* @return $XDG_CONFIG_HOME or $HOME/.config. * @return $NIX_CONFIG_HOME or $XDG_CONFIG_HOME/nix or $HOME/.config/nix.
*/ */
Path getConfigDir(); Path getConfigDir();
@ -39,12 +39,12 @@ Path getConfigDir();
std::vector<Path> getConfigDirs(); std::vector<Path> getConfigDirs();
/** /**
* @return $XDG_DATA_HOME or $HOME/.local/share. * @return $NIX_DATA_HOME or $XDG_DATA_HOME/nix or $HOME/.local/share/nix.
*/ */
Path getDataDir(); Path getDataDir();
/** /**
* @return $XDG_STATE_HOME or $HOME/.local/state. * @return $NIX_STATE_HOME or $XDG_STATE_HOME/nix or $HOME/.local/state/nix.
*/ */
Path getStateDir(); Path getStateDir();