From fcdc60ed227ba28f0d0d160d17df27fe179138b0 Mon Sep 17 00:00:00 2001 From: regnat Date: Thu, 27 Jan 2022 15:32:14 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20require=20`NIX=5FPATH`=20entrie?= =?UTF-8?q?s=20to=20be=20valid=20paths?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s totally valid to have entries in `NIX_PATH` that aren’t valid paths (they can even be arbitrary urls or `channel:`). Fix #5998 and #5980 --- src/libexpr/primops.cc | 20 ++++++++++++++------ tests/local.mk | 1 + tests/nix_path.sh | 11 +++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 tests/nix_path.sh diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 9d36f72a8..acee71d19 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -92,8 +92,6 @@ StringMap EvalState::realiseContext(const PathSet & context) } 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 bool checkForPureEval = true; }; @@ -105,9 +103,7 @@ static Path realisePath(EvalState & state, const Pos & pos, Value & v, const Rea auto path = [&]() { try { - return flags.requireAbsolutePath - ? state.coerceToPath(pos, v, context) - : state.coerceToString(pos, v, context, false, false); + return state.coerceToPath(pos, v, context); } catch (Error & e) { e.addTrace(pos, "while realising the context of a path"); throw; @@ -1489,7 +1485,19 @@ static void prim_findFile(EvalState & state, const Pos & pos, Value * * args, Va 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); } diff --git a/tests/local.mk b/tests/local.mk index 47d2862a4..2ec9baa12 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -63,6 +63,7 @@ nix_tests = \ eval-store.sh \ readfile-context.sh \ store-ping.sh \ + nix_path.sh \ why-depends.sh # parallel.sh diff --git a/tests/nix_path.sh b/tests/nix_path.sh new file mode 100644 index 000000000..d3657abf0 --- /dev/null +++ b/tests/nix_path.sh @@ -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 '' --restrict-eval +nix-instantiate --eval -E '' --restrict-eval + +# Should ideally also test this, but there’s no pure way to do it, so just trust me that it works +# nix-instantiate --eval -E '' -I nixpkgs=channel:nixos-unstable --restrict-eval