Skip to content

Commit

Permalink
WIP fix some type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantk232 authored and Ericson2314 committed Jul 3, 2024
1 parent d1a3df5 commit 6a7d34f
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,13 @@ EvalState::~EvalState()
void EvalState::allowPath(const Path & path)
{
if (auto rootFS2 = rootFS.dynamic_pointer_cast<AllowListSourceAccessor>())
rootFS2->allowPrefix(CanonPath(path));
rootFS2->allowPrefix(CanonPath(path.string()));
}

void EvalState::allowPath(const StorePath & storePath)
{
if (auto rootFS2 = rootFS.dynamic_pointer_cast<AllowListSourceAccessor>())
rootFS2->allowPrefix(CanonPath(store->toRealPath(storePath)));
rootFS2->allowPrefix(CanonPath(store->toRealPath(storePath).string()));
}

void EvalState::allowAndSetStorePathString(const StorePath & storePath, Value & v)
Expand Down Expand Up @@ -1970,7 +1970,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
else if (firstType == nPath) {
if (!context.empty())
state.error<EvalError>("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);
}
Expand Down Expand Up @@ -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/"))
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())};
}

}
13 changes: 7 additions & 6 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<EvalError>("path '%1%' is not in the Nix store", path)
.atPos(pos).debugThrow();
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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());
Expand All @@ -2371,7 +2372,7 @@ static void addPath(
std::unique_ptr<PathFilter> filter;
if (filterFun)
filter = std::make_unique<PathFilter>([&](const Path & p) {
auto p2 = CanonPath(p);
auto p2 = CanonPath(p.string());
return state.callPathFilter(filterFun, {path.accessor, p2}, p2.abs(), pos);
});

Expand Down Expand Up @@ -4663,7 +4664,7 @@ void EvalState::createBaseEnv()
)",
});

v.mkString(store->storeDir);
v.mkString(store->storeDir.string());
addConstant("__storeDir", v, {
.type = nString,
.doc = R"(
Expand Down
4 changes: 2 additions & 2 deletions src/libexpr/primops/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<EvalError>(
"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");
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/build/derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
20 changes: 19 additions & 1 deletion src/libstore/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -94,6 +94,15 @@ StorePath StoreDirConfig::parseStorePath(std::string_view path) const
return StorePath(baseNameOf(p));
}

std::set<StorePath> StoreDirConfig::parseStorePathSet(PathSet paths) const
{
std::set<StorePath> result;
for (auto &p : paths) {
result.insert(parseStorePath(p.string()));
}
return result;
}

std::optional<StorePath> StoreDirConfig::maybeParseStorePath(std::string_view path) const
{
try {
Expand All @@ -113,4 +122,13 @@ std::string StoreDirConfig::printStorePath(const StorePath & path) const
return (storeDir + "/").append(path.to_string());
}

std::set<std::string> StoreDirConfig::printStorePathSet(const StorePathSet & paths) const
{
std::set<std::string> result;
for (auto & p : paths)
result.insert(printStorePath(p));

return result;
}

}
4 changes: 3 additions & 1 deletion src/libstore/store-dir-config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ struct StoreDirConfig : public Config

// pure methods

StorePath parseStorePath(std::string_view path) const;
StorePath parseStorePath(PathView path) const;
std::set<StorePath> parseStorePathSet(PathSet paths) const;

std::optional<StorePath> maybeParseStorePath(std::string_view path) const;

std::string printStorePath(const StorePath & path) const;
std::set<std::string> printStorePathSet(const StorePathSet & paths) const;

/**
* Display a set of paths in human-readable form (i.e., between quotes
Expand Down
5 changes: 5 additions & 0 deletions src/libutil/args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ protected:
, arity(1)
{ }

Handler(std::optional<std::filesystem::path> * dest)
: fun([=](std::vector<std::string> ss) { *dest = std::filesystem::path { ss[0] }; })
, arity(1)
{ }

template<class T>
Handler(T * dest, const T & val)
: fun([dest, val](std::vector<std::string> ss) { *dest = val; })
Expand Down
2 changes: 1 addition & 1 deletion src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/nix-channel/nix-channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 + " }"
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ static int main_nix_env(int argc, char * * argv)
globals.instSource.nixExprPath = std::make_shared<SourcePath>(
file != ""
? lookupFileArg(*globals.state, file)
: globals.state->rootPath(CanonPath(nixExprPath)));
: globals.state->rootPath(CanonPath(nixExprPath.string())));

globals.instSource.autoArgs = myArgs.getAutoArgs(*globals.state);

Expand Down
2 changes: 1 addition & 1 deletion src/nix-store/nix-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
6 changes: 3 additions & 3 deletions src/nix/config-check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 &) {
Expand Down
2 changes: 1 addition & 1 deletion src/nix/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Paths>(accessor->readFile(propPath)))
todo.push(store->parseStorePath(p));
todo.push(store->parseStorePath(p.string()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/nix/prefetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ std::string resolveMirrorUrl(EvalState & state, const std::string & url)

std::tuple<StorePath, Hash> prefetchFile(
ref<Store> store,
std::string_view url,
PathView url,
std::optional<std::string> name,
HashAlgorithm hashAlgo,
std::optional<Hash> expectedHash,
Expand Down Expand Up @@ -110,7 +110,7 @@ std::tuple<StorePath, Hash> 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);

Expand Down
4 changes: 2 additions & 2 deletions src/nix/profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/nix/run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static auto rCmdRun = registerCommand<CmdRun>("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++];
Expand Down Expand Up @@ -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{"/"}) {
Expand Down
10 changes: 5 additions & 5 deletions src/nix/upgrade-nix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,21 @@ 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/<name> 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);

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)))
Expand All @@ -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);
}
};

Expand Down

0 comments on commit 6a7d34f

Please sign in to comment.