Problem with NIX_PATH installing Home Manager standalone on NixOS 22.05

I’m very new to Nix, having installed NixOS 22.05 yesterday. I figured I’d play around with Home Manager (as I’d like my config to be somewhat portable to macOS, I don’t view my “home” setup to be particularly tightly tied to my system configuration, etc.). So, I’m attempting to install using the “standalone” instructions at Home Manager Manual

I run into a problem related to NIX_PATH straight away. Am I holding NixOS or Home Manager wrong?

According to the instructions the following should work on NixOS:

$ nix-channel --add home-manager
$ nix-channel --update
unpacking channels...
$ nix-channel --list
$ nix-shell '<home-manager>' -A install
error: file 'home-manager' was not found in the Nix search path (add it using $NIX_PATH or -I)

The docs say that for non-NixOS I should execute this in my shell:

export NIX_PATH=$HOME/.nix-defexpr/channels:/nix/var/nix/profiles/per-user/root/channels${NIX_PATH:+:$NIX_PATH}

…and indeed when I do the same it fixes the problem. But if NIX_PATH is needed, why isn’t this set up for me automatically on NixOS? I’m running a near vanilla NixOS 22.05 as installed via the GUI installer. Is there something I should be doing differently?

1 Like

When you first log in and haven’t altered NIX_PATH yet, what is it set to?

When I first log in NIX_PATH isn’t set at all. Searching around I found some mention of this being expected in NixOS, but I’m far from an expert.

Then somehow /etc/profile isn’t being sourced, or you’ve set some option that affects this. I just built a vm from a blank config, and I get:

export NIX_PATH="nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels"

in /etc/set-environment, which is sourced from /etc/profile.

Later in the same file I see:

if [ -e "$HOME/.nix-defexpr/channels" ]; then
  export NIX_PATH="$HOME/.nix-defexpr/channels${NIX_PATH:+:$NIX_PATH}"

Thanks @tejing, good to know how it is working for others.

More details: I’m running Gnome and using Gnome Terminal. My understanding is that this combination will not source /etc/profile at any point, and this is by (Gnome’s) design. See Initiatives/Wayland/SessionStart - GNOME Wiki!. Gnome Terminal doesn’t start login shells by default (although this is an option). Apparently environment.d is the new hotness for setting up default environment variables.

Some other possibly related discussions:

Short-term, you can probably get away with just adding . /etc/set-environment to your ~/.bashrc, but that might come back to bite you later. I know zip about wayland or gnome, so I can’t help you there, but the ideal solution is to make sure your desktop environment is running with /etc/set-environment sourced. Lots of nixos modules will expect that path to work, so it’s definitely a bug if it doesn’t. What display manager are you using? At least with lightdm, it’s actually part of the display manager config that makes sure /etc/profile is sourced before the session is started.

Had time to look at this again and when I install into a VM and do nothing but install gnome-terminal from there, all is well (i.e. NIX_PATH is set in the shells it runs). So, likely, this was something with my personal dotfiles or some such.

Good to know. I suspected there was an element specific to you involved somehow, since that seemed like a little too major a piece to be broken, if it happened in gnome generally.