nix icon indicating copy to clipboard operation
nix copied to clipboard

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

Open bergmark opened this issue 7 years ago • 27 comments

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?

bergmark avatar Jul 06 '18 17:07 bergmark

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.

neon64 avatar Jul 08 '18 10:07 neon64

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.

zenspider avatar Jul 16 '18 10:07 zenspider

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

2mol avatar Aug 05 '18 15:08 2mol

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

janek avatar Mar 27 '19 18:03 janek

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

zenspider avatar Mar 27 '19 19:03 zenspider

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 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.

ryanorendorff avatar Apr 22 '19 05:04 ryanorendorff

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

rreimche avatar Jun 27 '20 10:06 rreimche

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

xeruf avatar Oct 20 '20 09:10 xeruf

I marked this as stale due to inactivity. → More info

stale[bot] avatar Jun 03 '21 02:06 stale[bot]

Should be fixable by adjusting the PATH after finishing installation

xeruf avatar Jun 03 '21 09:06 xeruf

https://stackoverflow.com/a/54087505/6341943 This seems to work for me

jimmy-jos avatar Jan 25 '22 05:01 jimmy-jos

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).

This is what got it to work for me. I think that the installer script just assumes you use bash and doesn't account for the files loaded by zsh.

drod3763 avatar Feb 14 '22 23:02 drod3763

zsh is standard on mac OS now

keithy avatar Mar 17 '22 00:03 keithy

I had this issue after upgrading my macOS version. Personally, I am using home-manager with flake (not 100% sure what it means). To solve my issue, I needed to add the below snippet at the bottom of /etc/zshrc

# 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

didier-ivado avatar Jul 04 '22 13:07 didier-ivado

You must use sudo vim /etc/zshrc to add it. I think a recent macos update replaced the /etc/zshrc, so it has to added in again.

CMCDragonkai avatar Jul 14 '22 04:07 CMCDragonkai

Same issue on macOS 12.5.1 with typical zsh shell.

$ nix-shell
zsh: command not found: nix-shell

I verified the Nix installer appended the file /etc/zshrc:

$ tail /etc/zshrc
# 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

I verified the Nix installer created the file:

$ ls /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh

I verified the Nix daemon is running:

$ ps aux | grep nix-daemon
root             14409   0.0  0.0 409085616  21568   ??  Ss    1:07PM   0:00.06 /nix/var/nix/profiles/default/bin/nix-daemon

What's next please?

joelparkerhenderson avatar Aug 26 '22 20:08 joelparkerhenderson

if you have used nix itself to install zsh and ran the install script within zsh you will need to go back to editing the .bashrc and add the nix-daemon.sh line since you will be using bash instead of zsh again

99Percent avatar Sep 13 '22 17:09 99Percent

In case anyone else stumbles upon this after a MacOS upgrade (it hit me upgrading to Monterey 12.6), in the install/uninstall documentation it mentions the file to source, as shown above: https://nixos.org/manual/nix/stable/installation/installing-binary.html#macos

# 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

My preference is to override any system-wide setting with my user $HOME/.zshrc.

sjbodzo avatar Oct 04 '22 20:10 sjbodzo

