libexpr: associate path config with EvalContext

Change-Id: Id25f990ab4ae9d9e76c91c07d7397689acf57eca
This commit is contained in:
eldritch horrors 2024-12-03 20:38:41 +01:00
parent 1f37fc85ac
commit 7fa6e785ff
9 changed files with 34 additions and 34 deletions

View file

@ -104,7 +104,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
/* Construct a Nix expression that calls the user environment /* Construct a Nix expression that calls the user environment
builder with the manifest as argument. */ builder with the manifest as argument. */
auto attrs = state.ctx.buildBindings(3); auto attrs = state.ctx.buildBindings(3);
state.paths.mkStorePathString(manifestFile, attrs.alloc("manifest")); state.ctx.paths.mkStorePathString(manifestFile, attrs.alloc("manifest"));
attrs.insert(state.ctx.symbols.create("derivations"), &manifest); attrs.insert(state.ctx.symbols.create("derivations"), &manifest);
Value args; Value args;
args.mkAttrs(attrs); args.mkAttrs(attrs);

View file

@ -162,8 +162,8 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile)
} }
} else if (pathExists(profile + "/manifest.nix")) { } else if (pathExists(profile + "/manifest.nix")) {
// FIXME: needed because of pure mode; ugly. // FIXME: needed because of pure mode; ugly.
state.paths.allowPath(state.ctx.store->followLinksToStore(profile)); state.ctx.paths.allowPath(state.ctx.store->followLinksToStore(profile));
state.paths.allowPath(state.ctx.store->followLinksToStore(profile + "/manifest.nix")); state.ctx.paths.allowPath(state.ctx.store->followLinksToStore(profile + "/manifest.nix"));
auto drvInfos = queryInstalled(state, state.ctx.store->followLinksToStore(profile)); auto drvInfos = queryInstalled(state, state.ctx.store->followLinksToStore(profile));

View file

@ -939,7 +939,7 @@ struct CachedEvalFile
void EvalState::evalFile(const SourcePath & path_, Value & v) void EvalState::evalFile(const SourcePath & path_, Value & v)
{ {
auto path = paths.checkSourcePath(path_); auto path = ctx.paths.checkSourcePath(path_);
if (auto i = caches.fileEval.find(path); i != caches.fileEval.end()) { if (auto i = caches.fileEval.find(path); i != caches.fileEval.end()) {
v = i->second->result; v = i->second->result;
@ -953,7 +953,7 @@ void EvalState::evalFile(const SourcePath & path_, Value & v)
} }
debug("evaluating file '%1%'", resolvedPath); debug("evaluating file '%1%'", resolvedPath);
Expr & e = parseExprFromFile(paths.checkSourcePath(resolvedPath)); Expr & e = parseExprFromFile(ctx.paths.checkSourcePath(resolvedPath));
try { try {
auto dts = ctx.debug auto dts = ctx.debug
@ -2285,7 +2285,7 @@ BackedStringView EvalState::coerceToString(
// slash, as in /foo/${x}. // slash, as in /foo/${x}.
v._path v._path
: copyToStore : copyToStore
? ctx.store->printStorePath(paths.copyPathToStore(context, v.path(), ctx.repair)) ? ctx.store->printStorePath(ctx.paths.copyPathToStore(context, v.path(), ctx.repair))
: std::string(v.path().path.abs()); : std::string(v.path().path.abs());
} }

View file

@ -242,8 +242,8 @@ static Flake getFlake(
}; };
// FIXME: symlink attack // FIXME: symlink attack
auto resolvedFlakeFile = resolveExprPath(state.paths.checkSourcePath(CanonPath(flakeFile))); auto resolvedFlakeFile = resolveExprPath(state.ctx.paths.checkSourcePath(CanonPath(flakeFile)));
Expr & flakeExpr = state.parseExprFromFile(state.paths.checkSourcePath(resolvedFlakeFile)); Expr & flakeExpr = state.parseExprFromFile(state.ctx.paths.checkSourcePath(resolvedFlakeFile));
// Enforce that 'flake.nix' is a direct attrset, not a computation. // Enforce that 'flake.nix' is a direct attrset, not a computation.
if (!(dynamic_cast<ExprAttrs *>(&flakeExpr))) { if (!(dynamic_cast<ExprAttrs *>(&flakeExpr))) {

View file

@ -129,12 +129,12 @@ static SourcePath realisePath(EvalState & state, const PosIdx pos, Value & v, co
auto path = state.coerceToPath(noPos, v, context, "while realising the context of a path"); auto path = state.coerceToPath(noPos, v, context, "while realising the context of a path");
try { try {
StringMap rewrites = state.paths.realiseContext(context); StringMap rewrites = state.ctx.paths.realiseContext(context);
auto realPath = CanonPath(state.paths.toRealPath(rewriteStrings(path.path.abs(), rewrites), context)); auto realPath = CanonPath(state.ctx.paths.toRealPath(rewriteStrings(path.path.abs(), rewrites), context));
return flags.checkForPureEval return flags.checkForPureEval
? state.paths.checkSourcePath(realPath) ? state.ctx.paths.checkSourcePath(realPath)
: realPath; : realPath;
} catch (Error & e) { } catch (Error & e) {
e.addTrace(state.ctx.positions[pos], "while realising the context of path '%s'", path); e.addTrace(state.ctx.positions[pos], "while realising the context of path '%s'", path);
@ -323,7 +323,7 @@ void prim_exec(EvalState & state, const PosIdx pos, Value * * args, Value & v)
false, false).toOwned()); false, false).toOwned());
} }
try { try {
auto _ = state.paths.realiseContext(context); // FIXME: Handle CA derivations auto _ = state.ctx.paths.realiseContext(context); // FIXME: Handle CA derivations
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
e.addTrace(state.ctx.positions[pos], "while realising the context for builtins.exec"); e.addTrace(state.ctx.positions[pos], "while realising the context for builtins.exec");
throw; throw;
@ -1173,7 +1173,7 @@ static void prim_storePath(EvalState & state, const PosIdx pos, Value * * args,
).atPos(pos).debugThrow(); ).atPos(pos).debugThrow();
NixStringContext context; NixStringContext context;
auto path = state.paths.checkSourcePath(state.coerceToPath(pos, *args[0], context, "while evaluating the first argument passed to builtins.storePath")).path; auto path = state.ctx.paths.checkSourcePath(state.coerceToPath(pos, *args[0], context, "while evaluating the first argument passed to builtins.storePath")).path;
/* Resolve symlinks in path, unless path itself is a symlink /* Resolve symlinks in path, unless path itself is a symlink
directly in the store. The latter condition is necessary so directly in the store. The latter condition is necessary so
e.g. nix-push does the right thing. */ e.g. nix-push does the right thing. */
@ -1207,7 +1207,7 @@ static void prim_pathExists(EvalState & state, const PosIdx pos, Value * * args,
try { try {
auto checked = state auto checked = state
.paths .ctx.paths
.checkSourcePath(path) .checkSourcePath(path)
.resolveSymlinks(mustBeDir ? SymlinkResolution::Full : SymlinkResolution::Ancestors); .resolveSymlinks(mustBeDir ? SymlinkResolution::Full : SymlinkResolution::Ancestors);
@ -1306,7 +1306,7 @@ static void prim_findFile(EvalState & state, const PosIdx pos, Value * * args, V
false, false).toOwned(); false, false).toOwned();
try { try {
auto rewrites = state.paths.realiseContext(context); auto rewrites = state.ctx.paths.realiseContext(context);
path = rewriteStrings(path, rewrites); path = rewriteStrings(path, rewrites);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
state.ctx.errors.make<EvalError>( state.ctx.errors.make<EvalError>(
@ -1324,7 +1324,7 @@ static void prim_findFile(EvalState & state, const PosIdx pos, Value * * args, V
auto path = state.forceStringNoCtx(*args[1], pos, "while evaluating the second argument passed to builtins.findFile"); auto path = state.forceStringNoCtx(*args[1], pos, "while evaluating the second argument passed to builtins.findFile");
v.mkPath(state.paths.checkSourcePath(state.paths.findFile(searchPath, path, pos))); v.mkPath(state.ctx.paths.checkSourcePath(state.ctx.paths.findFile(searchPath, path, pos)));
} }
/* Return the cryptographic hash of a file in base-16. */ /* Return the cryptographic hash of a file in base-16. */
@ -1479,7 +1479,7 @@ static void prim_toFile(EvalState & state, const PosIdx pos, Value * * args, Val
used in args[1]. */ used in args[1]. */
/* Add the output of this to the allowed paths. */ /* Add the output of this to the allowed paths. */
state.paths.allowAndSetStorePathString(storePath, v); state.ctx.paths.allowAndSetStorePathString(storePath, v);
} }
static void addPath( static void addPath(
@ -1496,8 +1496,8 @@ static void addPath(
try { try {
// FIXME: handle CA derivation outputs (where path needs to // FIXME: handle CA derivation outputs (where path needs to
// be rewritten to the actual output). // be rewritten to the actual output).
auto rewrites = state.paths.realiseContext(context); auto rewrites = state.ctx.paths.realiseContext(context);
path = state.paths.toRealPath(rewriteStrings(path, rewrites), context); path = state.ctx.paths.toRealPath(rewriteStrings(path, rewrites), context);
StorePathSet refs; StorePathSet refs;
@ -1513,7 +1513,7 @@ static void addPath(
path = evalSettings.pureEval && expectedHash path = evalSettings.pureEval && expectedHash
? path ? path
: state.paths.checkSourcePath(CanonPath(path)).path.abs(); : state.ctx.paths.checkSourcePath(CanonPath(path)).path.abs();
PathFilter filter = filterFun ? ([&](const Path & path) { PathFilter filter = filterFun ? ([&](const Path & path) {
auto st = lstat(path); auto st = lstat(path);
@ -1553,9 +1553,9 @@ static void addPath(
"store path mismatch in (possibly filtered) path added from '%s'", "store path mismatch in (possibly filtered) path added from '%s'",
path path
).atPos(pos).debugThrow(); ).atPos(pos).debugThrow();
state.paths.allowAndSetStorePathString(dstPath, v); state.ctx.paths.allowAndSetStorePathString(dstPath, v);
} else } else
state.paths.allowAndSetStorePathString(*expectedStorePath, v); state.ctx.paths.allowAndSetStorePathString(*expectedStorePath, v);
} catch (Error & e) { } catch (Error & e) {
e.addTrace(state.ctx.positions[pos], "while adding path '%s'", path); e.addTrace(state.ctx.positions[pos], "while adding path '%s'", path);
throw; throw;

View file

@ -58,7 +58,7 @@ static void runFetchClosureWithRewrite(EvalState & state, const PosIdx pos, Stor
}); });
} }
state.paths.mkStorePathString(toPath, v); state.ctx.paths.mkStorePathString(toPath, v);
} }
/** /**
@ -84,7 +84,7 @@ static void runFetchClosureWithContentAddressedPath(EvalState & state, const Pos
}); });
} }
state.paths.mkStorePathString(fromPath, v); state.ctx.paths.mkStorePathString(fromPath, v);
} }
/** /**
@ -107,7 +107,7 @@ static void runFetchClosureWithInputAddressedPath(EvalState & state, const PosId
}); });
} }
state.paths.mkStorePathString(fromPath, v); state.ctx.paths.mkStorePathString(fromPath, v);
} }
typedef std::optional<StorePath> StorePathOrGap; typedef std::optional<StorePath> StorePathOrGap;

View file

@ -51,7 +51,7 @@ static void prim_fetchMercurial(EvalState & state, const PosIdx pos, Value * * a
// FIXME: git externals probably can be used to bypass the URI // FIXME: git externals probably can be used to bypass the URI
// whitelist. Ah well. // whitelist. Ah well.
state.paths.checkURI(url); state.ctx.paths.checkURI(url);
if (evalSettings.pureEval && !rev) if (evalSettings.pureEval && !rev)
throw Error("in pure evaluation mode, 'fetchMercurial' requires a Mercurial revision"); throw Error("in pure evaluation mode, 'fetchMercurial' requires a Mercurial revision");
@ -68,7 +68,7 @@ static void prim_fetchMercurial(EvalState & state, const PosIdx pos, Value * * a
auto [tree, input2] = input.fetch(state.ctx.store); auto [tree, input2] = input.fetch(state.ctx.store);
auto attrs2 = state.ctx.buildBindings(8); auto attrs2 = state.ctx.buildBindings(8);
state.paths.mkStorePathString(tree.storePath, attrs2.alloc(state.ctx.s.outPath)); state.ctx.paths.mkStorePathString(tree.storePath, attrs2.alloc(state.ctx.s.outPath));
if (input2.getRef()) if (input2.getRef())
attrs2.alloc("branch").mkString(*input2.getRef()); attrs2.alloc("branch").mkString(*input2.getRef());
// Backward compatibility: set 'rev' to // Backward compatibility: set 'rev' to
@ -80,7 +80,7 @@ static void prim_fetchMercurial(EvalState & state, const PosIdx pos, Value * * a
attrs2.alloc("revCount").mkInt(*revCount); attrs2.alloc("revCount").mkInt(*revCount);
v.mkAttrs(attrs2); v.mkAttrs(attrs2);
state.paths.allowPath(tree.storePath); state.ctx.paths.allowPath(tree.storePath);
} }
static RegisterPrimOp r_fetchMercurial({ static RegisterPrimOp r_fetchMercurial({

View file

@ -74,7 +74,7 @@ void emitTreeAttrs(
std::string fixURI(std::string uri, EvalState & state, const std::string & defaultScheme = "file") std::string fixURI(std::string uri, EvalState & state, const std::string & defaultScheme = "file")
{ {
state.paths.checkURI(uri); state.ctx.paths.checkURI(uri);
if (uri.find("://") == std::string::npos) { if (uri.find("://") == std::string::npos) {
const auto p = ParsedURL { const auto p = ParsedURL {
.scheme = defaultScheme, .scheme = defaultScheme,
@ -194,7 +194,7 @@ static void fetchTree(
auto [tree, input2] = input.fetch(state.ctx.store); auto [tree, input2] = input.fetch(state.ctx.store);
state.paths.allowPath(tree.storePath); state.ctx.paths.allowPath(tree.storePath);
emitTreeAttrs(state, tree, input2, v, params.emptyRevFallback, false); emitTreeAttrs(state, tree, input2, v, params.emptyRevFallback, false);
} }
@ -244,7 +244,7 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
if (who == "fetchTarball") if (who == "fetchTarball")
url = evalSettings.resolvePseudoUrl(*url); url = evalSettings.resolvePseudoUrl(*url);
state.paths.checkURI(*url); state.ctx.paths.checkURI(*url);
if (name == "") if (name == "")
name = baseNameOf(*url); name = baseNameOf(*url);
@ -263,7 +263,7 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
}); });
if (state.ctx.store->isValidPath(expectedPath)) { if (state.ctx.store->isValidPath(expectedPath)) {
state.paths.allowAndSetStorePathString(expectedPath, v); state.ctx.paths.allowAndSetStorePathString(expectedPath, v);
return; return;
} }
} }
@ -290,7 +290,7 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
} }
} }
state.paths.allowAndSetStorePathString(storePath, v); state.ctx.paths.allowAndSetStorePathString(storePath, v);
} }
void prim_fetchurl(EvalState & state, const PosIdx pos, Value * * args, Value & v) void prim_fetchurl(EvalState & state, const PosIdx pos, Value * * args, Value & v)

View file

@ -37,7 +37,7 @@ json printValueAsJSON(EvalState & state, bool strict,
case nPath: case nPath:
if (copyToStore) if (copyToStore)
out = state.ctx.store->printStorePath( out = state.ctx.store->printStorePath(
state.paths.copyPathToStore(context, v.path(), state.ctx.repair)); state.ctx.paths.copyPathToStore(context, v.path(), state.ctx.repair));
else else
out = v.path().path.abs(); out = v.path().path.abs();
break; break;