1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-28 13:11:00 +01:00

Merge pull request #14649 from vinayakankugoyal/path

Use std::filesystem::path instead of Path in libexpr.
This commit is contained in:
John Ericson 2025-11-26 01:23:16 +00:00 committed by GitHub
commit 31ce0c8169
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 41 additions and 37 deletions

View file

@ -103,9 +103,9 @@ const std::string & EvalSettings::getCurrentSystem() const
return evalSystem != "" ? evalSystem : settings.thisSystem.get(); 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 } // namespace nix

View file

@ -366,6 +366,6 @@ struct EvalSettings : Config
/** /**
* Conventionally part of the default nix path in impure mode. * Conventionally part of the default nix path in impure mode.
*/ */
Path getNixDefExpr(); std::filesystem::path getNixDefExpr();
} // namespace nix } // namespace nix

View file

@ -1,6 +1,8 @@
#pragma once #pragma once
///@file ///@file
#include <filesystem>
#include "nix/util/types.hh" #include "nix/util/types.hh"
#ifndef _WIN32 #ifndef _WIN32
@ -15,43 +17,43 @@ std::string getUserName();
/** /**
* @return the given user's home directory from /etc/passwd. * @return the given user's home directory from /etc/passwd.
*/ */
Path getHomeOf(uid_t userId); std::filesystem::path getHomeOf(uid_t userId);
#endif #endif
/** /**
* @return $HOME or the user's home directory from /etc/passwd. * @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. * @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. * @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 * @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. * @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. * @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. * 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 * Perform tilde expansion on a path, replacing tilde with the user's

View file

@ -6,6 +6,7 @@
#include "nix/util/logging.hh" #include "nix/util/logging.hh"
#include "nix/util/strings.hh" #include "nix/util/strings.hh"
#include <filesystem>
#include <functional> #include <functional>
#include <map> #include <map>
#include <sstream> #include <sstream>

View file

@ -18,7 +18,7 @@ std::string getUserName()
return name; return name;
} }
Path getHomeOf(uid_t userId) std::filesystem::path getHomeOf(uid_t userId)
{ {
std::vector<char> buf(16384); std::vector<char> buf(16384);
struct passwd pwbuf; struct passwd pwbuf;
@ -28,9 +28,9 @@ Path getHomeOf(uid_t userId)
return pw->pw_dir; return pw->pw_dir;
} }
Path getHome() std::filesystem::path getHome()
{ {
static Path homeDir = []() { static std::filesystem::path homeDir = []() {
std::optional<std::string> unownedUserHomeDir = {}; std::optional<std::string> unownedUserHomeDir = {};
auto homeDir = getEnv("HOME"); auto homeDir = getEnv("HOME");
if (homeDir) { if (homeDir) {

View file

@ -5,7 +5,7 @@
namespace nix { namespace nix {
Path getCacheDir() std::filesystem::path getCacheDir()
{ {
auto dir = getEnv("NIX_CACHE_HOME"); auto dir = getEnv("NIX_CACHE_HOME");
if (dir) { if (dir) {
@ -13,14 +13,14 @@ Path getCacheDir()
} else { } else {
auto xdgDir = getEnv("XDG_CACHE_HOME"); auto xdgDir = getEnv("XDG_CACHE_HOME");
if (xdgDir) { if (xdgDir) {
return *xdgDir + "/nix"; return std::filesystem::path{*xdgDir} / "nix";
} else { } else {
return getHome() + "/.cache/nix"; return getHome() / ".cache" / "nix";
} }
} }
} }
Path getConfigDir() std::filesystem::path getConfigDir()
{ {
auto dir = getEnv("NIX_CONFIG_HOME"); auto dir = getEnv("NIX_CONFIG_HOME");
if (dir) { if (dir) {
@ -28,26 +28,27 @@ Path getConfigDir()
} else { } else {
auto xdgDir = getEnv("XDG_CONFIG_HOME"); auto xdgDir = getEnv("XDG_CONFIG_HOME");
if (xdgDir) { if (xdgDir) {
return *xdgDir + "/nix"; return std::filesystem::path{*xdgDir} / "nix";
} else { } 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"); auto configDirs = getEnv("XDG_CONFIG_DIRS").value_or("/etc/xdg");
std::vector<Path> result = tokenizeString<std::vector<std::string>>(configDirs, ":"); auto tokens = tokenizeString<std::vector<std::string>>(configDirs, ":");
for (auto & p : result) { std::vector<std::filesystem::path> result;
p += "/nix"; result.push_back(configHome);
for (auto & token : tokens) {
result.push_back(std::filesystem::path{token} / "nix");
} }
result.insert(result.begin(), configHome);
return result; return result;
} }
Path getDataDir() std::filesystem::path getDataDir()
{ {
auto dir = getEnv("NIX_DATA_HOME"); auto dir = getEnv("NIX_DATA_HOME");
if (dir) { if (dir) {
@ -55,14 +56,14 @@ Path getDataDir()
} else { } else {
auto xdgDir = getEnv("XDG_DATA_HOME"); auto xdgDir = getEnv("XDG_DATA_HOME");
if (xdgDir) { if (xdgDir) {
return *xdgDir + "/nix"; return std::filesystem::path{*xdgDir} / "nix";
} else { } else {
return getHome() + "/.local/share/nix"; return getHome() / ".local" / "share" / "nix";
} }
} }
} }
Path getStateDir() std::filesystem::path getStateDir()
{ {
auto dir = getEnv("NIX_STATE_HOME"); auto dir = getEnv("NIX_STATE_HOME");
if (dir) { if (dir) {
@ -70,16 +71,16 @@ Path getStateDir()
} else { } else {
auto xdgDir = getEnv("XDG_STATE_HOME"); auto xdgDir = getEnv("XDG_STATE_HOME");
if (xdgDir) { if (xdgDir) {
return *xdgDir + "/nix"; return std::filesystem::path{*xdgDir} / "nix";
} else { } 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); createDirs(dir);
return dir; return dir;
} }

View file

@ -35,10 +35,10 @@ std::string getUserName()
return name; return name;
} }
Path getHome() std::filesystem::path getHome()
{ {
static Path homeDir = []() { static std::filesystem::path homeDir = []() {
Path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default"); std::filesystem::path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default");
assert(!homeDir.empty()); assert(!homeDir.empty());
return canonPath(homeDir); return canonPath(homeDir);
}(); }();