Installing NixOS from another Linux distribution doesn’t work when done according to the manual

The NixOS Manual describes a procedure for installing NixOS from another Linux distribution, which involves running some code as an ordinary user and some code as root. However, when I follow this approach, nixos-install fails, complaining it can’t find <nixpkgs/nixos> (from what I remember) and suggesting to add the relevant directory to NIX_PATH.

The procedure described in the manual involves setting NIX_PATH for the nixos-install invocation according to the value of NIX_PATH that the ordinary user sees. However, NIX_PATH is not set for the ordinary user. Still, running nix-instantiate --eval -E '<nixpkgs/nixos>' as the ordinary user succeeds. Apparently, there is some additional magic that makes Nix find <nixpkgs/nixos> when working as the ordinary user, but not when working as root.

Could the manual please be fixed such that the description of how to install NixOS from another Linux distribution is correct again?

I quickly tried to install NixOS performing everything as root, and at a first glance this seems to work. However, running in particular as root is not recommended. Is there any inherent problem with performing the whole installation as root? I understand that you should minimize the amount of code executions as root, but in my case the installation is performed from a rescue system, where there is not much to damage. Can I rely on a root-only installation giving a correct result?

Did . $HOME/.nix-profile/etc/profile.d/ really not set NIX_PATH? As I understand it, it should have.

Also, how did you manage the earlier steps like nix-env -f '<nixpkgs>' -iA nixos-install-tools without a NIX_PATH?

There is nothing about NIX_PATH in $HOME/.nix-profile/etc/profile.d/ I have no clue how tools like nix-env find the files they need. As I wrote, nix-instantiate --eval -E '<nixpkgs/nixos>' is successful when run as the ordinary user who installed Nix (but not when run as root).

I think I read in some issue on GitHub that at some point something was changed regarding finding files such that NIX_PATH became unnecessary in certain situations, but I’m not sure about any details.

Well, if nix is using a default NIX_PATH because there isn’t one set, then it makes sense that it fails when run as root, as the default would be different (hence the command trying to preserve your current NIX_PATH). If I remember the pre-flake defaults correctly, you should be able to proceed by using:

$ sudo PATH="$PATH" NIX_PATH="$HOME/.nix-defexpr/channels" `which nixos-install` --root /mnt
1 Like

This approach works until shortly before the end of the installation, where I get the following error messages from nixos-enter:

line 101: chroot: command not found
line 104: chroot: command not found
line 109: exec: chroot: not found

When running the whole installation procedure as root, this doesn’t happen.

On some distributions there are separate PATHS for programs intended only for root. In order for the installation to succeed, you might have to use PATH="$PATH:/usr/sbin:/sbin" in the following command.

Sounds like this was the issue.

Actually, I did things slightly differently: instead of logging in as the ordinary user and then becoming root via sudo, I logged in as root, fetched PATH and NIX_PATH from the ordinary user, and then executed nixos-install directly as root. However, the above problem showed up nevertheless. The directories /usr/sbin and /sbin are in root’s path, and I can run chroot as root from the shell without problem. So it seems there must be a different problem.

Now I had to discover that executing the whole installation procedure as root results in another problem: the install script complains about the group nixbld not being present. The manual asks you to create this group, but only at a later point. When run as an ordinary user, install did not insist on nixbld being present. Why is install working differently when being run by root?

Hosted by Flying Circus.