Zsh

From NixOS Wiki
Revision as of 04:57, 29 July 2024 by YoshiRulz (talk | contribs) (→‎How to use plugins?: Fix syntax error)

Zsh is a powerful Unix shell that functions both as an interactive shell and a scripting language interpreter. It extends the Bourne Shell (sh) with features from bash, ksh, and tcsh, offering advanced tab completion, improved globbing, and extensive customization options. Though not POSIX sh-compatible by default, it can be configured to be so with emulate sh.

Key features include highly customizable prompts, enhanced command history, spelling correction, and robust job control. The Oh My Zsh framework simplifies managing plugins and themes. Zsh is cross-platform, available on Unix-like systems including Linux and macOS, and is popular among developers and system administrators for its advanced features and user-friendly enhancements.

The Zsh FAQ offers more reasons to use Zsh.

Installation

See Command Shell.

Configuration

Basic

programs.zsh = {
    enable = true;
};

Advanced

The configuration below is using home manager, but a more limited version of it can be achieved if system-wide.

programs.zsh = {
    enable = true;
    enableCompletion = true;
    autosuggestions.enable = true;
    syntaxHighlighting.enable = true;

    shellAliases = {
      ll = "ls -l";
      edit = "sudo -e";
      update = "sudo nixos-rebuild switch";
    };

    history.size = 10000;
    history.ignoreAllDups = true;
    history.path = "$HOME/.zsh_history";
    history.ignorePatterns = ["rm *" "pkill *" "cp *"];
};

Tips and Tricks

Where to see a list of options?

The home manager options are defined in the following Home Manager Options Manual.

The system-wide options are listed on MyNixOS.

How to use plugins?

Home manager has three ways of managing plugins: Zplug, Oh-My-Zsh and Manual.

programs.zsh = {
  enable = true;

  zplug = {
    enable = true;
    plugins = [
      {name = "zsh-users/zsh-autosuggestions";} # Simple plugin installation
      {
        name = "romkatv/powerlevel10k";
        tags = [ "as:theme" "depth:1" ];
      } # Installations with additional options. For the list of options, please refer to Zplug README.
    ];
  };

  ohMyZsh = {
    enable = true;
    plugins = ["git" "thefuck"];
    theme = "robbyrussell";
  };

  plugins = [
    {
      name = "zsh-autocomplete";
      src = pkgs.fetchFromGitHub {
        owner = "marlonrichert";
        repo = "zsh-autocomplete";
        rev = "23.07.13";
        sha256 = "sha256-/6V6IHwB5p0GT1u5SAiUa20LjFDSrMo731jFBq/bnpw=";
      };
    }
    {
      name = "powerlevel10k";
      src = pkgs.zsh-powerlevel10k;
      file = "share/zsh-powerlevel10k/powerlevel10k.zsh-theme";
    }
    {
      name = "powerlevel10k-config";
      src = ./p10k-config;
      file = "p10k.zsh";
    }
    {
      name = "zsh-syntax-highlighting";
      src = pkgs.fetchFromGitHub {
        owner = "zsh-users";
        repo = "zsh-syntax-highlighting";
        rev = "0.8.0";
        sha256 = "sha256-iJdWopZwHpSyYl5/FQXEW7gl/SrKaYDEtTH9cGP7iPo=";
      };
    }
  ];
};

Troubleshooting

Zsh-autocomplete not working

You may have some issues with the marlonrichert/zsh-autocomplete plugin on NixOS. That's because the default NixOS configuration overrides keybinds for up and down arrow keys. To fix this issue, you need to add this somewhere in your .zshrc (either manually if your .zshrc is not managed by Nix, or with packages.zsh.initExtra)

bindkey "''${key[Up]}" up-line-or-search

SHA Mismatch during manual plugin installation

If manual plugin installation fails with SHA mismatch, generating a valid hash as part of the error message can be achieved by temporarily switching to:

sha256 = lib.fakeSha256;

This will print a valid SHA to the console and then can be used as final value for the sha256 field. Redoing this is mandatory if one wants to update to a newer commit of the targeted plugin repository.

GDM does not show user when zsh is the default shell

GDM only shows users that have their default shell set to a shell listed in /etc/shells. Setting the default shell using the following does not update /etc/shells.

users.defaultUserShell = pkgs.zsh;

To add the zsh package to /etc/shells you must update environment.shells.

environment.shells = with pkgs; [ zsh ];

References

  1. https://www.zsh.org/
  2. http://zsh.sourceforge.net/Guide/zshguide06.html
  3. http://zsh.sourceforge.net/Doc/Release/Expansion.html
  4. http://zsh.sourceforge.net/FAQ/zshfaq01.html#l4
  5. https://nix-community.github.io/home-manager/options.xhtml#opt-programs.zsh.enable
  6. https://mynixos.com/search?q=zsh