From e1131b59279f7cf9f9bea93b5355608d78097f65 Mon Sep 17 00:00:00 2001 From: Rodney Lorrimar Date: Sun, 4 Feb 2024 12:02:06 +0800 Subject: [PATCH] print-dev-env: Avoid using unbound shellHook variable Some tools which consume the "nix print-dev-env" rc script (such as "nix-direnv") are sensitive to the use of unbound variables. They use "set -u". The "nix print-dev-env" rc script initially unsets "shellHook", then loads variables from the derivation, and then evaluates "shellHook". However, most derivations don't have a "shellHook" attribute. So users get the error "shellHook: unbound variable". This can be demonstrated with the command: nix print-dev-env nixpkgs#hello | bash -u This commit changes the rc script to provide an empty fallback value for the "shellHook" variable. Closes: #7951 #8253 --- src/nix/develop.cc | 2 +- tests/functional/nix-shell.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nix/develop.cc b/src/nix/develop.cc index 1f2891378..403178a5d 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -354,7 +354,7 @@ struct Common : InstallableCommand, MixProfile for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"}) out << fmt("export %s=\"$NIX_BUILD_TOP\"\n", i); - out << "eval \"$shellHook\"\n"; + out << "eval \"${shellHook:-}\"\n"; auto script = out.str(); diff --git a/tests/functional/nix-shell.sh b/tests/functional/nix-shell.sh index 13403fadb..04c83138e 100644 --- a/tests/functional/nix-shell.sh +++ b/tests/functional/nix-shell.sh @@ -118,10 +118,10 @@ diff $TEST_ROOT/dev-env{,2}.json # Ensure `nix print-dev-env --json` contains variable assignments. [[ $(jq -r .variables.arr1.value[2] $TEST_ROOT/dev-env.json) = '3 4' ]] -# Run tests involving `source <(nix print-dev-inv)` in subshells to avoid modifying the current +# Run tests involving `source <(nix print-dev-env)` in subshells to avoid modifying the current # environment. -set +u # FIXME: Make print-dev-env `set -u` compliant (issue #7951) +set -u # Ensure `source <(nix print-dev-env)` modifies the environment. (