DeterminateSystems / nix-installer

Install Nix and flakes with the fast and reliable Determinate Nix Installer, with over 7 million installs.
https://determinate.systems
GNU Lesser General Public License v2.1
2.21k stars 57 forks source link

`nix shell` puts `~/.nix-profile/bin` first in PATH #782

Open wch opened 10 months ago

wch commented 10 months ago

On my Mac where I used the Determinate Systems installer, when I run nix shell, it puts ~/.nix-profile/bin as the first thing in the PATH, but this means that if there is a copy of a program installed there, it will be used instead of the program I want to use with nix shell.

For example, suppose you already have hello installed on your system.

❯ hello
Hello, world!

I have a hello flake at https://github.com/wch/nix-hello-demo that prints something slightly different:

❯ nix run github:wch/nix-hello-demo
Hello, World! This is a demo of a Nix flake.

However, when I use nix shell, and the run hello, it runs the regular version instead of my customized one.

# Mac with Determinate Systems installer
❯ nix shell github:wch/nix-hello-demo
❯ hello
Hello, world!

Note that this happens on my Mac, but not on my NixOS computer (where I didn't use the Determinate Systems installer). The behavior on NixOS is what I would expect on the Mac.

# NixOS (without Determinate Systems installer)
❯ nix shell github:wch/nix-hello-demo
❯ hello
Hello, World! This is a demo of a Nix flake.

On the Mac, in the nix shell the PATH is set so that /Users/winston/.nix-profile/bin:/nix/var/nix/profiles/default/bin: come first, before /nix/store/050vas2swsvr7sqrf75mkxwi851ycxcz-hello/bin. On the NixOS machine, those two paths are not put first.

I found that the PATH is being set by /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh. Is it intentional that those directories are put first even when nix shell is used?

Hoverbear commented 10 months ago

I was trying to reproduce this on an older Mac install I had and was not able to. I suspect it's related to something that changed in 0.15, perhaps related to the new Mac Nix hooks. I'm going to spend some more time investigating. Thank you for this report!

Hoverbear commented 9 months ago

I tested this again in 0.16.0 today and was able to confirm it. Noting you do need to nix profile install nixpkgs#hello before entering the nix shell to reproduce.

Hoverbear commented 9 months ago

I was able to reproduce this on an aarch64 Sonoma 14.3 using the installer from https://nixos.org/download as well as ours, in your issue you stated you also tried with the upstream installer, however it was when the upstream installer was pointing to 2.18 (I believe). I wonder if this is a Nix 2.19 issue.

~ % nix profile install nixpkgs#hello
~ % nix shell github:wch/nix-hello-demo
~ % hello
Hello, world!
~ % echo $PATH
/Users/user/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/nix/store/050vas2swsvr7sqrf75mkxwi851ycxcz-hello/bin:/Users/user/.nix-profile/bin:/nix/var/nix/profiles/default/bin:...
~ % nix --version
nix (Nix) 2.19.3
~ % sw_vers
ProductName:            macOS
ProductVersion:         14.3
BuildVersion:           23D56

I was also able to reproduce this on a fresh Ubuntu VM using the upstream installer:

image