Command Shell: Difference between revisions
Adds section on using different shell in nix development environments Tags: Mobile edit Mobile web edit Advanced mobile edit |
m hyperlink shell, fish, and nushell |
||
Line 1: | Line 1: | ||
A shell is a program that translates text commands (like {{ic|ls}}, {{ic|vim}}, {{ic|reboot}} etc) into instructions for your computer. The default shell on NixOS is [[bash]], but it can be easily changed. | A [https://en.wikipedia.org/wiki/Unix_shell shell] is a program that translates text commands (like {{ic|ls}}, {{ic|vim}}, {{ic|reboot}} etc) into instructions for your computer. The default shell on NixOS is [[bash]], but it can be easily changed. | ||
{{note|[[Zsh]] is used here as an example. You can use other shells, e.g. | {{note|[[Zsh]] is used here as an example. You can use other shells, e.g. [[Fish]] or [[Nushell]].}} | ||
== Enable == | == Enable == |
Revision as of 00:52, 14 May 2025
A shell is a program that translates text commands (like ls
, vim
, reboot
etc) into instructions for your computer. The default shell on NixOS is bash, but it can be easily changed.
Enable
When adding a new shell, always enable the shell system-wide, even if it's already enabled in your Home Manager configuration, otherwise it won't source the necessary files.
For example, for Zsh:
programs.zsh.enable = true;
Changing the default shell
For all users
To set a command shell as the default for all users, use the defaultUserShell
option.
For example, to set Zsh as the default user shell for all users(including root):
users.defaultUserShell = pkgs.zsh;
For a specific user
To set a command shell as the default for a particular user, use the <name>.shell
option.
For example, to set user "myuser"'s shell to fish:
users.users.myuser.shell = pkgs.fish;
You can also choose whether or not a user should use the default shell:
users.users.myuser.useDefaultShell = true;
Using a different shell in nix-shell and nix develop
By default, both nix develop
and nix-shell
launch an interactive bash shell. However, it is possible to configure these environments to use an alternative shell.
There are multiple approaches available:
nix-your-shell
Refer to nix-your-shell for installation and usage instructions.
any-nix-shell
Refer to any-nix-shell for installation and usage instructions.
Aliasing command
A simple, lightweight method is to alias the relevant commands to invoke the shell directly. For example:
alias nix-shell='nix-shell --run zsh'
nix() {
if [[ $1 == "develop" ]]; then
shift
command nix develop -c zsh "$@"
else
command nix "$@"
fi
}
There are some caveats with this approach:
- Nested shells will require you to type
exit
twice, once to leave the inner shell and once to exit the Nix shell environment itself. - Potential for alias conflicts as overriding commands can introduce unintended side effects in scripts or other tooling that expects the standard behavior.