mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 23:42:43 +01:00
The short answer for why we need to do this is so we can consistently do `#include "nix/..."`. Without this change, there are ways to still make that work, but they are hacky, and they have downsides such as making it harder to make sure headers from the wrong Nix library (e..g. `libnixexpr` headers in `libnixutil`) aren't being used. The C API alraedy used `nix_api_*`, so its headers are *not* put in subdirectories accordingly. Progress on #7876 We resisted doing this for a while because it would be annoying to not have the header source file pairs close by / easy to change file path/name from one to the other. But I am ameliorating that with symlinks in the next commit.
100 lines
2 KiB
C++
100 lines
2 KiB
C++
#include "nix/util.hh"
|
|
#include "nix/users.hh"
|
|
#include "nix/environment-variables.hh"
|
|
#include "nix/file-system.hh"
|
|
|
|
namespace nix {
|
|
|
|
Path getCacheDir()
|
|
{
|
|
auto dir = getEnv("NIX_CACHE_HOME");
|
|
if (dir) {
|
|
return *dir;
|
|
} else {
|
|
auto xdgDir = getEnv("XDG_CACHE_HOME");
|
|
if (xdgDir) {
|
|
return *xdgDir + "/nix";
|
|
} else {
|
|
return getHome() + "/.cache/nix";
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Path getConfigDir()
|
|
{
|
|
auto dir = getEnv("NIX_CONFIG_HOME");
|
|
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()
|
|
{
|
|
Path configHome = getConfigDir();
|
|
auto configDirs = getEnv("XDG_CONFIG_DIRS").value_or("/etc/xdg");
|
|
std::vector<Path> result = tokenizeString<std::vector<std::string>>(configDirs, ":");
|
|
for (auto& p : result) {
|
|
p += "/nix";
|
|
}
|
|
result.insert(result.begin(), configHome);
|
|
return result;
|
|
}
|
|
|
|
|
|
Path getDataDir()
|
|
{
|
|
auto dir = getEnv("NIX_DATA_HOME");
|
|
if (dir) {
|
|
return *dir;
|
|
} else {
|
|
auto xdgDir = getEnv("XDG_DATA_HOME");
|
|
if (xdgDir) {
|
|
return *xdgDir + "/nix";
|
|
} else {
|
|
return getHome() + "/.local/share/nix";
|
|
}
|
|
}
|
|
}
|
|
|
|
Path getStateDir()
|
|
{
|
|
auto dir = getEnv("NIX_STATE_HOME");
|
|
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 dir = getStateDir();
|
|
createDirs(dir);
|
|
return dir;
|
|
}
|
|
|
|
|
|
std::string expandTilde(std::string_view path)
|
|
{
|
|
// TODO: expand ~user ?
|
|
auto tilde = path.substr(0, 2);
|
|
if (tilde == "~/" || tilde == "~")
|
|
return getHome() + std::string(path.substr(1));
|
|
else
|
|
return std::string(path);
|
|
}
|
|
|
|
}
|