Weird inconsistency for `nix-shell --run`

Hi, I have found a strange behaviour of nix-shell when I try to make it run some simple Shell commands inside a spawned nix-shell. It works as expected on one machine, but not on another. It seems like the argument I passed for --run somehow got interpreted as Nix instead of Shell script. Here are the console output for running the command on the two machines: (I’m using -p python3 simply because I don’t need to create a default.nix to call the Shell command, and Python 3 is a pretty stable package).

Machine A (work as expected):

harry@machine-a ~> nix-shell --version
nix-shell (Nix) 2.3.10
harry@machine-a ~> nix-shell -p python3 --run "echo Hello" --pure
Hello

Machine B (not working):

harry@machine-b ~> nix-shell --version
nix-shell (Nix) 2.3.10
harry@machine-b ~> nix-shell -p python3 --run "echo Hello" --pure                                       exited nix-shell 
error: undefined variable 'Hello' at (string):1:104
(use '--show-trace' to show detailed location information)

Both are using fish 3.1.2 too. But since I am still a very early learner of Nix and NixOS, I am worried it might be me not fully understanding how nix-shell works yet. I would be grateful to see some ideas from anyone. Thanks!

I’ll be a little impressed if anyone knows what this is off the top of their head :slight_smile:

Some initial debugging thoughts/questions:

  • What’s the traceback for --show-trace on machine b?
  • Does it do this without the flag?
  • What does file $(which nix-shell) say on both?

Hi @abathur, thanks for checking this out. After a bit more investigation, I’m inclined to think there’s some breaking change in a recent nixpkgs commit, OR there is an error in the configuration.nix for Machine B, but I haven’t spent time to go further at this moment. My answers to your questions:

This gets a bit interesting. First of all, the NixOS versions running on the two machines are a bit different:

  • Machine A: 20.09.3218.6bebc91e288
  • Machine B: 20.09.3301.42809feaa9f (should be more recent)

On Machine B where this error happens, the traceback message is as follows:

harry@machine-b ~> nix-shell -p python3 --run "echo Hello" --show-trace
error: while evaluating the attribute 'buildInputs' of the derivation 'shell' at /nix/store/r75c3sk3nan2bhja4ixwi4hfknnqzkr9-nixos/pkgs/build-support/trivial-builders.nix:7:7:
while evaluating 'chooseDevOutputs' at /nix/store/r75c3sk3nan2bhja4ixwi4hfknnqzkr9-nixos/lib/attrsets.nix:475:22, called from undefined position:
undefined variable 'Hello' at (string):1:104

I also have access to some other machines that are built with the same configuration.nix and have the same NixOS version as Machine B, and they all show this weird behaviour and the same traceback message.

Yes, and the traceback message is the same regardless of --pure. And I’m very sure that echo Hello can run properly if I enter an interactive Nix shell to run echo Hello (i.e. without --run or --command).

An even more interesting thing is that if I do --run instead of --command, and use the exact example shown on the man nix-shell page like below, I get this kind of error message on Machine B (and no error on Machine A):

harry@machine-b ~ [1]> nix-shell -p python3 --run "echo Hello; return" --show-trace                                                                                                                                      ERROR 
error: syntax error, unexpected ';', expecting ')', at (string):1:109

They should both be using the same version, unless the difference between the two commit versions of nixpkgs on the two machines actually matter. Note that I removed $ due to fish's restriction.

Machine A:

harry@machine-a ~> file (which nix-shell)
/run/current-system/sw/bin/nix-shell: symbolic link to nix/store/3nfsyqncwb0cqq50nahrzx7w5kgrh1jl-nix-2.3.10/bin/nix-shell

Machine B:

harry@machine-b ~> file (which nix-shell)
/run/current-system/sw/bin/nix-shell: symbolic link to /nix/store/pzxsa9j0yzyxk5xx5ldipasyadwcrmf6-nix-2.3.10/bin/nix-shell
1 Like

Ah, I didn’t realize this was nixOS, so I was focused a bit more on potential environment misconfig stuff.

I guess a good next step is invoking the commands with explicitly-specified nixpkgs commits to help clarify whether the nixpkgs commit or something about the systems is more likely to be significant.

https://channels.nix.gsc.io/nixos-20.09/history indicates that the full refs corresponding to your commits are:

  • machine a: 6bebc91e2882e0b4917567d0c025bbfafb01be69
  • machine b: 42809feaa9f7474f5566a5c6e8e317e15e39160e

Try running each of the following on both systems:

nix-shell -p python3 --run "echo Hello" -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/6bebc91e2882e0b4917567d0c025bbfafb01be69.tar.gz
nix-shell -p python3 --run "echo Hello" -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/42809feaa9f7474f5566a5c6e8e317e15e39160e.tar.gz
1 Like
error: undefined variable 'Hello'

I am seeing a very similar error, namely undefined variable in bash being interpreted as nix, trying to port back some test suite improvements to 2.3.

edit: My issue end up being that I needed nix-shell: don't check for "nix-shell" in shebang script name · NixOS/nix@a70706b · GitHub

Hosted by Flying Circus.