Skip to content

Commit

Permalink
Be mindful about the default arguments passed to nixpkgs in nix-shell
Browse files Browse the repository at this point in the history
Starting with 6eeb6f9 Nix started injecting `inNixShell`
unconditonally into all nix-shell calls. This caused to a minor breakage
with old Nix expressions that had a thin wrapper around nixpkgs. While
we can fix those projects we should try to be backwards compatible in
this regard.

The error encountered in those cases looked like this:
    error: anonymous function at /home/andi/dev/nixos/nix/tests/shell-empty-attrs-args.nix:1:1 called with unexpected argument 'inNixShell'
           at «string»:1:18:

                1| {...}@Args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) "shell" { buildInputs = [ (foo) (run) (echo "$(foo)") ]; } ""
                 |                  ^

           … while evaluating anonymous lambda

           at «string»:1:1:

                1| {...}@Args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) "shell" { buildInputs = [ (foo) (run) (echo "$(foo)") ]; } ""
                 | ^

           … from call site
  • Loading branch information
andir committed Nov 29, 2021
1 parent 55275fc commit e1b9bb7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ static void main_nix_build(int argc, char * * argv)

if (packages) {
std::ostringstream joined;
joined << "{...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) \"shell\" { buildInputs = [ ";
joined << "{...}@args: "
<< "let nixpkgs = import <nixpkgs>; "
<< "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 << "]; } \"\"";
Expand Down
5 changes: 5 additions & 0 deletions tests/nix-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/shell-empty-attrs-args.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }: import ./shell.nix { fooContents = "zes"; }

0 comments on commit e1b9bb7

Please sign in to comment.