Using Nix to install login shell on non-NixOS platform

Has anyone used Nix to install a user login shell on a non-NixOS platform (such as macOS)? If I wanted to do that, I’m not sure how I’d go about setting up the Nix-installed shell as my login shell, given that the path to the shell will change every time it updates (necessitating both an update to /etc/shells and to my user configuration).

My only thought is some wrapper script that knows how to find the real shell, but I’m not thrilled about the idea of having every login do something like run Bash, do Nix PATH setup, search for my real shell, and then exec that (which would then do its own Nix setup), because that seems like a bunch of unnecessary work every time I want to open a terminal. Is there a better way, some way to have Nix just e.g. hardlink my specified shell into a known path and update it any time my shell updates?

Or alternatively, can I set a symlink as my login shell? If so, then in theory I could set ~/.nix-profile/bin/fish as my login shell, though I then risk screwing myself if I ever accidentally remove all packages from the current generation (e.g. accidentally run nix-env -i -r someUnrelatedPackage).

I set the shell to $HOME/.nix-profile/bin/zsh. This should work, just need to edit /etc/shells:

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/Users/mbauer/.nix-profile/bin/bash
/Users/mbauer/.nix-profile/bin/zsh

and then i think you need to set it in system preferences. if you remove the nix-env, you will need to reset it to one of the builtin macos shells above.

2 Likes

As a small variation to what @matthewbauer is using, nix-darwin has a shells module that can revise /etc/shells on your behalf.

I use mine like so:

  # Create /etc/bashrc that loads the nix-darwin environment.
  programs.bash.enable = true;
  programs.zsh.enable = true;
  programs.fish.enable = true;
  environment.shells = with pkgs; [ bashInteractive fish zsh ];

then

$ chsh -s /run/current-system/sw/bin/fish

Yes, that is on OSX (Mojave 10.14.4 in my case, using nixpkgs-19.03-darwin channel and nix-darwin @ master.

PS - can anyone point me to the tool I see infrequently mentioned where people have described their channels with an JSON output that can be used to pin the specific channel revision?

4 Likes

I hadn’t seen nix-darwin before. Very interesting. Though I think I’d be too nervous to try and retrofit my existing system with that.