I have the same issue on macOS Ventura 13.2 (22D49) + zsh on a fresh install through $ sh <(curl -L https://nixos.org/nix/install).

I've confirmed that:

  • nix-daemon is running
  • /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh exists
  • nix-daemon.sh is sourced in both /etc/zshrc and $HOME/zsh.rc.

I've tried:

  • rebooting
  • restarting Terminal
  • re-installing (uninstalled using instructions in https://nixos.org/manual/nix/stable/installation/installing-binary.html#macos)
  • changing . to source:
    - . '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
    + source '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
    
  • source-ing manually:
    $ source /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
    $ nix-shell
    zsh: command not found: nix-shell
    $ . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
    $ nix-shell
    zsh: command not found: nix-shell
    
  • adding /nix/var/nix/profiles/default/bin to $PATH:
    $ nix-shell
    error: getting status of '/Users/me/default.nix': No such file or directory
    
  • using bash, same issue
  • using stock Terminal.app, same issue

Any ideas on what next?

edit

I fixed this by commenting out the lines that short-circuits on __ETC_PROFILE_NIX_SOURCED=1 in /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh:

# file: /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh

# Only execute this file once per shell.
- if [ -n "${__ETC_PROFILE_NIX_SOURCED:-}" ]; then return; fi
- __ETC_PROFILE_NIX_SOURCED=1
+ # if [ -n "${__ETC_PROFILE_NIX_SOURCED:-}" ]; then return; fi
+ # __ETC_PROFILE_NIX_SOURCED=1

Unsure why this happens 🤷🏻‍♂️ __ETC_PROFILE_NIX_SOURCED probably got unintendedly set to 1 somewhere along the line.

half0wl avatar Apr 24 '23 13:04 half0wl

Just chiming in to say adding this your PATH somewhere might be enough, without editing /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh:

export PATH="$NIX_LINK/bin:/nix/var/nix/profiles/default/bin:$PATH"

webpro avatar May 23 '23 14:05 webpro

Also chiming in to say I fixed it by adding it to my local zshrc and adding this UNSET line:

# Nix
if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
  unset __ETC_PROFILE_NIX_SOURCED
  . '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
fi
# End Nix

dylanarmstrong avatar Jun 28 '23 05:06 dylanarmstrong

I had the same issue after upgrading to macOS ventura 13.5, and fixed it by adding the following line:

source $HOME/.nix-profile/etc/profile.d/nix.sh to my .zprofile (you can also add it to .zshrc)

I got this from the docs: https://nixos.org/manual/nix/stable/installation/env-variables#environment-variables

dbuddha avatar Aug 11 '23 11:08 dbuddha

I would recommend re-running the Nix installer. It walks through cleaning up system files from previous installations and brings the system back into the correct state. In my case, I actually lost $HOME/.nix-profile/etc/.

sourcec0de avatar Sep 28 '23 13:09 sourcec0de

Ran into this as well and the hints from answers above (@2mol @ryanorendorff) did help me move forward.

Short version:

  • after install (MacOS Sonoma + zsh) , nix-shell is not found
  • ln -s /nix/var/nix/profiles/default ~/.nix-profile
  • edit your .bashrc or .zshrc with: . $HOME/.nix-profile/etc/profile.d/nix.sh

now nix-shell -p nix-info --run "nix-info -m" (assuming you have internet connection) does run

chevdor avatar Oct 16 '23 12:10 chevdor

same issue for me, @chevdor solution worked for me. I use OSX 13.4.1 and zsh and used sh <(curl -L https://nixos.org/nix/install) to install nix and got no errors/warnings from it (TBH the install tools is so verbose that it would be easy to miss important information)

tbarbugli avatar Oct 18 '23 07:10 tbarbugli

I would recommend re-running the Nix installer. It walks through cleaning up system files from previous installations and brings the system back into the correct state. In my case, I actually lost $HOME/.nix-profile/etc/.

I upgraded macOS from version 13 to 14, and I confirm that reinstalling restored my Nix environment. Thanks!

fred-snyder avatar Mar 11 '24 13:03 fred-snyder

https://stackoverflow.com/a/54087505/6341943 This seems to work for me

The answer https://stackoverflow.com/a/73799336 from this question solved my problem.

hsteinshiromoto avatar Oct 09 '24 10:10 hsteinshiromoto

I had this issue because my ~/.zshrc was unsetting PATH, thereby undoing the nix path setup in /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh, sourced by /etc/zshrc after the installer modified that file.

Specifically, I had this stanza:

if [ -x /usr/libexec/path_helper ]; then
  unset PATH
  unset MANPATH
  eval `/usr/libexec/path_helper -s`
fi

I have absolutely no idea why, and have no memory of writing this. Maybe it's a default on mac, oh-my-zsh, something else? Either way, if you have unset PATH in a shell profile that executes after /etc/zshrc, consider removing it.

craigfurman avatar Dec 13 '24 15:12 craigfurman

I had this issue and fixed it like this (Step-by-step, working the list-downwards):

  • Run find /nix/store -name nix-shell 2>/dev/null
  • For me, that returned a path. So it was installed somewhere but could not be found. I will refer to this returned path as x.
  • I ran <x> --version, which ran successfully as it returned a version
  • I exported x permanently for zsh/bash (Note, only up until /bin subdirectory): echo 'export PATH="/nix/store/jgfqs02g7gimrg4x6a3i0c03x9byqhc4-nix-2.26.1/bin:$PATH"' >> ~/.zshrc

Hope that helps!

suesslin avatar Feb 03 '25 10:02 suesslin

Last night's upgrade to macOS Tahoe 26.1 broke my Nix setup (which I had freshly installed yesterday on the previous version of macOS 26). nix-shell could no longer be found. I ran the Nix installer again. The first time that failed, because it found an existing /etc/bashrc.backup-before-nix file. It also warned me the existing "real" /etc/bashrc file shouldn't contain any Nix stuff. So I moved the Nix backup of that file to the real one (using sudo mv /etc/bashrc.backup-before-nix /etc/bashrc). I also deleted the /etc/bash.bashrc file, which seemed to contain only Nix stuff. From that "clean" state I was able to re-run the Nix installer and get everything working again.

While I get why these steps were necessary, I do not think this is a great end-user experience. The Nix installer should've done a better job explaining this.

lzandman avatar Nov 04 '25 10:11 lzandman