Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libexpr/eval-settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ const std::string & EvalSettings::getCurrentSystem() const
return evalSystem != "" ? evalSystem : settings.thisSystem.get();
}

Path getNixDefExpr()
std::filesystem::path getNixDefExpr()
{
return settings.useXDGBaseDirectories ? getStateDir() + "/defexpr" : getHome() + "/.nix-defexpr";
return settings.useXDGBaseDirectories ? getStateDir() / "defexpr" : getHome() / ".nix-defexpr";
}

} // namespace nix
2 changes: 1 addition & 1 deletion src/libexpr/include/nix/expr/eval-settings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,6 @@ struct EvalSettings : Config
/**
* Conventionally part of the default nix path in impure mode.
*/
Path getNixDefExpr();
std::filesystem::path getNixDefExpr();

} // namespace nix
2 changes: 1 addition & 1 deletion src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ path_start
std::string_view($1.p, $1.l)
);
}
Path path(getHome() + std::string($1.p + 1, $1.l - 1));
Path path(getHome().string() + std::string($1.p + 1, $1.l - 1));
$$ = state->exprs.add<ExprPath>(state->exprs.alloc, ref<SourceAccessor>(state->rootFS), path);
}
;
Expand Down
18 changes: 10 additions & 8 deletions src/libutil/include/nix/util/users.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
///@file

#include <filesystem>

#include "nix/util/types.hh"

#ifndef _WIN32
Expand All @@ -15,43 +17,43 @@ std::string getUserName();
/**
* @return the given user's home directory from /etc/passwd.
*/
Path getHomeOf(uid_t userId);
std::filesystem::path getHomeOf(uid_t userId);
#endif

/**
* @return $HOME or the user's home directory from /etc/passwd.
*/
Path getHome();
std::filesystem::path getHome();

/**
* @return $NIX_CACHE_HOME or $XDG_CACHE_HOME/nix or $HOME/.cache/nix.
*/
Path getCacheDir();
std::filesystem::path getCacheDir();

/**
* @return $NIX_CONFIG_HOME or $XDG_CONFIG_HOME/nix or $HOME/.config/nix.
*/
Path getConfigDir();
std::filesystem::path getConfigDir();

/**
* @return the directories to search for user configuration files
*/
std::vector<Path> getConfigDirs();
std::vector<std::filesystem::path> getConfigDirs();

/**
* @return $NIX_DATA_HOME or $XDG_DATA_HOME/nix or $HOME/.local/share/nix.
*/
Path getDataDir();
std::filesystem::path getDataDir();

/**
* @return $NIX_STATE_HOME or $XDG_STATE_HOME/nix or $HOME/.local/state/nix.
*/
Path getStateDir();
std::filesystem::path getStateDir();

/**
* Create the Nix state directory and return the path to it.
*/
Path createNixStateDir();
std::filesystem::path createNixStateDir();

/**
* Perform tilde expansion on a path, replacing tilde with the user's
Expand Down
1 change: 1 addition & 0 deletions src/libutil/include/nix/util/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "nix/util/logging.hh"
#include "nix/util/strings.hh"

#include <filesystem>
#include <functional>
#include <map>
#include <sstream>
Expand Down
6 changes: 3 additions & 3 deletions src/libutil/unix/users.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::string getUserName()
return name;
}

Path getHomeOf(uid_t userId)
std::filesystem::path getHomeOf(uid_t userId)
{
std::vector<char> buf(16384);
struct passwd pwbuf;
Expand All @@ -28,9 +28,9 @@ Path getHomeOf(uid_t userId)
return pw->pw_dir;
}

Path getHome()
std::filesystem::path getHome()
{
static Path homeDir = []() {
static std::filesystem::path homeDir = []() {
std::optional<std::string> unownedUserHomeDir = {};
auto homeDir = getEnv("HOME");
if (homeDir) {
Expand Down
41 changes: 21 additions & 20 deletions src/libutil/users.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,82 @@

namespace nix {

Path getCacheDir()
std::filesystem::path getCacheDir()
{
auto dir = getEnv("NIX_CACHE_HOME");
if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_CACHE_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
return std::filesystem::path{*xdgDir} / "nix";
} else {
return getHome() + "/.cache/nix";
return getHome() / ".cache" / "nix";
}
}
}

Path getConfigDir()
std::filesystem::path getConfigDir()
{
auto dir = getEnv("NIX_CONFIG_HOME");
if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_CONFIG_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
return std::filesystem::path{*xdgDir} / "nix";
} else {
return getHome() + "/.config/nix";
return getHome() / ".config" / "nix";
}
}
}

std::vector<Path> getConfigDirs()
std::vector<std::filesystem::path> getConfigDirs()
{
Path configHome = getConfigDir();
std::filesystem::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";
auto tokens = tokenizeString<std::vector<std::string>>(configDirs, ":");
std::vector<std::filesystem::path> result;
result.push_back(configHome);
for (auto & token : tokens) {
result.push_back(std::filesystem::path{token} / "nix");
}
result.insert(result.begin(), configHome);
return result;
}

Path getDataDir()
std::filesystem::path getDataDir()
{
auto dir = getEnv("NIX_DATA_HOME");
if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_DATA_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
return std::filesystem::path{*xdgDir} / "nix";
} else {
return getHome() + "/.local/share/nix";
return getHome() / ".local" / "share" / "nix";
}
}
}

Path getStateDir()
std::filesystem::path getStateDir()
{
auto dir = getEnv("NIX_STATE_HOME");
if (dir) {
return *dir;
} else {
auto xdgDir = getEnv("XDG_STATE_HOME");
if (xdgDir) {
return *xdgDir + "/nix";
return std::filesystem::path{*xdgDir} / "nix";
} else {
return getHome() + "/.local/state/nix";
return getHome() / ".local" / "state" / "nix";
}
}
}

Path createNixStateDir()
std::filesystem::path createNixStateDir()
{
Path dir = getStateDir();
std::filesystem::path dir = getStateDir();
createDirs(dir);
return dir;
}
Expand Down
6 changes: 3 additions & 3 deletions src/libutil/windows/users.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ std::string getUserName()
return name;
}

Path getHome()
std::filesystem::path getHome()
{
static Path homeDir = []() {
Path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default");
static std::filesystem::path homeDir = []() {
std::filesystem::path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default");
assert(!homeDir.empty());
return canonPath(homeDir);
}();
Expand Down