diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index e2325c91fd4..10f825ebdf0 100755 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -270,7 +270,12 @@ static void main_nix_build(int argc, char * * argv) if (packages) { std::ostringstream joined; - joined << "{...}@args: with import args; (pkgs.runCommandCC or pkgs.runCommand) \"shell\" { buildInputs = [ "; + joined << "{...}@args: " + << "let nixpkgs = import ; " + << "nixpkgsArgs = builtins.intersectAttrs (builtins.functionArgs nixpkgs) args; " + << "in " + << "with nixpkgs nixpkgsArgs; " + << "(pkgs.runCommandCC or pkgs.runCommand) \"shell\" { buildInputs = [ "; for (const auto & i : left) joined << '(' << i << ") "; joined << "]; } \"\""; diff --git a/tests/nix-shell.sh b/tests/nix-shell.sh index 3241d7a0f72..d6457479bbf 100644 --- a/tests/nix-shell.sh +++ b/tests/nix-shell.sh @@ -52,6 +52,11 @@ output=$(NIX_PATH=nixpkgs="$shellDotNix" nix-shell --pure -p foo bar --run 'echo output=$(NIX_PATH=nixpkgs="$shellDotNix" nix-shell --pure -p foo --argstr fooContents baz --run 'echo "$(foo)"') [ "$output" = "baz" ] +# Test nix-shell -p with older versions of Nixpkgs that do not expect +# the (new) isNixShell attribute. +output=$(NIX_PATH=nixpkgs=shell-empty-attrs-args.nix nix-shell --pure -p foo --run 'echo "$(foo)"') +[ "$output" = "zes" ] + # 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 diff --git a/tests/shell-empty-attrs-args.nix b/tests/shell-empty-attrs-args.nix new file mode 100644 index 00000000000..061589af18c --- /dev/null +++ b/tests/shell-empty-attrs-args.nix @@ -0,0 +1 @@ +{ }: import ./shell.nix { fooContents = "zes"; }