From c698ec1f220f9be4c4184532f0a6c2a9370f3839 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Sun, 1 Jan 2017 18:42:06 +0200 Subject: [PATCH 1/3] nix-shell: Fix 'nix-shell -i' The 'args' variable here is shadowing one in the outer scope and its contents end up unused. This causes any '#! nix-shell' lines to effectively be ignored. The intention here was to clear the args vector, as far as I can tell (and it seems to work). --- src/nix-build/nix-build.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 08c679357..4d86f394b 100755 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -117,7 +117,7 @@ int main(int argc, char ** argv) inShebang = true; for (int i = 2; i < argc - 1; ++i) savedArgs.push_back(argv[i]); - std::vector args; + args.clear(); for (auto line : lines) { line = chomp(line); std::smatch match; From 018f884ffd834ee339e9b4182b4537a3cd80ee28 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Tue, 3 Jan 2017 09:59:09 +0200 Subject: [PATCH 2/3] nix-shell: Don't act interactive in shebangs I had observed that 'bash --rcfile' would do nothing in a non-interactive context and cause nothing to be executed if a script using nix-shell shebangs were run in a non-interactive context. --- src/nix-build/nix-build.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 4d86f394b..ef959fa00 100755 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -276,6 +276,7 @@ int main(int argc, char ** argv) if (n >= args.size()) { throw UsageError(format("%1% requires an argument") % arg); } + interactive = false; auto interpreter = args[n]; auto execArgs = ""; From b501bea25fac9c43dfd0d197e16836facd2b63a5 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Sun, 1 Jan 2017 18:45:51 +0200 Subject: [PATCH 3/3] tests: Add simple tests for nix-shell nix-shell -A, -p and -i are lightly tested. --- tests/local.mk | 2 +- tests/nix-shell.sh | 21 +++++++++++++++++++ tests/shell.nix | 46 ++++++++++++++++++++++++++++++++++++++++++ tests/shell.shebang.sh | 4 ++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/nix-shell.sh create mode 100644 tests/shell.nix create mode 100755 tests/shell.shebang.sh diff --git a/tests/local.mk b/tests/local.mk index 2ca52144b..b3ce39cda 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -11,7 +11,7 @@ nix_tests = \ multiple-outputs.sh import-derivation.sh fetchurl.sh optimise-store.sh \ binary-cache.sh nix-profile.sh repair.sh dump-db.sh case-hack.sh \ check-reqs.sh pass-as-file.sh tarball.sh restricted.sh \ - placeholders.sh + placeholders.sh nix-shell.sh # parallel.sh install-tests += $(foreach x, $(nix_tests), tests/$(x)) diff --git a/tests/nix-shell.sh b/tests/nix-shell.sh new file mode 100644 index 000000000..12a0ecd03 --- /dev/null +++ b/tests/nix-shell.sh @@ -0,0 +1,21 @@ +source common.sh + +clearStore + +# Test nix-shell -A +export IMPURE_VAR=foo +output=$(nix-shell --pure shell.nix -A shellDrv --run \ + 'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') + +[ "$output" = " - foo - bar" ] + +# Test nix-shell -p +output=$(NIX_PATH=nixpkgs=shell.nix nix-shell --pure -p foo bar --run 'echo "$(foo) $(bar)"') +[ "$output" = "foo bar" ] + +# Test nix-shell shebang mode +sed -e "s|@ENV_PROG@|$(type -p env)|" shell.shebang.sh > $TEST_ROOT/shell.shebang.sh +chmod a+rx $TEST_ROOT/shell.shebang.sh + +output=$($TEST_ROOT/shell.shebang.sh) +[ "$output" = "foo bar" ] diff --git a/tests/shell.nix b/tests/shell.nix new file mode 100644 index 000000000..ed4d6fbaa --- /dev/null +++ b/tests/shell.nix @@ -0,0 +1,46 @@ +{ }: + +with import ./config.nix; + +rec { + setupSh = builtins.toFile "setup" '' + export VAR_FROM_STDENV_SETUP=foo + for pkg in $buildInputs; do + export PATH=$PATH:$pkg/bin + done + ''; + + stdenv = mkDerivation { + name = "stdenv"; + buildCommand = '' + mkdir -p $out + ln -s ${setupSh} $out/setup + ''; + }; + + shellDrv = mkDerivation { + name = "shellDrv"; + builder = "/does/not/exist"; + VAR_FROM_NIX = "bar"; + inherit stdenv; + }; + + # Used by nix-shell -p + runCommand = name: args: buildCommand: mkDerivation (args // { + inherit name buildCommand stdenv; + }); + + foo = runCommand "foo" {} '' + mkdir -p $out/bin + echo 'echo foo' > $out/bin/foo + chmod a+rx $out/bin/foo + ''; + + bar = runCommand "bar" {} '' + mkdir -p $out/bin + echo 'echo bar' > $out/bin/bar + chmod a+rx $out/bin/bar + ''; + + bash = shell; +} diff --git a/tests/shell.shebang.sh b/tests/shell.shebang.sh new file mode 100755 index 000000000..544e28217 --- /dev/null +++ b/tests/shell.shebang.sh @@ -0,0 +1,4 @@ +#! @ENV_PROG@ nix-shell +#! nix-shell -I nixpkgs=shell.nix --option use-binary-caches false +#! nix-shell --pure -i bash -p foo bar +echo "$(foo) $(bar)"