From 6a7d34ff0c7ff1acca8517af1dd67fbcb7f6c6b3 Mon Sep 17 00:00:00 2001 From: siddhantCodes Date: Sun, 30 Jun 2024 16:09:20 +0530 Subject: [PATCH] WIP fix some type errors --- src/libexpr/eval.cc | 8 ++++---- src/libexpr/paths.cc | 2 +- src/libexpr/primops.cc | 13 +++++++------ src/libexpr/primops/context.cc | 4 ++-- src/libstore/build/derivation-goal.cc | 4 ++-- src/libstore/path.cc | 20 +++++++++++++++++++- src/libstore/store-dir-config.hh | 4 +++- src/libutil/args.hh | 5 +++++ src/nix-build/nix-build.cc | 2 +- src/nix-channel/nix-channel.cc | 4 +++- src/nix-env/nix-env.cc | 2 +- src/nix-store/nix-store.cc | 2 +- src/nix/config-check.cc | 6 +++--- src/nix/env.cc | 2 +- src/nix/prefetch.cc | 4 ++-- src/nix/profile.cc | 4 ++-- src/nix/run.cc | 6 +++--- src/nix/upgrade-nix.cc | 10 +++++----- 18 files changed, 65 insertions(+), 37 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index d2be00e55f1..c68ad1f7ddd 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -359,13 +359,13 @@ EvalState::~EvalState() void EvalState::allowPath(const Path & path) { if (auto rootFS2 = rootFS.dynamic_pointer_cast()) - rootFS2->allowPrefix(CanonPath(path)); + rootFS2->allowPrefix(CanonPath(path.string())); } void EvalState::allowPath(const StorePath & storePath) { if (auto rootFS2 = rootFS.dynamic_pointer_cast()) - rootFS2->allowPrefix(CanonPath(store->toRealPath(storePath))); + rootFS2->allowPrefix(CanonPath(store->toRealPath(storePath).string())); } void EvalState::allowAndSetStorePathString(const StorePath & storePath, Value & v) @@ -1970,7 +1970,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v) else if (firstType == nPath) { if (!context.empty()) state.error("a string that refers to a store path cannot be appended to a path").atPos(pos).withFrame(env, *this).debugThrow(); - v.mkPath(state.rootPath(CanonPath(canonPath(str())))); + v.mkPath(state.rootPath(CanonPath(canonPath(str()).string()))); } else v.mkStringMove(c_str(), context); } @@ -2738,7 +2738,7 @@ SourcePath EvalState::findFile(const LookupPath & lookupPath, const std::string_ auto r = *rOpt; Path res = suffix == "" ? r : concatStrings(r, "/", suffix); - if (pathExists(res)) return rootPath(CanonPath(canonPath(res))); + if (pathExists(res)) return rootPath(CanonPath(canonPath(res).string())); } if (hasPrefix(path, "nix/")) diff --git a/src/libexpr/paths.cc b/src/libexpr/paths.cc index 50d0d989564..9426b4dd50d 100644 --- a/src/libexpr/paths.cc +++ b/src/libexpr/paths.cc @@ -9,7 +9,7 @@ SourcePath EvalState::rootPath(CanonPath path) SourcePath EvalState::rootPath(PathView path) { - return {rootFS, CanonPath(absPath(path))}; + return {rootFS, CanonPath(absPath(path).string())}; } } diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 7a946bdaac3..e0730fc14d6 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -134,7 +134,7 @@ static SourcePath realisePath(EvalState & state, const PosIdx pos, Value & v, st if (!context.empty() && path.accessor == state.rootFS) { auto rewrites = state.realiseContext(context); auto realPath = state.toRealPath(rewriteStrings(path.path.abs(), rewrites), context); - path = {path.accessor, CanonPath(realPath)}; + path = {path.accessor, CanonPath(realPath.string())}; } return resolveSymlinks ? path.resolveSymlinks(*resolveSymlinks) : path; } catch (Error & e) { @@ -1612,7 +1612,7 @@ static void prim_storePath(EvalState & state, const PosIdx pos, Value * * args, directly in the store. The latter condition is necessary so e.g. nix-push does the right thing. */ if (!state.store->isStorePath(path.abs())) - path = CanonPath(canonPath(path.abs(), true)); + path = CanonPath(canonPath(PathView{path.abs()}, true).string()); if (!state.store->isInStore(path.abs())) state.error("path '%1%' is not in the Nix store", path) .atPos(pos).debugThrow(); @@ -1738,7 +1738,7 @@ static void prim_dirOf(EvalState & state, const PosIdx pos, Value * * args, Valu auto path = state.coerceToString(pos, *args[0], context, "while evaluating the first argument passed to 'builtins.dirOf'", false, false); - auto dir = dirOf(*path); + auto dir = dirOf(PathView{*path}).string(); v.mkString(dir, context); } } @@ -2357,7 +2357,8 @@ static void addPath( // FIXME: handle CA derivation outputs (where path needs to // be rewritten to the actual output). auto rewrites = state.realiseContext(context); - path = {state.rootFS, CanonPath(state.toRealPath(rewriteStrings(path.path.abs(), rewrites), context))}; + auto realPath = state.toRealPath(rewriteStrings(path.path.abs(), rewrites), context); + path = {state.rootFS, CanonPath(realPath.string())}; try { auto [storePath, subPath] = state.store->toStorePath(path.path.abs()); @@ -2371,7 +2372,7 @@ static void addPath( std::unique_ptr filter; if (filterFun) filter = std::make_unique([&](const Path & p) { - auto p2 = CanonPath(p); + auto p2 = CanonPath(p.string()); return state.callPathFilter(filterFun, {path.accessor, p2}, p2.abs(), pos); }); @@ -4663,7 +4664,7 @@ void EvalState::createBaseEnv() )", }); - v.mkString(store->storeDir); + v.mkString(store->storeDir.string()); addConstant("__storeDir", v, { .type = nString, .doc = R"( diff --git a/src/libexpr/primops/context.cc b/src/libexpr/primops/context.cc index 8c3f1b4e8b0..390ffd0eb31 100644 --- a/src/libexpr/primops/context.cc +++ b/src/libexpr/primops/context.cc @@ -271,12 +271,12 @@ static void prim_appendContext(EvalState & state, const PosIdx pos, Value * * ar auto sAllOutputs = state.symbols.create("allOutputs"); for (auto & i : *args[1]->attrs()) { const auto & name = state.symbols[i.name]; - if (!state.store->isStorePath(name)) + if (!state.store->isStorePath(PathView{name})) state.error( "context key '%s' is not a store path", name ).atPos(i.pos).debugThrow(); - auto namePath = state.store->parseStorePath(name); + auto namePath = state.store->parseStorePath(PathView{name}); if (!settings.readOnlyMode) state.store->ensurePath(namePath); state.forceAttrs(*i.value, i.pos, "while evaluating the value of a string context"); diff --git a/src/libstore/build/derivation-goal.cc b/src/libstore/build/derivation-goal.cc index 29bf494a68a..4ef0387cddc 100644 --- a/src/libstore/build/derivation-goal.cc +++ b/src/libstore/build/derivation-goal.cc @@ -897,9 +897,9 @@ void runPostBuildHook( hookEnvironment.emplace("DRV_PATH", store.printStorePath(drvPath)); hookEnvironment.emplace("OUT_PATHS", chomp(concatStringsSep(" ", ({ StringSet paths; - for (auto & p : outPaths) + for (auto & p : outputPaths) paths.insert(store.printStorePath(p)); - paths + paths; })))); hookEnvironment.emplace("NIX_CONFIG", globalConfig.toKeyValue()); diff --git a/src/libstore/path.cc b/src/libstore/path.cc index 04e7f97ea49..2f94e06be50 100644 --- a/src/libstore/path.cc +++ b/src/libstore/path.cc @@ -75,7 +75,7 @@ StorePath StorePath::random(std::string_view name) return StorePath(Hash::random(HashAlgorithm::SHA1), name); } -StorePath StoreDirConfig::parseStorePath(std::string_view path) const +StorePath StoreDirConfig::parseStorePath(PathView path) const { // On Windows, `/nix/store` is not a canonical path. More broadly it // is unclear whether this function should be using the native @@ -94,6 +94,15 @@ StorePath StoreDirConfig::parseStorePath(std::string_view path) const return StorePath(baseNameOf(p)); } +std::set StoreDirConfig::parseStorePathSet(PathSet paths) const +{ + std::set result; + for (auto &p : paths) { + result.insert(parseStorePath(p.string())); + } + return result; +} + std::optional StoreDirConfig::maybeParseStorePath(std::string_view path) const { try { @@ -113,4 +122,13 @@ std::string StoreDirConfig::printStorePath(const StorePath & path) const return (storeDir + "/").append(path.to_string()); } +std::set StoreDirConfig::printStorePathSet(const StorePathSet & paths) const +{ + std::set result; + for (auto & p : paths) + result.insert(printStorePath(p)); + + return result; +} + } diff --git a/src/libstore/store-dir-config.hh b/src/libstore/store-dir-config.hh index a49969bead2..d6690f3ce23 100644 --- a/src/libstore/store-dir-config.hh +++ b/src/libstore/store-dir-config.hh @@ -37,11 +37,13 @@ struct StoreDirConfig : public Config // pure methods - StorePath parseStorePath(std::string_view path) const; + StorePath parseStorePath(PathView path) const; + std::set parseStorePathSet(PathSet paths) const; std::optional maybeParseStorePath(std::string_view path) const; std::string printStorePath(const StorePath & path) const; + std::set printStorePathSet(const StorePathSet & paths) const; /** * Display a set of paths in human-readable form (i.e., between quotes diff --git a/src/libutil/args.hh b/src/libutil/args.hh index 658183dcfa5..55985e3c60a 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -118,6 +118,11 @@ protected: , arity(1) { } + Handler(std::optional * dest) + : fun([=](std::vector ss) { *dest = std::filesystem::path { ss[0] }; }) + , arity(1) + { } + template Handler(T * dest, const T & val) : fun([dest, val](std::vector ss) { *dest = val; }) diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 238bb810c4b..0c9b070822b 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -585,7 +585,7 @@ static void main_nix_build(int argc, char * * argv) shellEscape(tmpDir.path().string()), (pure ? "" : "p=$PATH; "), (pure ? "" : "PATH=$PATH:$p; unset p; "), - shellEscape(dirOf(*shell)), + shellEscape(dirOf(*shell).string()), shellEscape(*shell), (getenv("TZ") ? (std::string("export TZ=") + shellEscape(getenv("TZ")) + "; ") : ""), envCommand); diff --git a/src/nix-channel/nix-channel.cc b/src/nix-channel/nix-channel.cc index fec970526a0..d7c41f9dd30 100644 --- a/src/nix-channel/nix-channel.cc +++ b/src/nix-channel/nix-channel.cc @@ -132,7 +132,9 @@ static void update(const StringSet & channelNames) } } // Regardless of where it came from, add the expression representing this channel to accumulated expression - exprs.push_back("f: f { name = \"" + cname + "\"; channelName = \"" + name + "\"; src = builtins.storePath \"" + filename + "\"; " + extraAttrs + " }"); + exprs.push_back( + "f: f { name = \"" + cname + "\"; channelName = \"" + name + "\"; src = builtins.storePath \"" + filename.string() + "\"; " + extraAttrs + " }" + ); } } diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index c72378cd5f4..3c8ddba1640 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1531,7 +1531,7 @@ static int main_nix_env(int argc, char * * argv) globals.instSource.nixExprPath = std::make_shared( file != "" ? lookupFileArg(*globals.state, file) - : globals.state->rootPath(CanonPath(nixExprPath))); + : globals.state->rootPath(CanonPath(nixExprPath.string()))); globals.instSource.autoArgs = myArgs.getAutoArgs(*globals.state); diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index d0840a02e5e..4a66d55652b 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -491,7 +491,7 @@ static void opPrintEnv(Strings opFlags, Strings opArgs) if (opArgs.size() != 1) throw UsageError("'--print-env' requires one derivation store path"); Path drvPath = opArgs.front(); - Derivation drv = store->derivationFromPath(store->parseStorePath(drvPath)); + Derivation drv = store->derivationFromPath(store->parseStorePath(drvPath.string())); /* Print each environment variable in the derivation in a format * that can be sourced by the shell. */ diff --git a/src/nix/config-check.cc b/src/nix/config-check.cc index 9575bf33887..53bc8fa6c9f 100644 --- a/src/nix/config-check.cc +++ b/src/nix/config-check.cc @@ -100,11 +100,11 @@ struct CmdConfigCheck : StoreCommand try { Path userEnv = canonPath(profileDir, true); - if (store->isStorePath(userEnv) && hasSuffix(userEnv, "user-environment")) { - while (profileDir.find("/profiles/") == std::string::npos && std::filesystem::is_symlink(profileDir)) + if (store->isStorePath(userEnv) && hasSuffix(userEnv.string(), "user-environment")) { + while (profileDir.string().find("/profiles/") == std::string::npos && std::filesystem::is_symlink(profileDir)) profileDir = absPath(readLink(profileDir), dirOf(profileDir)); - if (profileDir.find("/profiles/") == std::string::npos) + if (profileDir.string().find("/profiles/") == std::string::npos) dirs.insert(dir); } } catch (SystemError &) { diff --git a/src/nix/env.cc b/src/nix/env.cc index 021c47cbbd0..41bc19b1b56 100644 --- a/src/nix/env.cc +++ b/src/nix/env.cc @@ -86,7 +86,7 @@ struct CmdShell : InstallablesCommand, MixEnvironment CanonPath(store->printStorePath(path)) / "nix-support" / "propagated-user-env-packages"); if (auto st = accessor->maybeLstat(propPath); st && st->type == SourceAccessor::tRegular) { for (auto & p : tokenizeString(accessor->readFile(propPath))) - todo.push(store->parseStorePath(p)); + todo.push(store->parseStorePath(p.string())); } } diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index 5e890f3c8ce..916e73f7006 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -50,7 +50,7 @@ std::string resolveMirrorUrl(EvalState & state, const std::string & url) std::tuple prefetchFile( ref store, - std::string_view url, + PathView url, std::optional name, HashAlgorithm hashAlgo, std::optional expectedHash, @@ -110,7 +110,7 @@ std::tuple prefetchFile( if (unpack) { Activity act(*logger, lvlChatty, actUnknown, fmt("unpacking '%s'", url)); - auto unpacked = (tmpDir.path() / "unpacked").string(); + auto unpacked = tmpDir.path() / "unpacked"; createDirs(unpacked); unpackTarfile(tmpFile.string(), unpacked); diff --git a/src/nix/profile.cc b/src/nix/profile.cc index c89b8c9bde6..e1a596a2dd0 100644 --- a/src/nix/profile.cc +++ b/src/nix/profile.cc @@ -428,10 +428,10 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile for (auto it = begin; it != end; it++) { auto & [name, profileElement] = *it; for (auto & storePath : profileElement.storePaths) { - if (conflictError.fileA.starts_with(store->printStorePath(storePath))) { + if (conflictError.fileA.string().starts_with(store->printStorePath(storePath))) { return std::tuple(conflictError.fileA, name, profileElement.toInstallables(*store)); } - if (conflictError.fileB.starts_with(store->printStorePath(storePath))) { + if (conflictError.fileB.string().starts_with(store->printStorePath(storePath))) { return std::tuple(conflictError.fileB, name, profileElement.toInstallables(*store)); } } diff --git a/src/nix/run.cc b/src/nix/run.cc index c1aae168577..3d1ea244bf2 100644 --- a/src/nix/run.cc +++ b/src/nix/run.cc @@ -137,7 +137,7 @@ static auto rCmdRun = registerCommand("run"); void chrootHelper(int argc, char * * argv) { int p = 1; - std::string storeDir = argv[p++]; + std::filesystem::path storeDir = argv[p++]; std::string realStoreDir = argv[p++]; std::string system = argv[p++]; std::string cmd = argv[p++]; @@ -167,9 +167,9 @@ void chrootHelper(int argc, char * * argv) Path tmpDir = createTempDir(); - createDirs(tmpDir + storeDir); + createDirs(tmpDir / storeDir); - if (mount(realStoreDir.c_str(), (tmpDir + storeDir).c_str(), "", MS_BIND, 0) == -1) + if (mount(realStoreDir.c_str(), (tmpDir / storeDir).c_str(), "", MS_BIND, 0) == -1) throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir); for (auto entry : std::filesystem::directory_iterator{"/"}) { diff --git a/src/nix/upgrade-nix.cc b/src/nix/upgrade-nix.cc index 29297253bda..0db4257d178 100644 --- a/src/nix/upgrade-nix.cc +++ b/src/nix/upgrade-nix.cc @@ -115,13 +115,13 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand printInfo("found Nix in '%s'", where); - if (hasPrefix(where, "/run/current-system")) + if (hasPrefix(where.string(), "/run/current-system")) throw Error("Nix on NixOS must be upgraded via 'nixos-rebuild'"); Path profileDir = dirOf(where); // Resolve profile to /nix/var/nix/profiles/ link. - while (canonPath(profileDir).find("/profiles/") == std::string::npos && std::filesystem::is_symlink(profileDir)) + while (canonPath(profileDir).string().find("/profiles/") == std::string::npos && std::filesystem::is_symlink(profileDir)) profileDir = readLink(profileDir); printInfo("found profile '%s'", profileDir); @@ -129,7 +129,7 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand Path userEnv = canonPath(profileDir, true); if (baseNameOf(where) != "bin" || - !hasSuffix(userEnv, "user-environment")) + !hasSuffix(userEnv.string(), "user-environment")) throw Error("directory '%s' does not appear to be part of a Nix profile", where); if (!store->isValidPath(store->parseStorePath(userEnv))) @@ -152,8 +152,8 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand state->eval(state->parseExprFromString(res.data, state->rootPath(CanonPath("/no-such-path"))), *v); Bindings & bindings(*state->allocBindings(0)); auto v2 = findAlongAttrPath(*state, settings.thisSystem, bindings, *v).first; - - return store->parseStorePath(state->forceString(*v2, noPos, "while evaluating the path tho latest nix version")); + auto p = PathView{state->forceString(*v2, noPos, "while evaluating the path tho latest nix version")}; + return store->parseStorePath(p); } };