Jump to content

Command Shell: Difference between revisions

From NixOS Wiki
imported>Liassica
For a specific user: Fix <name>.shell link
m I added a small section helping users to fix a strange problem with the fish shell when using it alongside flakes. I think it was worth adding because I followed this page, but was suprised to find strange output when I ran a command that didn't exist.
 
(4 intermediate revisions by 3 users not shown)
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. [[Fish]] or [[Nushell]].}}


{{note|[[Zsh]] is used here as an example. You can use other shells, e.g. {{ic|fish}} or {{ic|nushell}}.}}
== Enable ==
== 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.
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.


Line 16: Line 18:
To set a command shell as the default for all users, use the [https://search.nixos.org/options?query=defaultUserShell <code>defaultUserShell</code>] option.
To set a command shell as the default for all users, use the [https://search.nixos.org/options?query=defaultUserShell <code>defaultUserShell</code>] option.


For example, to set Zsh as the default user shell for all users:
For example, to set Zsh as the default user shell for all users(including root):


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 38: Line 40:
</nowiki>}}
</nowiki>}}


== Changing /bin/sh ==
== Using a different shell in nix-shell and nix develop ==
{{Warning|Please note that NixOS assumes all over the place that the shell is bash, so override the default setting only if you know what you're doing.}}
 
{{ic|/bin/sh}} is a symlink to your default POSIX-compliant shell. It's used when writing shell scripts, so that the script works on all machines independently of which shell the user is using. /bin/sh doesn't have to be the same as your interactive shell (i.e. the one you use in your terminal). For example, some people set their interactive shells to [[zsh]]/[[fish]] but set /bin/sh to dash, because it's fast and scripts don't need any of those fancy zsh/fish features.
By default, both <code>nix develop</code> and <code>nix-shell</code> 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 [https://github.com/MercuryTechnologies/nix-your-shell nix-your-shell] for installation and usage instructions.
 
=== any-nix-shell ===
 
Refer to [https://github.com/haslersn/any-nix-shell 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:
 
{{file|.bashrc|bash|
<nowiki>
alias nix-shell='nix-shell --run zsh'
nix() {
  if [[ $1 == "develop" ]]; then
    shift
    command nix develop -c zsh "$@"
  else
    command nix "$@"
  fi
}
</nowiki>
}}
 
There are some caveats with this approach:
* Nested shells will require you to type <code>exit</code> 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.


To change your default POSIX shell on NixOS, use
== Using Flakes ==
<syntaxhighlight lang="nix">
If you are using [[Flakes]] with fish, the 'command-not-found' error message will not work correctly, but can be fixed by disabling the builtin Nix <code>command-not-found</code> program:<ref>https://github.com/NixOS/nixpkgs/issues/425613#issuecomment-3076081921</ref>
# Dash is just an example, you can use whatever you want
{{File|3=programs.command-not-found.enable = false;|name=/etc/nixos/configuration.nix|lang=nix}}
environment.binsh = "${pkgs.dash}/bin/dash";
</syntaxhighlight>


== See also ==
== See also ==
Line 52: Line 84:
* [[Nushell]]
* [[Nushell]]
* [[Zsh]]
* [[Zsh]]
* [[Flakes]]


[[Category:Configuration]]
[[Category:Configuration]]
[[Category:Software]]
[[Category:Software]]
[[Category:Shell]]

Latest revision as of 06:20, 16 July 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.

Note: Zsh is used here as an example. You can use other shells, e.g. Fish or Nushell.

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:

❄︎ /etc/nixos/configuration.nix
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):

❄︎ /etc/nixos/configuration.nix
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:

❄︎ /etc/nixos/configuration.nix
users.users.myuser.shell = pkgs.fish;

You can also choose whether or not a user should use the default shell:

❄︎ /etc/nixos/configuration.nix
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:

≡︎ .bashrc
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.

Using Flakes

If you are using Flakes with fish, the 'command-not-found' error message will not work correctly, but can be fixed by disabling the builtin Nix command-not-found program:[1]

❄︎ /etc/nixos/configuration.nix
programs.command-not-found.enable = false;

See also