Using home-manager to control default user shell?

Hi everyone! Is it possible to use home-manager to control my default user shell? Something like chsh, but made Nix-y?

I wasn’t able to find anything searching on google or looking through the home-manager docs. I’m running home-manager on Ubuntu.


To follow up on this: It also seems like it’s not possible to make chsh use a home-manager-installed shell:

$ chsh -s /home/skainswo/.nix-profile/bin/zsh
chsh: /home/skainswo/.nix-profile/bin/zsh is an invalid shell

Is there any way to use a home-manager-managed shell as my default login shell?

EDIT: Fix is to add /home/skainswo/.nix-profile/bin/zsh as a line to /etc/shells.

EDIT: Fix is to add /home/skainswo/.nix-profile/bin/zsh as a line to /etc/shells .

I’ve did this for a nix install on Debian WSL, and while it works, I have found it is incomplete, since zsh does not source ~/.profile (NB this friendly helpful graph on which startup files sh, bash and zsh source depending on if the shell is login/interactive).

The nix installer adds a line to .profile that, among other things, add ~/.nix-profile/bin/ to $PATH, so if you chsh you nix-installed zsh, it won’t be able find your other nix-installed programs. Luckily, this is easy to fix; just add the additional line from .profile to .zprofile:

if [ -e $HOME/.nix-profile/etc/profile.d/ ]; then . $HOME/.nix-profile/etc/profile.d/; fi

I think that should fix it, but it wouldn’t surprise me if I am missing something, as with anything involving the shell.

Has there been any progress on this in the Nix world, in the meantime?

Unfortunately this doesn’t work on machines where you don’t have admin access.

It’s impossible to change your default shell to a shell the admin doesn’t approve first. From the chsh manual:

The shell field is the command interpreter the user prefers. If the
shell field is empty, the Bourne shell, /bin/sh, is assumed. When
altering a login shell, and not the super-user, the user may not change
from a non-standard shell or to a non-standard shell. Non-standard is
defined as a shell not found in /etc/shells.

This isn’t a restriction imposed by NixOS. I believe the reasoning for this is that this allows creating chroot jails, which while a bit outdated in terms of modern user management, are still a use case basic Linux user management supports.

You can just have an exec at the start of your ~/.bash_profile, or whatever your sysadmin wants you to use by default, and then read your chosen shell’s docs for how to make it properly source init files when executed this way.