summary refs log tree commit diff stats
path: root/user/settings/zsh.nix
blob: 203cdb6d55d11c272d858204a2edded6bd1af087 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{ config, lib, pkgs, ... }:

let
  inherit (pkgs) stdenv;
  lsOptions = if stdenv.isDarwin then "-p" else "-v --group-directories-first";
  lsIsoDate = if stdenv.isDarwin then "" else "--time-style=long-iso";
  zshrc = ".config/zsh/.zshrc";
  mkZshPlugin = attrs: {
    name = attrs.name;
    src = stdenv.mkDerivation {
      inherit (attrs) src;
      name = "zsh-plugin-${attrs.name}";
      buildInputs = [ pkgs.zsh ];
      buildPhase = ''
        zsh -c 'for f in **/*; zcompile "$f"'
      '';
      installPhase = ''
        cp -a $PWD $out/
      '';
    };
  };
in
{
  home.packages = with pkgs; [
    fzf
    ghq
    git
    git-lfs
    zsh-completions
  ];

  home.activation.linkOldZshHistory = config.lib.dag.entryAfter ["writeBoundary"] ''
    if [[ ! -f ${config.home.sessionVariables.XDG_DATA_HOME}/zsh/history ]]; then
      mkdir -p ${config.home.sessionVariables.XDG_DATA_HOME}/zsh/
      ln ~/.cache/zsh/history ${config.home.sessionVariables.XDG_DATA_HOME}/zsh/history
    fi
  '';

  programs.zsh = {
    enable = true;

    enableAutosuggestions = true;
    enableCompletion = true;
    defaultKeymap = "emacs";

    dotDir = ".config/zsh";

    history = {
      expireDuplicatesFirst = true;
      extended = true;
      path = "\${XDG_DATA_HOME#$HOME/}/zsh/history";
      save = 20000;
      size = 10000;
      share = false;
    };

    localVariables = {
      ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE = "fg=8";
    };

    plugins = (map mkZshPlugin [
      {
        name = "cd-gitroot";
        src = pkgs.fetchFromGitHub {
          owner = "mollifier";
          repo = "cd-gitroot";
          rev = "a1d59511eba2b99935635b2efef00f8d2363aecd";
          # date = 2016-03-23T18:29:15+09:00;
          sha256 = "1l0fb24vcgd7vgya1r47h2bfczkxrb3mxn791l305f5d350wvqfz";
        };
      }
      {
        name = "calc";
        src = pkgs.fetchFromGitHub {
          owner = "arzzen";
          repo = "calc.plugin.zsh";
          rev = "ea59bc2bfd1d2791de539c31c0c544d4dc16dad6";
          # date = 2020-02-05T09:22:01+00:00;
          sha256 = "0as30w1na8idrkxifim44ky0521r93r2qm201qnnl6by2qbmbiqv";
        };
      }
      {
        name = "zsh-nix-shell";
        src = pkgs.fetchFromGitHub {
          owner = "chisui";
          repo = "zsh-nix-shell";
          rev = "69e90b9bccecd84734948fb03087c2454a8522f6";
          # date = 2020-04-08T00:11:55+02:00;
          sha256 = "0snhch9hfy83d4amkyxx33izvkhbwmindy0zjjk28hih1a9l2jmx";
        };
      }
      {
        name = "you-should-use";
        src = pkgs.fetchFromGitHub {
          owner = "MichaelAquilina";
          repo = "zsh-you-should-use";
          rev = "888da3af69162c927e0f933ab8dc12395a52ad67";
          # date = 2020-01-19T20:28:40+00:00;
          sha256 = "1gcxm08ragwrh242ahlq3bpfg5yma2cshwdlj8nrwnd4qwrsflgq";
        };
      }
      {
        name = "agkozak-zsh-prompt";
        src = pkgs.fetchFromGitHub {
          owner = "agkozak";
          repo = "agkozak-zsh-prompt";
          rev = "83ec46e7d0af4b7aef64919ed3551d7da789436c";
          # date = 2020-01-24T19:54:05-08:00;
          sha256 = "0kq7qvwkbkx18k4wxcnc76xnsrfn32k8dykvrsmmz8y9f81zhnpz";
        };
      }
      {
        name = "anyframe";
        src = pkgs.fetchFromGitHub {
          owner = "mollifier";
          repo = "anyframe";
          rev = "598675303044df8e9d04722f3adff4f63a238922";
          # date = 2017-07-19T21:59:49+09:00;
          sha256 = "08bjm1dd2mpv8rk8x6yvm6gj490rgimmiq7ln4jr5hik2k3mm82r";
        };
      }
    ] ++ lib.optionals stdenv.isDarwin [
      {
        name = "tumult";
        src = pkgs.fetchFromGitHub {
          owner = "unixorn";
          repo = "tumult.plugin.zsh";
          rev = "e74db60be21dfebaa89b84fb9a45939814573c8c";
          # date = 2019-03-07T08:53:24-07:00;
          sha256 = "1328g3h51478ai25niv9qgf8bjgg3cw98zdqfznrbgw0k3qj66zk";
        };
      }
    ]);

    shellAliases = {
      l = "ls ${lsOptions} -Bp";
      l1="ls -1";
      ls="ls ${lsOptions} -hF";
      la="ls ${lsOptions} -hA";
      ll="ls ${lsOptions} ${lsIsoDate} -hl";
      lal="ll -A";
      lla="lal";
      llr="ll -t";

      cg = "cd-gitroot";
      cdg = "cd-gitroot";

      https = "http --default-scheme https";

      history = "fc -l $(( $LINES - 2 ))";
      hist-freq-lines = "fc -l -10000 | cut -d' ' -f4- | sort | uniq -c | sort -g | tail -n100 | less";
      hist-freq-commands = "fc -l -10000 | cut -d' ' -f4 | sort | uniq -c | sort -g | tail -n10 | less";
      wprop = "xprop | egrep '^WM_(CLASS|NAME|WINDOW_ROLE|TYPE)'";

      # Enable the following commands to support aliases.
      sudo = "sudo ";
      watch = "watch ";

      d = "docker";
      db = "docker build";
      dr = "docker run";
      di = "docker image";
      dj = "docker pull";
      dk = "docker push";

      dcb = "dc build";
      dcd = "dc down";
      dcj = "dc pull";
      dck = "dc push";
      dcl = "dc logs";
      dclf = "dc logs -f";
      dcu = "dc up";
      dcud = "dc up -d";
      dcr = "dc restart";

      g = "rg";

      ga = "git add";
      gs = "git st";
      gd = "git diff";
      gf = "git fetch";
      gk = "git push";
      gkf = "git push --force-with-lease";
      gj = "git pull";
      gl = "git lg";
      gm = "git merge";
      ge = "git remote";
      gr = "git rebase";
      gz = "git stash";
      gls = "git ls-files";
      gzl = "git stash list";
      gzp = "git stash pop";
      gdt = "git difftool";
      grl = "git reflog";
      gri = "git rebase --interactive";
      gfa = "git fetch --all";
      grs = "git reset";
      grsh = "git reset --hard";
      gsh = "git show";
      gsm = "git submodule";
      gci = "git commit";
      gx = "git restore";
      gb = "git switch";
      gbr = "git br";
      gbrc = "git checkout -b";
      gbrd = "git branch --delete";
      gbrm = "git branch --move";
      gmup = "git mup";
      grup = "git rup";

      hos = "home-manager switch";
      hon = "home-manager news";
      hoh = "home-manager-help";

      nish  = "nix-shell";
      nic = "nix-channel";
      nica = "nix-channel --add";
      nicl = "nix-channel --list";
      nicu = "nix-channel --update";
      nicr = "nix-channel --remove";
      nicb = "nix-channel --rollback";
      snic = "sudo nix-channel";
      snica = "sudo nix-channel --add";
      snicl = "sudo nix-channel --list";
      snicu = "sudo nix-channel --update";
      snicr = "sudo nix-channel --remove";
      snicb = "sudo nix-channel --rollback";
      n = "nix-env";
      ni = "nix-env -iA";
      nq = "nix-env -q";
      ne = "nix-env -e";
      nup = "nix-env -u";
      nlg = "nix-env --list-generations";
      snlg = "sudo nix-env --list-generations --profile /nix/var/nix/profiles/system";
      ngc = "nix-collect-garbage --delete-older-than 14d";
      sngc = "sudo nix-collect-garbage --delete-older-than 14d";
    };

    envExtra = ''
      if [[ ''${path[(I)$HOME/.local/bin ]} ]]
      then
        path=($HOME/.local/bin $path)
      fi

      if [[ ''${path[(I)$HOME/go/bin ]} ]]
      then
        path+=($HOME/go/bin)
      fi
    '';

    initExtra = ''
      typeset -T GHQ_ROOT ghq_root
      export GHQ_ROOT="$HOME/projects:$HOME/go/src:$HOME/quicklisp/local-projects"

      function hist-freq-subcommands () {
        fc -l -m "$1*" -10000 | cut -d' ' -f4- | sort | uniq -c | sort -g | tail -n100 | less
      }

      source ${pkgs.fzf}/share/fzf/key-bindings.zsh

    '' + builtins.readFile ../zsh/zshrc;
  };

  home.file."${zshrc}".onChange = "${pkgs.zsh}/bin/zsh -i -c 'autoload -Uz compinit && compinit && zcompile ${zshrc}'";
}