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.29k stars 60 forks source link

nix: not found on root user PATH using sudo after linux multiuser install #965

Open timblaktu opened 6 months ago

timblaktu commented 6 months ago

On Ubuntu/WSL, I installed latest nix (2.21.2) with sudo nix-installer install linux, and can access it as normal user at /nix/var/nix/profiles/default/bin/nix:

$ id
uid=1000(tim) gid=1000(tim) groups=1000(tim),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),116(netdev)
$ which nix
/nix/var/nix/profiles/default/bin/nix

and as root user at /root/.nix-profile/bin/nix:

$ sudo su
# id
uid=0(root) gid=0(root) groups=0(root)
# which nix
/root/.nix-profile/bin/nix

However, normal user utterances of sudo nix don't find nix binary:

$ sudo nix --version
sudo: nix: command not found

We can make this error go away with a root login shell (sudo -i/--login):

$ sudo -i nix --version
nix (Nix) 2.21.2

..but being forced to use a login shell is undesirable in cases where I want to run nix escalated with sudo but do NOT want the environment to change.

For example, I'm using sudo nix run to execute the tarballBuilder defined in a modified, local working copy of the NixOS-WSL project, which requires root privileges. Passing the flakeref argument to sudo -i nix run requires an absolute path, even when run from the flake root directory, because cwd is changed by sudo -i.

This is cumbersome when you consider that instructions for users of nix projects like NixOS-WSL now depend on the way the user installed nix. In my case, none of the NixOS-WSL build examples worked for me because I happen to have installed nix with nix-installer.

So, the summary of this issue is that:

Below are my environment details. I look forward to your feedback. Thanks for your time and contributions for this amazing project!

Environment

Ubuntu 22.04.3 LTS on WSL version: 2.1.5.0 Kernel version: 5.15.146.1-2 WSLg version: 1.0.60 MSRDC version: 1.2.5105 Direct3D version: 1.611.1-81528511 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.22631.3593

cole-h commented 5 months ago

Thanks for the report. Does sudo -E work as an alternative to sudo -i without changing the cwd?

jazzdan commented 5 months ago

I can also reproduce this on Linux/aarch64

Avanesov79 commented 1 month ago

I have just had this issue on uBlue Aurora (Fedoa Silverblue based, rpm-ostree distro).

sudo -E nix-env --help Does not work for me. The binary is not in $PATH

I dont understand the discrepancy here between these three commands:

sudo -E echo $PATH /home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/var/home/matthew/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/var/home/matthew/.local/bin:/var/home/matthew/bin:/var/home/matthew/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

sudo -E which nix-env which: no nix-env in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/var/lib/snapd/snap/bin)

Used "nix-foo" in my user profile just so it would fail and return the paths for comparison.

which nix-foo /usr/bin/which: no nix-foo in (/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/var/home/matthew/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/var/home/matthew/.local/bin:/var/home/matthew/bin:/var/home/matthew/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin)

Avanesov79 commented 1 month ago

As an aside, what can be done to manually fix this while waiting for the bug to be fixed? I don't understand the difference between "sudo " and "sudo -i " as far as why one would work and the other will not.