Nix-shell does not use my user's shell (zsh)

Why doesn’t nix-shell use my shell, zsh?

EDIT: Using GitHub - chisui/zsh-nix-shell: zsh plugin that lets you use zsh in nix-shell shells. was easy enough to solve the problem (but it only works with nix-shell and not nix run). Is there an official way to do it?

5 Likes

I’m pretty sure that is related to reproducibility.

If there is a zsh for you and bash for everyone else, then the environments aren’t the same anymore.

2 Likes

I set zsh as the system shell for “everyone”. It’ll be only me using my personal laptop anyways, no one else.

1 Like

And all the other computers? That you do not own and control?

Bash is the “guaranteed” shell on nix. It has to be the default environment to run things in. If you need something else, you probably need to build your own stdenv which provides another shell.

1 Like

Not sure what you mean. I’m not asking for zsh to be the default for NixOS, I’m just asking how to make zsh the default just for me.

That is too advanced for me right now (given the time constraints that I have and needing to get to work on some deliverables that I’ve put off while learning the awesome NixOS).

It’d be neat if nixpkgs had an official option. That’s something for later though.

I’ll use chisui/zsh-nix-shell for now. I’ve opened an issue in that repo asking about support for nix run too.

Well, the main issue is that generic builder is written in bash. nix-shell, among other things, is supposed to place you into the development environment where you can call the bash functions manually. For the other use cases it is not necessary and we have several issues open about that:

Personally, I just run nix-shell … --run fish when I do not need to access builder functions.

Yeah, basically I only been using nix-shell so that I can spin up a new shell with certain binaries present, but not needing the nixos-specific build commands. F.e. to make shells with different versions of node or electron.

If you’re using it for that, you may well be better off using lorri.

3 Likes

nix-shell should honor $SHELL when in interactive mode. I don’t buy that you should be forced to drop into bash as a human. I’m happy to be in a zsh inside a bash, but it seems uncouth to disregard the system shell preference. We wouldn’t drop into nano if EDITOR=vi.

7 Likes

Thanks, that helps a lot.
Maybe we should treat nix-shell as tool for package builder, not for package users. So maybe we need some other tools to provide virtural environment for package users?

nix-shell is indeed a tool for package developers.

The experimental commands provide nix run (2.3)/nix shell (2.4) which are meant to be used by package consumers.

There is no such tool in the stable commands. nix-shell -p comes closest, but still enforces bash.

1 Like

I’m not sure what’s the status of this now but I can confirm that any-nix-shell worked for me. There is also a more manual approach presented here How to Learn Nix, Part 42: Running zsh in nix-shell

Edit: or you can manually start zsh once you are in the shell ^^

2 Likes

Hello!
I also had this problem. I found that nix-shell --run $SHELL and nix develop -c $SHELL would both open ZSH as I preferred, so my solution was to make a bash alias for these two functions.

Add the following to your shell rc file:

alias nix-shell='nix-shell --run $SHELL'
nix() {
  if [[ $1 == "develop" ]]; then
    shift
    command nix develop -c $SHELL "$@"
  else
    command nix "$@"
  fi
}

For zsh, I added this to my zshrc file.

Now when I type nix-shell or nix develop, it opens zsh instead of bash.
Hope this helps!