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?
pxc
April 30, 2024, 8:16pm
2
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?
pxc
April 30, 2024, 8:25pm
4
What do you get when you add --pure
?
pxc
April 30, 2024, 8:28pm
5
--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?
pxc
April 30, 2024, 8:31pm
8
Yep! That’s right. Again, see man nix-shell
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