A NixOS user, I am studying the Nix Pills. They frequently reference ~/.nix-profile/etc/profile.d/nix.sh as a means of "entering the Nix environment". E.g. quoting from chapter 5:

I remind you how to enter the Nix environment: source~/.nix-profile/etc/profile.d/nix.sh

However, my NixOS machine does not even have a ~/.nix-profile/etc directory, let alone a ~/.nix-profile/etc/profile.d/nix.sh file. My questions:

  1. Why might my machine not have ~/.nix-profile/etc?
  2. Is there an ordinary way to generate ~/.nix-profile/etc/profile.d/nix.sh?
  3. Is there straightforward alternative way to "enter the Nix environment" in NixOS?
2

Best Answer


According to the official documentation, profile could be automatically generated when you've switched to it by command:$ nix-env --switch-profile /nix/var/nix/profiles/default

So, you should try this command and it should create missed files and directories.

Here is the text copied from official doc:

You generally wouldn’t have /nix/var/nix/profiles/some-profile/bin in your PATH. Rather, there is a symlink ~/.nix-profile that points to your current profile. This means that you should put ~/.nix-profile/bin in your PATH (and indeed, that’s what the initialisation script /nix/etc/profile.d/nix.sh does). This makes it easier to switch to a different profile. You can do that using the command nix-env --switch-profile:

$ 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. You should be careful about storing a profile in another location than the profiles directory, since otherwise it might not be used as a root of the garbage collector.

I do not yet have answers to (1) and (2), but do have an answer to (3). That is:

$ nix repl

works to enter the nix environment.