From 526bdbda3cfa396808a12e6598df50dacb04e6dd Mon Sep 17 00:00:00 2001 From: lbodor Date: Sun, 12 Mar 2023 22:40:47 +1100 Subject: [PATCH 1/3] print-dev-env: stop inadvertently adding . to PATH --- src/nix/develop.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix/develop.cc b/src/nix/develop.cc index 9d07a7a85..0ee533e85 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -313,7 +313,7 @@ struct Common : InstallableCommand, MixProfile buildEnvironment.toBash(out, ignoreVars); for (auto & var : savedVars) - out << fmt("%s=\"$%s:$nix_saved_%s\"\n", var, var, var); + out << fmt("%s=\"$%s${nix_saved_%s:+:$nix_saved_%s}\"\n", var, var, var, var); out << "export NIX_BUILD_TOP=\"$(mktemp -d -t nix-shell.XXXXXX)\"\n"; for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"}) From e210de47998d4052e525b2d285fcce6114ec34e5 Mon Sep 17 00:00:00 2001 From: lbodor Date: Mon, 13 Mar 2023 17:50:36 +1100 Subject: [PATCH 2/3] print-dev-env: test the case when PATH is empty --- tests/nix-shell.sh | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/tests/nix-shell.sh b/tests/nix-shell.sh index a6e32ed54..8cb4fd385 100644 --- a/tests/nix-shell.sh +++ b/tests/nix-shell.sh @@ -94,16 +94,43 @@ echo foo | nix develop -f "$shellDotNix" shellDrv -c cat | grepQuiet foo nix develop -f "$shellDotNix" shellDrv -c echo foo |& grepQuiet foo # Test 'nix print-dev-env'. -[[ $(nix print-dev-env -f "$shellDotNix" shellDrv --json | jq -r .variables.arr1.value[2]) = '3 4' ]] + +scratch=$(mktemp -d -t tmp.XXXXXXXXXX) +function finish { + rm -rf "$scratch" +} +trap finish EXIT + +nix print-dev-env -f "$shellDotNix" shellDrv > "$scratch/dev-env.sh" +nix print-dev-env -f "$shellDotNix" shellDrv --json > "$scratch/dev-env.json" + +# Ensure `nix print-dev-env --json` contains variable assignments. +[[ $(jq -r .variables.arr1.value[2] "$scratch/dev-env.json") = '3 4' ]] + +# Run tests involving `source <(nix print-dev-inv)` in subshells to avoid modifying the current +# environment. set +u # FIXME: Make print-dev-env `set -u` compliant (issue #7951) -source <(nix print-dev-env -f "$shellDotNix" shellDrv) -[[ -n $stdenv ]] -[[ ${arr1[2]} = "3 4" ]] -[[ ${arr2[1]} = $'\n' ]] -[[ ${arr2[2]} = $'x\ny' ]] -[[ $(fun) = blabla ]] +# Ensure `source <(nix print-dev-env)` modifies the environment. +( + path=$PATH + source "$scratch/dev-env.sh" + [[ -n $stdenv ]] + [[ ${arr1[2]} = "3 4" ]] + [[ ${arr2[1]} = $'\n' ]] + [[ ${arr2[2]} = $'x\ny' ]] + [[ $(fun) = blabla ]] + [[ $PATH = $(jq -r .variables.PATH.value "$scratch/dev-env.json"):$path ]] +) + +# Ensure `source <(nix print-dev-env)` handles the case when PATH is empty. +( + path=$PATH + PATH= + source "$scratch/dev-env.sh" + [[ $PATH = $(PATH=$path jq -r .variables.PATH.value "$scratch/dev-env.json") ]] +) # Test nix-shell with ellipsis and no `inNixShell` argument (for backwards compat with old nixpkgs) cat >$TEST_ROOT/shell-ellipsis.nix < Date: Tue, 14 Mar 2023 01:48:12 +1100 Subject: [PATCH 3/3] Use $TEST_ROOT --- tests/nix-shell.sh | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/tests/nix-shell.sh b/tests/nix-shell.sh index 8cb4fd385..044b96d54 100644 --- a/tests/nix-shell.sh +++ b/tests/nix-shell.sh @@ -95,17 +95,11 @@ nix develop -f "$shellDotNix" shellDrv -c echo foo |& grepQuiet foo # Test 'nix print-dev-env'. -scratch=$(mktemp -d -t tmp.XXXXXXXXXX) -function finish { - rm -rf "$scratch" -} -trap finish EXIT - -nix print-dev-env -f "$shellDotNix" shellDrv > "$scratch/dev-env.sh" -nix print-dev-env -f "$shellDotNix" shellDrv --json > "$scratch/dev-env.json" +nix print-dev-env -f "$shellDotNix" shellDrv > $TEST_ROOT/dev-env.sh +nix print-dev-env -f "$shellDotNix" shellDrv --json > $TEST_ROOT/dev-env.json # Ensure `nix print-dev-env --json` contains variable assignments. -[[ $(jq -r .variables.arr1.value[2] "$scratch/dev-env.json") = '3 4' ]] +[[ $(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 # environment. @@ -115,21 +109,21 @@ set +u # FIXME: Make print-dev-env `set -u` compliant (issue #7951) # Ensure `source <(nix print-dev-env)` modifies the environment. ( path=$PATH - source "$scratch/dev-env.sh" + source $TEST_ROOT/dev-env.sh [[ -n $stdenv ]] [[ ${arr1[2]} = "3 4" ]] [[ ${arr2[1]} = $'\n' ]] [[ ${arr2[2]} = $'x\ny' ]] [[ $(fun) = blabla ]] - [[ $PATH = $(jq -r .variables.PATH.value "$scratch/dev-env.json"):$path ]] + [[ $PATH = $(jq -r .variables.PATH.value $TEST_ROOT/dev-env.json):$path ]] ) # Ensure `source <(nix print-dev-env)` handles the case when PATH is empty. ( path=$PATH PATH= - source "$scratch/dev-env.sh" - [[ $PATH = $(PATH=$path jq -r .variables.PATH.value "$scratch/dev-env.json") ]] + source $TEST_ROOT/dev-env.sh + [[ $PATH = $(PATH=$path jq -r .variables.PATH.value $TEST_ROOT/dev-env.json) ]] ) # Test nix-shell with ellipsis and no `inNixShell` argument (for backwards compat with old nixpkgs)