Nix-shell not adding packages to path

I recently updated my nix to nix (Nix) 2.21.2 and noticing something weird (I use zsh as default shell)

> nix-shell -p hello --run 'hello'
Hello, world!

But if I don’t use run

> nix-shell -p hello
> hello
zsh: command not found: hello

Somewhat unusual, now the following:

> nix-shell -p hello --run 'zsh'
> hello
Hello, world!

Seems like without providing --run, it doesn’t actually modify the PATH. I still need to run exit to close it. Is there a setting or env var this behaviour is depending on?

nix-shell without --run is launching a bash shell. Something in your bash shell initialization files (bashrc and friends) is clobbering your PATH.

2 Likes

Running nix-shell with explicit bash works fine:

> nix-shell -p hello --run 'bash'
> hello
Hello, world!

At the bottom of my .bashrc I have:

if test -t 1; then
  exec zsh
fi

nonetheless if doing --run 'bash' works, shouldn’t the standard one work fine too?

What do you get when you add --pure?

--run uses a non-interactive shell, so it won’t be sourcing your bashrc. See man nix-shell.

Interesting --pure launches bash and it works as expected:

> nix-shell -p hello --pure

[nix-shell:~/]$ hello
Hello, world!

Still confused as to what could be causing it to behave differenly between having --run and without it. Maybe some extra flags are being passed to bash!

ah in that case you’re spot on. Moving .bashrc file fixed the issue. Is it --command that runs an interactive shell?

Yep! That’s right. Again, see man nix-shell :slight_smile:

1 Like

For anyone else stumbling upon this, it ended up being the following in .bashrc:

if test -t 1; then
  exec zsh
fi

I did not manually add this. It must have been added automatically by one of the zsh installers

1 Like