Solvednix "nix-env --switch-profile=PATH" does not create PATH

Quoting the manual:

$ nix-env --switch-profile /nix/var/nix/profiles/my-profile
$ nix-env --switch-profile /nix/var/nix/profiles/default

These commands switch to the my-profile and default profile, respectively. If the profile doesn’t exist, it will
be created automatically. On my system (3.15.0-linuxlite), if I do

nix-env --switch-profile /nix/var/nix/profiles/mad

there's no error but no profile is created in /nix/var/nix/profiles and the symlink ~/.nix-profile will point to the nonexistent /nix/var/nix/profiles/mad (and cease functioning)

24 Answers

✔️Accepted Answer

IMHO, and it's documented correctly and quite clearly.

the online manual, probably the entry point for people who aren't nix-using experts, says:

$ nix-env --switch-profile /nix/var/nix/profiles/my-profile
$ nix-env --switch-profile /nix/var/nix/profiles/default

These commands switch to the my-profile and default profile, respectively. If the profile doesn’t exist, it will be created automatically.

(Bold added by me) which appears to contradict the manpage, and seems to imply that you can create a new profile with --switch-profile, especially when these example paths don't exist (only having a profiles/per-user folder and no default seem to be a long-standing bugs #1101 #905). Nowhere in the Profile chapter does it tell you how to create a new profile, or that you have to actually run a separate install command for the profile to be created.

But actually using this for a folder that doesn't exist breaks my installation (because nix-env is no longer accessible in thePATH - my (mac, --no-daemon) installer added ~/.nix-profile/etc/profile.d/nix.sh to my .bash_profile (which the manual says is the best way to set up the paths) but this script only adds ~/.nix-profile to the PATH for me.

So "The Bug" is a combination of:

  • --switch-profile switches the softlink away
  • At least with some installers, only ~/.nix-profile is added to path so moving the softlink means you cannot run nix-env to fix the problem
    exacerbated by the manual giving a different impression.

Other Answers:

I think the problem is when you run nix outside of nixos, when you do nix-env --switch-profile PATH, you lose access to the nix binary. It's actually a bit frustrating figuring out how to just switch back to your default profile. I think you should at least document that it does not indeed create the PATH and you should install nix into said profile for it to be useful (if you're not running nixos).

I worked around it by manually installing nix itself into a new profile:

nix-env -p /nix/var/nix/profiles/coursera -i nix

After this I was able to switch to and from the new profile.

More Issues: