{ config
, ... }: {
  programs.zsh = {
    enable = true;
    autocd = false;
    defaultKeymap = "emacs";
    localVariables.PROMPT = let
      op = {
        res = "%{$reset_color%}";
        col = color: "%{$fg[${color}]%}";
      };
      c = color: text: "${op.col color}${text}${op.res}";
    in "${c "green" "%n"}${c "cyan" "@"}${c "yellow" "%m"} ${c "magenta" "<3"} %~ ${c "blue" "$PARTICLE"}${c "red" "%#"} ";
    sessionVariables.LIBVIRT_DEFAULT_URI = "qemu:///system";

    history = {
      extended = true;
      path = "${config.home.homeDirectory}/.local/state/zsh/history";
      ignorePatterns = [
        "rm *" "reboot"
        "kill *" "killall *" "pkill *"
      ];
    };

    enableCompletion = true;
    initExtraBeforeCompInit = ''
      zstyle ':completion:*' auto-description '%d'
      zstyle ':completion:*' completer _expand _complete _ignored _approximate
      zstyle ':completion:*' format '%S%d%s'
      zstyle ':completion:*' group-name ""
      zstyle ':completion:*' glob 1
      zstyle ':completion:*' list-colors ""
      zstyle ':completion:*' list-prompt %S%p TAB to scroll, any other key to continue typing%s
      zstyle ':completion:*' matcher-list 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' 'l:|=* r:|=*' 'r:|[._-]=** r:|=**'
      zstyle ':completion:*' max-errors 5
      zstyle ':completion:*' menu select=0
      zstyle ':completion:*' prompt '%e errors'
      zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
      zstyle ':completion:*' substitute 1
    '';

    initExtraFirst = ''
      # enable colors for prompt
      autoload -U colors && colors

      # shell context indicator
      PARTICLE=""
      if [[ -v IN_NIX_SHELL ]]; then
        PARTICLE="$IN_NIX_SHELL "
      elif [[ $SHLVL != 1 ]]; then
        PARTICLE="nested "
      fi
    '';
    initExtra = ''
      # set misc zsh options
      setopt HIST_FIND_NO_DUPS
      unsetopt extendedglob
      unsetopt beep
      unsetopt notify
      unsetopt nomatch

      # set terminal title
      function precmd {
        echo -ne "\e]0;$USER@$HOST <3 ''${PWD/#"$HOME"/~} $PARTICLE\a"
      }

      # set up history search
      autoload -U history-search-end
      zle -N history-beginning-search-forward-end history-search-end
      bindkey '^[[B' history-beginning-search-forward-end
      bindkey '^[OB' history-beginning-search-forward-end
      zle -N history-beginning-search-backward-end history-search-end
      bindkey '^[[A' history-beginning-search-backward-end
      bindkey '^[OA' history-beginning-search-backward-end
    '';

    shellAliases = {
      gs = "git status";
      ls = "ls --color=auto";
      nix-shell = "nix-shell --run zsh";
    };
  };
}