diff --git a/src/libexpr/eval-cache.cc b/src/libexpr/eval-cache.cc index 4f6c5fe8e78..b6c1c647faa 100644 --- a/src/libexpr/eval-cache.cc +++ b/src/libexpr/eval-cache.cc @@ -70,7 +70,7 @@ struct AttrDb { auto state(_state->lock()); - auto cacheDir = std::filesystem::path(getCacheDir()) / "eval-cache-v6"; + auto cacheDir = getCacheDir() / "eval-cache-v6"; createDirs(cacheDir); auto dbPath = cacheDir / (fingerprint.to_string(HashFormat::Base16, false) + ".sqlite"); diff --git a/src/libexpr/eval-settings.cc b/src/libexpr/eval-settings.cc index 286d588b07d..c0acede70a1 100644 --- a/src/libexpr/eval-settings.cc +++ b/src/libexpr/eval-settings.cc @@ -70,7 +70,7 @@ Strings EvalSettings::getDefaultNixPath() } }; - add(std::filesystem::path{getNixDefExpr()} / "channels"); + add(getNixDefExpr() / "channels"); auto profilesDirOpts = settings.getProfileDirsOptions(); add(rootChannelsDir(profilesDirOpts) / "nixpkgs", "nixpkgs"); add(rootChannelsDir(profilesDirOpts)); diff --git a/src/libfetchers/cache.cc b/src/libfetchers/cache.cc index 79e9f220cc2..36812344e94 100644 --- a/src/libfetchers/cache.cc +++ b/src/libfetchers/cache.cc @@ -44,8 +44,8 @@ struct CacheImpl : Cache { auto state(_state.lock()); - auto dbPath = (getCacheDir() / "fetcher-cache-v4.sqlite").string(); - createDirs(dirOf(dbPath)); + auto dbPath = getCacheDir() / "fetcher-cache-v4.sqlite"; + createDirs(dbPath.parent_path()); state->db = SQLite(dbPath, {.useWAL = nix::settings.useSQLiteWAL}); state->db.isCache(); diff --git a/src/libfetchers/git-lfs-fetch.cc b/src/libfetchers/git-lfs-fetch.cc index ec495c33556..5e2e4d91e1c 100644 --- a/src/libfetchers/git-lfs-fetch.cc +++ b/src/libfetchers/git-lfs-fetch.cc @@ -268,10 +268,10 @@ void Fetch::fetch( return; } - std::filesystem::path cacheDir = getCacheDir() / "git-lfs"; + auto cacheDir = getCacheDir() / "git-lfs"; std::string key = hashString(HashAlgorithm::SHA256, pointerFilePath.rel()).to_string(HashFormat::Base16, false) + "/" + pointer->oid; - std::filesystem::path cachePath = cacheDir / key; + auto cachePath = cacheDir / key; if (pathExists(cachePath)) { debug("using cache entry %s -> %s", key, PathFmt(cachePath)); sink(readFile(cachePath)); diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc index 44269cb1751..a9b24557d6b 100644 --- a/src/libfetchers/git.cc +++ b/src/libfetchers/git.cc @@ -44,8 +44,9 @@ static bool isCacheFileWithinTtl(const Settings & settings, time_t now, const Po std::filesystem::path getCachePath(std::string_view key, bool shallow) { - return getCacheDir() / "gitv3" - / (hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) + (shallow ? "-shallow" : "")); + auto name = + hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) + (shallow ? "-shallow" : ""); + return getCacheDir() / "gitv3" / std::move(name); } // Returns the name of the HEAD branch. diff --git a/src/libfetchers/registry.cc b/src/libfetchers/registry.cc index a19661f22ec..0f168e4da97 100644 --- a/src/libfetchers/registry.cc +++ b/src/libfetchers/registry.cc @@ -153,7 +153,7 @@ static std::shared_ptr getGlobalRegistry(const Settings & settings, St store2->addPermRoot(storePath, (getCacheDir() / "flake-registry.json").string()); return {store.requireStoreObjectAccessor(storePath)}; } else { - return SourcePath{getFSSourceAccessor(), CanonPath{path}}.resolveSymlinks(); + return SourcePath{makeFSSourceAccessor(path)}.resolveSymlinks(); } }(), Registry::Global); diff --git a/src/libflake/config.cc b/src/libflake/config.cc index fd0e9c75fdc..b7d5cd8999e 100644 --- a/src/libflake/config.cc +++ b/src/libflake/config.cc @@ -31,7 +31,7 @@ namespace nix::flake { // setting name -> setting value -> allow or ignore. typedef std::map> TrustedList; -std::filesystem::path trustedListPath() +static std::filesystem::path trustedListPath() { return getDataDir() / "trusted-settings.json"; } diff --git a/src/libstore-tests/machines.cc b/src/libstore-tests/machines.cc index e4186372de4..1c4a6aab15c 100644 --- a/src/libstore-tests/machines.cc +++ b/src/libstore-tests/machines.cc @@ -13,10 +13,6 @@ using testing::Eq; using testing::Field; using testing::SizeIs; -namespace nix::fs { -using namespace std::filesystem; -} - using namespace nix; TEST(machines, getMachinesWithEmptyBuilders) diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index c90ed1e5023..9a38f68036c 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -293,8 +293,7 @@ std::string optimisticLockProfile(const std::filesystem::path & profile) std::filesystem::path profilesDir(ProfileDirsOptions settings) { - auto profileRoot = - isRootUser() ? rootProfilesDir(settings) : std::filesystem::path{createNixStateDir()} / "profiles"; + auto profileRoot = isRootUser() ? rootProfilesDir(settings) : createNixStateDir() / "profiles"; createDirs(profileRoot); return profileRoot; } @@ -306,9 +305,8 @@ std::filesystem::path rootProfilesDir(ProfileDirsOptions settings) std::filesystem::path getDefaultProfile(ProfileDirsOptions settings) { - std::filesystem::path profileLink = settings.useXDGBaseDirectories - ? std::filesystem::path{createNixStateDir()} / "profile" - : std::filesystem::path{getHome()} / ".nix-profile"; + std::filesystem::path profileLink = + settings.useXDGBaseDirectories ? createNixStateDir() / "profile" : getHome() / ".nix-profile"; try { auto profile = profilesDir(settings) / "profile"; if (!pathExists(profileLink)) { diff --git a/src/libstore/ssh.cc b/src/libstore/ssh.cc index 1a99083669c..00c112c6f38 100644 --- a/src/libstore/ssh.cc +++ b/src/libstore/ssh.cc @@ -98,7 +98,7 @@ void SSHMaster::addCommonSSHOpts(Strings & args) args.insert(args.end(), {"-i", keyFile}); if (!sshPublicHostKey.empty()) { std::filesystem::path fileName = tmpDir->path() / "host-key"; - writeFile(fileName.string(), authority.host + " " + sshPublicHostKey + "\n"); + writeFile(fileName, authority.host + " " + sshPublicHostKey + "\n"); args.insert(args.end(), {"-oUserKnownHostsFile=" + fileName.string()}); } if (compress) diff --git a/src/libstore/store-registration.cc b/src/libstore/store-registration.cc index 633556e481a..42caa819034 100644 --- a/src/libstore/store-registration.cc +++ b/src/libstore/store-registration.cc @@ -42,17 +42,18 @@ ref resolveStoreConfig(StoreReference && storeURI) /* If /nix doesn't exist, there is no daemon socket, and we're not root, then automatically set up a chroot store in ~/.local/share/nix/root. */ - auto chrootStore = getDataDir() + "/root"; + auto chrootStore = getDataDir() / "root"; if (!pathExists(chrootStore)) { try { createDirs(chrootStore); } catch (SystemError & e) { return make_ref(params); } - warn("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore); + warn("%s does not exist, so Nix will use %s as a chroot store", stateDir, PathFmt(chrootStore)); } else - debug("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore); - return make_ref("local", chrootStore, params); + debug( + "%s does not exist, so Nix will use %s as a chroot store", stateDir, PathFmt(chrootStore)); + return make_ref("local", chrootStore.string(), params); } #endif else diff --git a/src/libutil/executable-path.cc b/src/libutil/executable-path.cc index 75ab91f3a16..d57b6231a87 100644 --- a/src/libutil/executable-path.cc +++ b/src/libutil/executable-path.cc @@ -20,17 +20,23 @@ ExecutablePath ExecutablePath::load() } ExecutablePath ExecutablePath::parse(const OsString & path) +{ + ExecutablePath ret; + ret.parseAppend(path); + return ret; +} + +void ExecutablePath::parseAppend(const OsString & path) { auto strings = path.empty() ? (std::list{}) : basicSplitString, OsChar>(path, path_var_separator); - std::vector ret; - ret.reserve(strings.size()); + directories.reserve(directories.size() + strings.size()); std::transform( std::make_move_iterator(strings.begin()), std::make_move_iterator(strings.end()), - std::back_inserter(ret), + std::back_inserter(directories), [](OsString && str) { return std::filesystem::path{ str.empty() @@ -45,8 +51,6 @@ ExecutablePath ExecutablePath::parse(const OsString & path) : std::move(str), }; }); - - return {ret}; } OsString ExecutablePath::render() const diff --git a/src/libutil/include/nix/util/executable-path.hh b/src/libutil/include/nix/util/executable-path.hh index cf6f3b25200..4e9fe39a5ea 100644 --- a/src/libutil/include/nix/util/executable-path.hh +++ b/src/libutil/include/nix/util/executable-path.hh @@ -33,6 +33,12 @@ struct ExecutablePath */ static ExecutablePath parse(const OsString & path); + /** + * Like `parse` but appends new entries to the end of an existing + * `ExecutablePath`. + */ + void parseAppend(const OsString & path); + /** * Load the `PATH` environment variable and `parse` it. */ diff --git a/src/libutil/include/nix/util/file-system.hh b/src/libutil/include/nix/util/file-system.hh index c0966deae78..408d7c588a6 100644 --- a/src/libutil/include/nix/util/file-system.hh +++ b/src/libutil/include/nix/util/file-system.hh @@ -12,6 +12,7 @@ #include "nix/util/file-descriptor.hh" #include "nix/util/file-path.hh" +#include #include #include #include diff --git a/src/libutil/include/nix/util/users.hh b/src/libutil/include/nix/util/users.hh index 7a556fa8b7b..dec2c4315e6 100644 --- a/src/libutil/include/nix/util/users.hh +++ b/src/libutil/include/nix/util/users.hh @@ -2,13 +2,12 @@ ///@file #include - -#include "nix/util/types.hh" - #ifndef _WIN32 # include #endif +#include "nix/util/types.hh" + namespace nix { std::string getUserName(); diff --git a/src/libutil/users.cc b/src/libutil/users.cc index 814855071fd..f05dfcf760e 100644 --- a/src/libutil/users.cc +++ b/src/libutil/users.cc @@ -1,6 +1,7 @@ #include "nix/util/util.hh" #include "nix/util/users.hh" #include "nix/util/environment-variables.hh" +#include "nix/util/executable-path.hh" #include "nix/util/file-system.hh" #ifndef _WIN32 @@ -13,7 +14,7 @@ namespace nix { std::filesystem::path getCacheDir() { - auto dir = getEnv("NIX_CACHE_HOME"); + auto dir = getEnvOs(OS_STR("NIX_CACHE_HOME")); if (dir) return *dir; #ifndef _WIN32 @@ -25,7 +26,7 @@ std::filesystem::path getCacheDir() std::filesystem::path getConfigDir() { - auto dir = getEnv("NIX_CONFIG_HOME"); + auto dir = getEnvOs(OS_STR("NIX_CONFIG_HOME")); if (dir) return *dir; #ifndef _WIN32 @@ -51,7 +52,7 @@ std::vector getConfigDirs() std::filesystem::path getDataDir() { - auto dir = getEnv("NIX_DATA_HOME"); + auto dir = getEnvOs(OS_STR("NIX_DATA_HOME")); if (dir) return *dir; #ifndef _WIN32 @@ -63,7 +64,7 @@ std::filesystem::path getDataDir() std::filesystem::path getStateDir() { - auto dir = getEnv("NIX_STATE_HOME"); + auto dir = getEnvOs(OS_STR("NIX_STATE_HOME")); if (dir) return *dir; #ifndef _WIN32 @@ -84,9 +85,10 @@ std::string expandTilde(std::string_view path) { // TODO: expand ~user ? auto tilde = path.substr(0, 2); - if (tilde == "~/" || tilde == "~") - return getHome().string() + std::string(path.substr(1)); - else + if (tilde == "~/" || tilde == "~") { + auto suffix = path.size() >= 2 ? std::string(path.substr(2)) : std::string{}; + return (getHome() / suffix).string(); + } else return std::string(path); } diff --git a/src/libutil/windows/users.cc b/src/libutil/windows/users.cc index 36392ff5006..caab6745d4f 100644 --- a/src/libutil/windows/users.cc +++ b/src/libutil/windows/users.cc @@ -37,9 +37,9 @@ std::string getUserName() std::filesystem::path getHome() { static std::filesystem::path homeDir = []() { - std::filesystem::path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default"); + std::filesystem::path homeDir = getEnvOs(L"USERPROFILE").value_or(L"C:\\Users\\Default"); assert(!homeDir.empty()); - return canonPath(homeDir.string()); + return std::filesystem::path{canonPath(homeDir.string())}; }(); return homeDir; } diff --git a/src/nix/bundle.cc b/src/nix/bundle.cc index e11f37b847e..cbd9581dae4 100644 --- a/src/nix/bundle.cc +++ b/src/nix/bundle.cc @@ -7,10 +7,6 @@ #include "nix/expr/eval-inline.hh" #include "nix/store/globals.hh" -namespace nix::fs { -using namespace std::filesystem; -} - using namespace nix; struct CmdBundle : InstallableValueCommand diff --git a/src/nix/config-check.cc b/src/nix/config-check.cc index 3308dd0a51a..76c2eaa74ec 100644 --- a/src/nix/config-check.cc +++ b/src/nix/config-check.cc @@ -11,10 +11,6 @@ #include "nix/util/executable-path.hh" #include "nix/store/globals.hh" -namespace nix::fs { -using namespace std::filesystem; -} - using namespace nix; namespace { diff --git a/src/nix/develop.cc b/src/nix/develop.cc index 09fd62b7efa..826b17800d9 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -21,10 +21,6 @@ #include "nix/util/strings.hh" -namespace nix::fs { -using namespace std::filesystem; -} - using namespace nix; struct DevelopSettings : Config diff --git a/src/nix/eval.cc b/src/nix/eval.cc index 584b2122f09..ba3e4142a78 100644 --- a/src/nix/eval.cc +++ b/src/nix/eval.cc @@ -10,10 +10,6 @@ using namespace nix; -namespace nix::fs { -using namespace std::filesystem; -} - struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption { bool raw = false; diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 8906628ff10..e1ce69b204c 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -29,10 +29,6 @@ // FIXME is this supposed to be private or not? #include "flake-command.hh" -namespace nix::fs { -using namespace std::filesystem; -} - using namespace nix; using namespace nix::flake; using json = nlohmann::json; diff --git a/src/nix/nix-channel/nix-channel.cc b/src/nix/nix-channel/nix-channel.cc index 6c6a6356fc3..063be334271 100644 --- a/src/nix/nix-channel/nix-channel.cc +++ b/src/nix/nix-channel/nix-channel.cc @@ -188,11 +188,12 @@ static int main_nix_channel(int argc, char ** argv) { // Figure out the name of the `.nix-channels' file to use auto home = getHome(); - channelsList = settings.useXDGBaseDirectories ? createNixStateDir() + "/channels" : home + "/.nix-channels"; + channelsList = + settings.useXDGBaseDirectories ? (createNixStateDir() / "channels").string() : home + "/.nix-channels"; nixDefExpr = getNixDefExpr(); // Figure out the name of the channels profile. - profile = profilesDir(settings.getProfileDirsOptions()) + "/channels"; + profile = (profilesDir(settings.getProfileDirsOptions()) / "channels").string(); createDirs(dirOf(profile)); enum { cNone, cAdd, cRemove, cList, cUpdate, cListGenerations, cRollback } cmd = cNone; diff --git a/src/nix/nix-collect-garbage/nix-collect-garbage.cc b/src/nix/nix-collect-garbage/nix-collect-garbage.cc index 88fbc9db0e2..66030c78035 100644 --- a/src/nix/nix-collect-garbage/nix-collect-garbage.cc +++ b/src/nix/nix-collect-garbage/nix-collect-garbage.cc @@ -13,10 +13,6 @@ #include #include -namespace nix::fs { -using namespace std::filesystem; -} - using namespace nix; std::string deleteOlderThan; diff --git a/src/nix/nix-env/nix-env.cc b/src/nix/nix-env/nix-env.cc index 18bb2c74a03..946b6ece480 100644 --- a/src/nix/nix-env/nix-env.cc +++ b/src/nix/nix-env/nix-env.cc @@ -1294,7 +1294,7 @@ static void opSwitchProfile(Globals & globals, Strings opFlags, Strings opArgs) if (opArgs.size() != 1) throw UsageError("exactly one argument expected"); - Path profile = absPath(opArgs.front()); + auto profile = absPath(std::filesystem::path{opArgs.front()}); auto profileLink = settings.useXDGBaseDirectories ? createNixStateDir() / "profile" : getHome() / ".nix-profile"; switchLink(profileLink, profile); diff --git a/src/nix/run.cc b/src/nix/run.cc index c8636f7585d..be46319364c 100644 --- a/src/nix/run.cc +++ b/src/nix/run.cc @@ -24,10 +24,6 @@ extern char ** environ __attribute__((weak)); -namespace nix::fs { -using namespace std::filesystem; -} - using namespace nix; std::string chrootHelperName = "__run_in_chroot";