Zsh: Difference between revisions

From NixOS Wiki
imported>FMGordillo
m Updated `zsh.history` for the current one
imported>OctylFractal
correct oh-my-zsh config name
Line 44: Line 44:
programs.zsh = {
programs.zsh = {
   ... # Your zsh config
   ... # Your zsh config
  oh-my-zsh = {
  ohMyZsh = {
     enable = true;
     enable = true;
     plugins = [ "git" "thefuck" ];
     plugins = [ "git" "thefuck" ];

Revision as of 04:55, 18 September 2023

Zsh is a powerful shell that operates as both an interactive shell and as a scripting language interpreter. While being compatible with the POSIX sh (not by default, only if issuing emulate sh), it offers advantages such as improved tab completion and globbing.

The Zsh FAQ offers more reasons to use Zsh.

Installation

See Command Shell.

Configuration

Zsh can be configured with Home Manager.

Full list of home-manager options for zsh can be found here.

Example Configuration

~/.config/nixpkgs/home.nix
programs.zsh = {
  enable = true;
  shellAliases = {
    ll = "ls -l";
    update = "sudo nixos-rebuild switch";
  };
  histSize = 10000;
  histFile = "${config.xdg.dataHome}/zsh/history";
};

Plugin Management

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

Zplug

~/.config/nixpkgs/home.nix
programs.zsh = {
  ... # Your zsh config
  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.
    ];
  };
};

Oh-My-Zsh

~/.config/nixpkgs/home.nix
programs.zsh = {
  ... # Your zsh config
 ohMyZsh = {
    enable = true;
    plugins = [ "git" "thefuck" ];
    theme = "robbyrussell";
  };
};

Manual

~/.config/nixpkgs/home.nix
programs.zsh = {
  ... # Your zsh config
  plugins = [
    {
      # will source zsh-autosuggestions.plugin.zsh
      name = "zsh-autosuggestions";
      src = pkgs.fetchFromGitHub {
        owner = "zsh-users";
        repo = "zsh-autosuggestions";
        rev = "v0.4.0";
        sha256 = "0z6i9wjjklb4lvr7zjhbphibsyx51psv50gm07mbb0kj9058j6kc";
      };
    }
    {
      name = "enhancd";
      file = "init.sh";
      src = pkgs.fetchFromGitHub {
        owner = "b4b4r07";
        repo = "enhancd";
        rev = "v2.2.1";
        sha256 = "0iqa9j09fwm6nj5rpip87x3hnvbbz9w9ajgm6wkrd5fls8fn8i5g";
      };
    }
  ];
};

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

See also