Command Shell: Difference between revisions

From NixOS Wiki
imported>Nix
m add software and configuration categories
Claes (talk | contribs)
Add category shell
 
(12 intermediate revisions by 7 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 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, eg {{ic|fish}}.}}
{{note|[[Zsh]] is used here as an example. You can use other shells, e.g. {{ic|fish}} or {{ic|nushell}}.}}
== Enable ==
== Enable ==
Always enable the shell system-wide, even if it's already enabled in your <code>home.nix</code>. Otherwise it wont 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.
 
For example, for [[Zsh]]:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
programs.zsh.enable = true;
programs.zsh.enable = true;
</nowiki>}}
</nowiki>}}


== Changing default shell ==
== Changing the default shell ==
Shells can be changed system-wide and per-user. To change the shell system-wide, add the following line to your config:
 
=== For all users ===
 
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:
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
users.defaultUserShell = pkgs.zsh;
users.defaultUserShell = pkgs.zsh;
</nowiki>}}
</nowiki>}}
then run {{ic|nixos-rebuild switch}} and reboot your system.


To only change the default shell for one of the users, add
=== For a specific user ===
 
To set a command shell as the default for a particular user, use the [https://search.nixos.org/options?query=%3Cname%3E.shell <code><nowiki><name></nowiki>.shell</code>] option.
 
For example, to set user "myuser"'s shell to [[fish]]:
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
users.users.yourname.shell = pkgs.zsh;
users.users.myuser.shell = pkgs.fish;
</nowiki>}}  
</nowiki>}}


== Changing /bin/sh ==
You can also choose whether or not a user should use the default shell:
{{Warning|Please note that NixOS assumes all over the place that shell to be Bash, so override the default setting only if you know exactly 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 what shell the user is using. /bin/sh doesn't have to be the same as your interactive shell (e.g. the one you use in your terminal). In fact, a lot of 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.


To change your default POSIX shell on NixOS, use
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
<syntaxhighlight lang="nix">
users.users.myuser.useDefaultShell = true;
# Dash is just an example, you can use whatever you want
</nowiki>}}
environment.binsh = "${pkgs.dash}/bin/dash";
</syntaxhighlight>


== See also ==
== See also ==
* [[Fish]]
* [[Nushell]]
* [[Zsh]]
* [[Zsh]]
* [[Fish]]


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

Latest revision as of 08:21, 6 April 2024

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:

/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;

See also