Why can I not execute a new version of nix with `nix shell`?

I really tried my best to find out what’s going wrong here, but I just don’t get it.

Basically, I wanted to install nix 2.16.1 to try it out, but it doesn’t seem to work:

$ nix shell nixpkgs#nixVersion.2_16
$ nix --version
nix (Nix) 2.15.1
$ which nix
$ nix run nixpkgs#xonsh -- -c 'print("\n".join($(echo $PATH).split(":")))'

Haha, ok, after agonizing for half an hour, now I got it. I implemented a workaround for this issue a while ago, which is to source nix in my home .zshrc:

[ -e "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" ] && source "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"
[ -e "$HOME/.nix-profile/etc/profile.d/nix.sh" ] && source "$HOME/.nix-profile/etc/profile.d/nix.sh"

nix shell, as it starts a new instance of zsh, causes this to be sourced again, but it is added to $PATH after nix shell’s additions, and so those take precedence.

My fix was check whether nix is available first:

[[ ! $(command -v nix) && -e "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" ]] && source "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"                                                
[[ ! $(command -v nix) && -e "$HOME/.nix-profile/etc/profile.d/nix.sh" ]] && source "$HOME/.nix-profile/etc/profile.d/nix.sh"

That fixed my $PATH completely.

I’ll leave this here for posterity.

1 Like

Maybe mark the post as solved too :slight_smile:

1 Like