mherzl
mherzl

Reputation: 6220

Why does my NixOS installation not have `~/.nix-profile/etc/profile.d/nix.sh`?

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?

Upvotes: 4

Views: 3511

Answers (3)

fghibellini
fghibellini

Reputation: 695

"Entering the Nix environment" means having a shell environment with the environment variables setup in a way that you can execute the Nix commands and Nix-installed programs.

I just installed Nix and faced the same issue. Apparently the environment is now set through the means of /etc/profile.d/nix.sh (which gets automatically loaded by your shell):

$ cat /etc/profile.d/nix.sh
# Nix
if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
  . '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
fi
# End Nix
$ grep PATH /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
export PATH="$NIX_LINK/bin:/nix/var/nix/profiles/default/bin:$PATH"
$ ls /nix/var/nix/profiles/default/bin
nix  nix-build  nix-channel  nix-collect-garbage  nix-copy-closure  nix-daemon  nix-env  nix-hash  nix-instantiate  nix-prefetch-url  nix-shell  nix-store

so ~/.nix-profile/etc/profile.d/nix.sh is no longer necessary.

Upvotes: 0

Ada Lovelace
Ada Lovelace

Reputation: 41

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.

Upvotes: 1

mherzl
mherzl

Reputation: 6220

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.

Upvotes: -1

Related Questions