Nix: Can't find nix-shell after installationon mac OS X

Created on 6 Jul 2018  路  8Comments  路  Source: NixOS/nix

On Mac OS X (10.11.6) I ran curl https://nixos.org/nix/install | sh which downloaded https://nixos.org/releases/nix/nix-2.0.4/nix-2.0.4-x86_64-darwin.tar.bz2

On completion this was printed:

Before Nix will work in your existing shells, you'll need to close
them and open them again. Other than that, you should be ready to go.

Try it! Open a new terminal, and type:

  $ nix-shell -p nix-info --run "nix-info -m"

I started a new shell but nix-shell is not on my PATH in fish nor in bash

$ nix-shell
fish: Unknown command 'nix-shell'
$ bash
$ nix-shell
bash: nix-shell: command not found

I found nix-shell in /nix but could not start it:

$ /nix/store/771l8i0mz4c8kry8cz3sz8rr3alalckg-nix-2.0.4/bin/nix-shell -p nix-info --run "nix-info -m"
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I), at (string):1:13

Should the post-install instructions be updated? Are there any additional steps needed to get nix-shell to run?

Most helpful comment

There seems to be a case where the installer script modifies .profile but that file is not sourced correctly. For my case, after installation I got this message (with $HOME filled in).

Installation finished!  To ensure that the necessary environment
variables are set, either log in again, or type

  . $HOME/.nix-profile/etc/profile.d/nix.sh

in your shell.

However I am using zsh, which does not load .profile automatically. Hence I had to add the line . $HOME/.nix-profile/etc/profile.d/nix.sh to something zsh executes (.zshrc).

The installer script seems to assume you are using bash. The culprit appears to be this line, which doesn't look for any other shells than bash (and only specific bash files that I aren't guaranteed to be there) or shells that source .profile.

https://github.com/NixOS/nix/blob/5112a33fb17f792ceb6d641738277cbbe6a58bfc/scripts/install-nix-from-closure.sh#L144

Below is the problem line in context for the macOS installer.

if [ -z "$NIX_INSTALLER_NO_MODIFY_PROFILE" ]; then

    # Make the shell source nix.sh during login.
    p=$HOME/.nix-profile/etc/profile.d/nix.sh

    for i in .bash_profile .bash_login .profile; do # <-- This is the problematic line
        fn="$HOME/$i"
        if [ -w "$fn" ]; then
            if ! grep -q "$p" "$fn"; then
                echo "modifying $fn..." >&2
                echo "if [ -e $p ]; then . $p; fi # added by Nix installer" >> "$fn"
            fi
            added=1
            break
        fi
    done

fi

@zenspider Hopefully this helps.

All 8 comments

I'm struggling with this issue too. As far as I'm aware, when a login shell starts, it loads certain init scripts like /etc/bashrc or /etc/profile. The nix installer adds new stuff to these scripts so that nix tools like nix-shell can be found. This means:

  1. you need to open a new terminal window for these init scripts to be run again
  2. these scripts are specific to the type of shell you're running. It appears as if nix has built in support for bash and zsh, but not fish: see https://github.com/NixOS/nix/issues/1512 for previous discussion on this.

Same boat, but I'm using bash as my shell. It seems to never actually modify PATH and other things aren't quite working right either. I eventually "uninstalled", even tho that's not a thing either. It would be nice if there was a real cleanup script in the base.

Same problem here, and I'm running bash. I figured out after a while that the ~/.nix-profile was missing or broken (~/.nix-profile/bin is supposed to be added to your PATH).

You can hack-fix it by symlinking to /nix/var/nix/profiles/default, but no guarantee that that results in a clean install.

Seems kinda related to #2295

Is this currently still broken? Are there any good workarounds?

Dunno. After zero support here and anti-support in IRC I gave up. Just not worth the apathy and hostility to deal with it.

There seems to be a case where the installer script modifies .profile but that file is not sourced correctly. For my case, after installation I got this message (with $HOME filled in).

Installation finished!  To ensure that the necessary environment
variables are set, either log in again, or type

  . $HOME/.nix-profile/etc/profile.d/nix.sh

in your shell.

However I am using zsh, which does not load .profile automatically. Hence I had to add the line . $HOME/.nix-profile/etc/profile.d/nix.sh to something zsh executes (.zshrc).

The installer script seems to assume you are using bash. The culprit appears to be this line, which doesn't look for any other shells than bash (and only specific bash files that I aren't guaranteed to be there) or shells that source .profile.

https://github.com/NixOS/nix/blob/5112a33fb17f792ceb6d641738277cbbe6a58bfc/scripts/install-nix-from-closure.sh#L144

Below is the problem line in context for the macOS installer.

if [ -z "$NIX_INSTALLER_NO_MODIFY_PROFILE" ]; then

    # Make the shell source nix.sh during login.
    p=$HOME/.nix-profile/etc/profile.d/nix.sh

    for i in .bash_profile .bash_login .profile; do # <-- This is the problematic line
        fn="$HOME/$i"
        if [ -w "$fn" ]; then
            if ! grep -q "$p" "$fn"; then
                echo "modifying $fn..." >&2
                echo "if [ -e $p ]; then . $p; fi # added by Nix installer" >> "$fn"
            fi
            added=1
            break
        fi
    done

fi

@zenspider Hopefully this helps.

I've had the same problem on a fresh Ubuntu 19.10 and the problem was gone as soon as I've rebooted.

Yep, similar thing here. On my system the /etc/zshrc doesn't exist, so opening a new terminal or zsh session didn't help.

Instead, I had to log out and back in, which loaded the appropriate PATH from /etc/profile.d/nix.sh

Was this page helpful?
0 / 5 - 0 ratings