Where is NIX_PATH supposed to be set?

I just noticed the warning

error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

in my shell. And indeed, NIX_PATH isn’t set at all.

If I set it manually to what it used to be

export NIX_PATH=/home/magnus/.nix-defexpr/channels

then the warning goes away.

I’m a little confused as I’m sure this has happened automatically in the past. The only bigger change I’ve made in the last week or so would be an upgrade to 21.11 (bumping nixpkgs to 21.11 using nix-channel, followed by a nix-channel--update && nix-env --upgrade).

Anyway, where should I look in order to get the automatic setting of NIX_PATH back?

(I’m using a single-user installation, Nix on ArchLinux.)

1 Like

The nix installation should provide a file which sets up your user environment. You need to make sure to source it as part of your shell config. See Environment Variables in the Nix manual.

You need to source ~/.nix-profile/etc/profile.d/nix.sh or /etc/profile.d/nix.sh depending on your installation type.

As you said you are on a single user install, it is the former.

1 Like

The only problem is that I am including that in my shell!

This is the last few lines in my shell setup (ZSH):

if [[ -f ~/.nix-profile/etc/profile.d/nix.sh ]]; then
    source ~/.nix-profile/etc/profile.d/nix.sh
    export fpath=(~/.nix-profile/share/zsh/vendor-completions ${fpath})
fi

This has worked fine for months, but clearly isn’t working any longer.

There is no mention of NIX_PATH in ~/.nix-profile/etc/profile.d/nix.sh, so I’m still wondering exactly where it’s supposed to be set up.

This is what’s in the file I source:

if [ -n "$HOME" ] && [ -n "$USER" ]; then

    # Set up the per-user profile.
    # This part should be kept in sync with nixpkgs:nixos/modules/programs/shell.nix

    NIX_LINK=$HOME/.nix-profile

    # Set up environment.
    # This part should be kept in sync with nixpkgs:nixos/modules/programs/environment.nix
    export NIX_PROFILES="/nix/var/nix/profiles/default $HOME/.nix-profile"

    # Set $NIX_SSL_CERT_FILE so that Nixpkgs applications like curl work.
    if [ -e /etc/ssl/certs/ca-certificates.crt ]; then # NixOS, Ubuntu, Debian, Gentoo, Arch
        export NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
    elif [ -e /etc/ssl/ca-bundle.pem ]; then # openSUSE Tumbleweed
        export NIX_SSL_CERT_FILE=/etc/ssl/ca-bundle.pem
    elif [ -e /etc/ssl/certs/ca-bundle.crt ]; then # Old NixOS
        export NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
    elif [ -e /etc/pki/tls/certs/ca-bundle.crt ]; then # Fedora, CentOS
        export NIX_SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt
    elif [ -e "$NIX_LINK/etc/ssl/certs/ca-bundle.crt" ]; then # fall back to cacert in Nix profile
        export NIX_SSL_CERT_FILE="$NIX_LINK/etc/ssl/certs/ca-bundle.crt"
    elif [ -e "$NIX_LINK/etc/ca-bundle.crt" ]; then # old cacert in Nix profile
        export NIX_SSL_CERT_FILE="$NIX_LINK/etc/ca-bundle.crt"
    fi

    if [ -n "${MANPATH-}" ]; then
        export MANPATH="$NIX_LINK/share/man:$MANPATH"
    fi

    export PATH="$NIX_LINK/bin:$PATH"
    unset NIX_LINK
fi

There is also nix.nixPath.

The OP is on Arch Linux, so they do not have nix.nixPath.

And I tried to find where nix.sh would be generated or created from, though I was unable to find it in nixpkgs :frowning:

I hoped there would be something interesting to find in that files history, but…

On non-NixOS, NIX_PATH doesn’t get set anymore if you installed via the binary tarball installer. Instead, it’s supposed to use a default search path here.

On NixOS, it’s still set in the NixOS module that configures Nix. On Debian-based systems, if you install nix-setup-systemd, you get a file called /usr/lib/environment.d/nix-daemon.conf where this gets set. On my Ubuntu system, it reads:

NIX_REMOTE=daemon
PATH="$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:$PATH"
NIX_PATH="nixpkgs=/nix/var/nix/profiles/per-user/$USER/channels/nixpkgs:/nix/var/nix/profiles/per-user/$USER/channels"

I assume all of this led to few people noticing the issue you’re seeing.

What’s weird is that Nix is complaining about NIX_PATH being unset, even though it gets a default one in eval.cc. That seems inconsistent.

NIX_PATH still gets set in nix.sh and nix-daemon.sh in Nix 2.3.x, both for single-user and for multi-user installations. So that you now get the warning is a bug in Nix 2.4 which was previously unnoticed.

This commit is where NIX_PATH got pulled from the relevant file in favor of the default in eval.cc, I think.

If you want to add what was in Nix 2.3, for your convenience, it’s this for single-user:

    # Append ~/.nix-defexpr/channels to $NIX_PATH so that <nixpkgs>
    # paths work when the user has fetched the Nixpkgs channel.
    export NIX_PATH=${NIX_PATH:+$NIX_PATH:}$HOME/.nix-defexpr/channels

Are commands you need actually crapping out on you, or does the error message seem superfluous?

2 Likes

So, it’s a bug in 2.4 that it’s not set?

In that case I’ll check on GitHub if it’s been reported already, and if not, I’ll report it.

Thanks for the help.

1 Like

I’m not sure if it not being set is a bug, or if the bug is the default/fallback handling in eval.cc not covering your use case. That’s why I asked how the change was affecting you, over and above just the printing of the error message, to start figuring out whether it was a superfluous warning, missing functionality that should be covered by that default, or missing functionality that that default is not intended to cover.

I’m sure you’ll figure out a good way to summarize the issue. :slight_smile:

Bug reported: NIX_PATH not set after upgrade to 21.11 / 2.4 (single-user installation on ArchLinux) · Issue #149791 · NixOS/nixpkgs · GitHub

1 Like