Merge pull request #6001 from NixOS/fix-nix-path

Don’t require `NIX_PATH` entries to be valid paths
This commit is contained in:
Eelco Dolstra 2022-01-27 17:04:02 +01:00 committed by GitHub
commit 558c4ee3e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 6 deletions

View file

@ -92,8 +92,6 @@ StringMap EvalState::realiseContext(const PathSet & context)
} }
struct RealisePathFlags { struct RealisePathFlags {
// Whether to check whether the path is a valid absolute path
bool requireAbsolutePath = true;
// Whether to check that the path is allowed in pure eval mode // Whether to check that the path is allowed in pure eval mode
bool checkForPureEval = true; bool checkForPureEval = true;
}; };
@ -105,9 +103,7 @@ static Path realisePath(EvalState & state, const Pos & pos, Value & v, const Rea
auto path = [&]() auto path = [&]()
{ {
try { try {
return flags.requireAbsolutePath return state.coerceToPath(pos, v, context);
? state.coerceToPath(pos, v, context)
: state.coerceToString(pos, v, context, false, false);
} catch (Error & e) { } catch (Error & e) {
e.addTrace(pos, "while realising the context of a path"); e.addTrace(pos, "while realising the context of a path");
throw; throw;
@ -1489,7 +1485,19 @@ static void prim_findFile(EvalState & state, const Pos & pos, Value * * args, Va
pos pos
); );
auto path = realisePath(state, pos, *i->value, { .requireAbsolutePath = false }); PathSet context;
string path = state.coerceToString(pos, *i->value, context, false, false);
try {
auto rewrites = state.realiseContext(context);
path = rewriteStrings(path, rewrites);
} catch (InvalidPathError & e) {
throw EvalError({
.msg = hintfmt("cannot find '%1%', since path '%2%' is not valid", path, e.path),
.errPos = pos
});
}
searchPath.emplace_back(prefix, path); searchPath.emplace_back(prefix, path);
} }

View file

@ -63,6 +63,7 @@ nix_tests = \
eval-store.sh \ eval-store.sh \
readfile-context.sh \ readfile-context.sh \
store-ping.sh \ store-ping.sh \
nix_path.sh \
why-depends.sh why-depends.sh
# parallel.sh # parallel.sh

11
tests/nix_path.sh Normal file
View file

@ -0,0 +1,11 @@
# Regression for https://github.com/NixOS/nix/issues/5998 and https://github.com/NixOS/nix/issues/5980
source common.sh
export NIX_PATH=non-existent=/non-existent/but-unused-anyways:by-absolute-path=$PWD:by-relative-path=.
nix-instantiate --eval -E '<by-absolute-path/simple.nix>' --restrict-eval
nix-instantiate --eval -E '<by-relative-path/simple.nix>' --restrict-eval
# Should ideally also test this, but theres no pure way to do it, so just trust me that it works
# nix-instantiate --eval -E '<nixpkgs>' -I nixpkgs=channel:nixos-unstable --restrict-eval