about summary refs log tree commit diff
path: root/modules/user/aszlig/programs/zsh/default.nix
blob: 4876b865d3dafffb4c28106c1b8bfbf0cc0bc5f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{ config, lib, ... }:

with lib;

let
  cfg = config.vuizvui.user.aszlig.programs.zsh;
  inherit (cfg) machineColor;

in {
  options.vuizvui.user.aszlig.programs.zsh = {
    enable = lib.mkEnableOption "zsh";

    machineColor = lib.mkOption {
      type = lib.types.enum [
        "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"
      ];
      default = "red";
      example = "green";
      description = ''
        The color used for coloring the machine name in the prompt.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    environment.shellInit = ''
      export EDITOR="vim"
      export EMAIL="aszlig@nix.build"
    '';

    nixpkgs.overlays = lib.singleton (lib.const (super: {
      zsh = super.zsh.overrideAttrs (drv: {
        postConfigure = (drv.postConfigure or "") + ''
          sed -i -e '/^name=zsh\/newuser/d' config.modules
        '';
      });
    }));

    programs.zsh.enable = true;

    programs.zsh.shellAliases.t = "task";
    programs.zsh.shellAliases.p = "gopass";

    programs.zsh.setOptions = lib.mkForce [
      "auto_cd"
      "auto_pushd"
      "beep"
      "correct"
      "dvorak"
      "extended_glob"
      "extended_history"
      "hist_fcntl_lock"
      "hist_ignore_dups"
      "hist_no_store"
      "hist_reduce_blanks"
      "interactive_comments"
    ];

    programs.zsh.interactiveShellInit = let
      # This is to make sure that all themes use either color names or RGB
      # colors, otherwise they're not displayed correctly on 24bit color terms.
      highlighter = pkgs.zsh-fast-syntax-highlighting.overrideAttrs (drv: {
        fixThemeColors = pkgs.writers.writePython3 "fix-theme-colors" {
          libraries = [ pkgs.python3Packages.plumbum ];
          flakeIgnore = [ "E111" "E121" "E302" "E305" ];
        } ''
          import re
          from glob import glob
          from plumbum.colorlib.styles import Color
          from configparser import RawConfigParser

          def fix_color(color: str) -> str:
            if color[:2] in ('fg', 'bg') and color[2:3] in ':=':
              return color[:3] + fix_color(color[3:])
            return Color(int(color)).hex_code if color.isdigit() else color

          mainfile = re.sub(
            r'^(:\s+\''${FAST_HIGHLIGHT_STYLES\[[^]]+\]:=)([^}]+)',
            lambda m: m.group(1) + fix_color(m.group(2)),
            open('fast-highlight').read(),
            flags=re.MULTILINE
          )
          open('fast-highlight', 'w').write(mainfile)

          for themefile in glob('themes/*.ini'):
            parser = RawConfigParser(strict=False)
            parser.read(themefile)

            for name, section in parser.items():
              for key, color in section.items():
                section[key] = ','.join(fix_color(c) for c in color.split(','))

            with open(themefile, 'w') as fp:
              parser.write(fp)
        '';
        postPatch = (drv.postPatch or "") + ''
          "$fixThemeColors"
        '';
      });
    in lib.mkAfter ''
      export HISTFILE=~/.histfile
      export HISTSIZE=100000
      export SAVEHIST=100000
      export KEYTIMEOUT=1

      bindkey -v
      if [[ "$TERM" = xterm* ]]; then
        bindkey -v '\e[H' vi-beginning-of-line
        bindkey -v '\e[F' vi-end-of-line

        function set-title() {
          echo -en "\e]2;$2\a"
        }

        function reset-title() {
          echo -en "\e]2;''${(%):-%~}\a\a"
        }

        autoload -Uz add-zsh-hook
        add-zsh-hook preexec set-title
        add-zsh-hook precmd reset-title
      else
        bindkey -v '\e[1~' vi-beginning-of-line
        bindkey -v '\e[4~' vi-end-of-line
      fi

      bindkey -a '/' history-incremental-pattern-search-backward
      bindkey -a '?' history-incremental-pattern-search-forward
      bindkey '\e[A' up-line-or-history
      bindkey '\e[B' down-line-or-history

      zstyle ':completion:*' completer _expand _complete _ignored _approximate
      zstyle ':completion:*' expand prefix suffix
      zstyle ':completion:*' group-name '''
      zstyle ':completion:*' insert-unambiguous true
      zstyle ':completion:*' list-colors '''
      zstyle ':completion:*' list-prompt \
        %SAt %p: Hit TAB for more, or the character to insert%s
      zstyle ':completion:*' list-suffixes true
      zstyle ':completion:*' matcher-list ''' \
        'm:{[:lower:]}={[:upper:]}' \
        'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' \
        'l:|=* r:|=*' \
        'r:|[._-]=** r:|=**'
      zstyle ':completion:*' max-errors 2 numeric
      zstyle ':completion:*' menu select=long
      zstyle ':completion:*' original true
      zstyle ':completion:*' preserve-prefix '//[^/]##/'
      zstyle ':completion:*' prompt \
        'Hm, did you mistype something? There are %e errors in the completion.'
      zstyle ':completion:*' select-prompt \
        %SScrolling active: current selection at %p%s
      zstyle ':completion:*' use-compctl false
      zstyle ':completion:*' verbose true

      autoload -Uz zmv

      source ${lib.escapeShellArg "${highlighter}/${
        "share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh"
      }"}
    '';

    programs.zsh.promptInit = ''
      autoload -Uz prompt_special_chars

      () {
          local p_machine='%(!..%B%F{red}%n%b%F{blue}@)%b%F{${machineColor}}%m'
          local p_path='%B%F{blue}[%F{cyan}%~%B%F{blue}]'
          local p_exitcode='%F{green}%?%(!.%F{cyan}>.%b%F{green}>)%b%f '
          PROMPT="$p_machine$p_path$p_exitcode"
      }
    '';
  };
}