From 4a920b46da219ca7de4c9fe00038e82dab1b22f4 Mon Sep 17 00:00:00 2001 From: Valentin Gagarin Date: Wed, 4 Sep 2024 21:26:18 +0200 Subject: [PATCH 1/2] only set default values for `nix-path` if nothing else is set TODO: this doesn't work yet. we currently have no way of checking if a config value was set or not. this is the expected behavior for default values: new values should override them, not prepend. the change fixes a logic bug introduced when fixing the previously confused override mechanism in e0620213146b1a581533302a2fab54af21d49685. --- src/libexpr/eval-gc.cc | 2 +- src/libexpr/eval.cc | 1 + tests/functional/nix_path.sh | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libexpr/eval-gc.cc b/src/libexpr/eval-gc.cc index 07ce05a2c73..de0b53865f1 100644 --- a/src/libexpr/eval-gc.cc +++ b/src/libexpr/eval-gc.cc @@ -101,7 +101,7 @@ void initGC() #endif // NIX_PATH must override the regular setting - // See the comment in applyConfig + // See the XXX comment in `applyConfig()` if (auto nixPathEnv = getEnv("NIX_PATH")) { globalConfig.set("nix-path", concatStringsSep(" ", EvalSettings::parseNixPath(nixPathEnv.value()))); } diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 2420f15c19a..345acb6d25d 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -341,6 +341,7 @@ EvalState::EvalState( for (auto & i : settings.nixPath.get()) { lookupPath.elements.emplace_back(LookupPath::Elem::parse(i)); } + // TODO: only set if nothing else is if (!settings.restrictEval) { for (auto & i : EvalSettings::getDefaultNixPath()) { lookupPath.elements.emplace_back(LookupPath::Elem::parse(i)); diff --git a/tests/functional/nix_path.sh b/tests/functional/nix_path.sh index 90cba1f0c9c..5557ab7d550 100755 --- a/tests/functional/nix_path.sh +++ b/tests/functional/nix_path.sh @@ -42,6 +42,12 @@ done # finding something that's not in any of the default paths fails ( ! $(nix-instantiate --find-file test) ) +# setting anything overrides the default paths +# this ensures we can force an empty search path +[[ $(NIX_PATH= nix-instantiate --eval -E 'with builtins; length nixPath') = 0 ]] +[[ $(nix-instantiate --nix-path "" --eval -E 'with builtins; length nixPath') = 0 ]] +[[ $(nix-instantiate -I "" --eval -E 'with builtins; length nixPath') = 1 ]] + echo "nix-path = test=$TEST_ROOT/from-nix-path-file" >> "$test_nix_conf" # Use nix.conf in absence of NIX_PATH From 1f032a98c537f8e2182cc8281ab0f34738b8e505 Mon Sep 17 00:00:00 2001 From: Valentin Gagarin Date: Mon, 9 Sep 2024 16:09:37 +0200 Subject: [PATCH 2/2] WIP: only test in derivations --- tests/functional/nix_path.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/functional/nix_path.sh b/tests/functional/nix_path.sh index 5557ab7d550..51d22967f49 100755 --- a/tests/functional/nix_path.sh +++ b/tests/functional/nix_path.sh @@ -42,11 +42,18 @@ done # finding something that's not in any of the default paths fails ( ! $(nix-instantiate --find-file test) ) -# setting anything overrides the default paths -# this ensures we can force an empty search path -[[ $(NIX_PATH= nix-instantiate --eval -E 'with builtins; length nixPath') = 0 ]] -[[ $(nix-instantiate --nix-path "" --eval -E 'with builtins; length nixPath') = 0 ]] -[[ $(nix-instantiate -I "" --eval -E 'with builtins; length nixPath') = 1 ]] +# XXX: we can't manipulate $NIX_STATE_DIR contents on NixOS +# TODO: port to NixOS somehow; ideally we'd run the test suite in a non-NixOS VM +if [[ ! isTestOnNixOS ]]; then + mkdir -p $NIX_STATE_DIR/profiles/per-user/root/channels/nixpkgs + # check that the default values are set + [[ $(nix-instantiate --eval -E 'with builtins; length nixPath') = 2 ]] + # setting anything overrides the default paths + # this ensures we can force an empty search path + [[ $(NIX_PATH= nix-instantiate --eval -E 'with builtins; length nixPath') = 0 ]] + [[ $(nix-instantiate --nix-path "" --eval -E 'with builtins; length nixPath') = 0 ]] + [[ $(nix-instantiate -I "" --eval -E 'with builtins; length nixPath') = 1 ]] +fi echo "nix-path = test=$TEST_ROOT/from-nix-path-file" >> "$test_nix_conf"