fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
10.2 KiB345 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.tmux;
8
9  pluginName = p: if types.package.check p then p.pname else p.plugin.pname;
10
11  pluginModule = types.submodule {
12    options = {
13      plugin = mkOption {
14        type = types.package;
15        description = "Path of the configuration file to include.";
16      };
17
18      extraConfig = mkOption {
19        type = types.lines;
20        description = "Additional configuration for the associated plugin.";
21        default = "";
22      };
23    };
24  };
25
26  defaultKeyMode = "emacs";
27  defaultResize = 5;
28  defaultShortcut = "b";
29  defaultTerminal = "screen";
30  defaultShell = null;
31
32  boolToStr = value: if value then "on" else "off";
33
34  tmuxConf = ''
35    ${optionalString cfg.sensibleOnTop ''
36      # ============================================= #
37      # Start with defaults from the Sensible plugin  #
38      # --------------------------------------------- #
39      run-shell ${pkgs.tmuxPlugins.sensible.rtp}
40      # ============================================= #
41    ''}
42    set  -g default-terminal "${cfg.terminal}"
43    set  -g base-index      ${toString cfg.baseIndex}
44    setw -g pane-base-index ${toString cfg.baseIndex}
45    ${optionalString (cfg.shell != null) ''
46      # We need to set default-shell before calling new-session
47      set  -g default-shell "${cfg.shell}"
48    ''}
49    ${optionalString cfg.newSession "new-session"}
50
51    ${optionalString cfg.reverseSplit ''
52      bind -N "Split the pane into two, left and right" v split-window -h
53      bind -N "Split the pane into two, top and bottom" s split-window -v
54    ''}
55
56    set -g status-keys ${cfg.keyMode}
57    set -g mode-keys   ${cfg.keyMode}
58
59    ${optionalString
60    (cfg.keyMode == "vi" && cfg.customPaneNavigationAndResize) ''
61      bind -N "Select pane to the left of the active pane" h select-pane -L
62      bind -N "Select pane below the active pane" j select-pane -D
63      bind -N "Select pane above the active pane" k select-pane -U
64      bind -N "Select pane to the right of the active pane" l select-pane -R
65
66      bind -r -N "Resize the pane left by ${toString cfg.resizeAmount}" \
67        H resize-pane -L ${toString cfg.resizeAmount}
68      bind -r -N "Resize the pane down by ${toString cfg.resizeAmount}" \
69        J resize-pane -D ${toString cfg.resizeAmount}
70      bind -r -N "Resize the pane up by ${toString cfg.resizeAmount}" \
71        K resize-pane -U ${toString cfg.resizeAmount}
72      bind -r -N "Resize the pane right by ${toString cfg.resizeAmount}" \
73        L resize-pane -R ${toString cfg.resizeAmount}
74    ''}
75
76    ${if cfg.prefix != null then ''
77      # rebind main key: ${cfg.prefix}
78      unbind C-${defaultShortcut}
79      set -g prefix ${cfg.prefix}
80      bind -N "Send the prefix key through to the application" \
81        ${cfg.prefix} send-prefix
82    '' else
83      optionalString (cfg.shortcut != defaultShortcut) ''
84        # rebind main key: C-${cfg.shortcut}
85        unbind C-${defaultShortcut}
86        set -g prefix C-${cfg.shortcut}
87        bind -N "Send the prefix key through to the application" \
88          ${cfg.shortcut} send-prefix
89        bind C-${cfg.shortcut} last-window
90      ''}
91
92    ${optionalString cfg.disableConfirmationPrompt ''
93      bind-key -N "Kill the current window" & kill-window
94      bind-key -N "Kill the current pane" x kill-pane
95    ''}
96
97    set  -g mouse             ${boolToStr cfg.mouse}
98    setw -g aggressive-resize ${boolToStr cfg.aggressiveResize}
99    setw -g clock-mode-style  ${if cfg.clock24 then "24" else "12"}
100    set  -s escape-time       ${toString cfg.escapeTime}
101    set  -g history-limit     ${toString cfg.historyLimit}
102  '';
103
104  configPlugins = {
105    assertions = [
106      (let
107        hasBadPluginName = p: !(hasPrefix "tmuxplugin" (pluginName p));
108        badPlugins = filter hasBadPluginName cfg.plugins;
109      in {
110        assertion = badPlugins == [ ];
111        message = ''Invalid tmux plugin (not prefixed with "tmuxplugins"): ''
112          + concatMapStringsSep ", " pluginName badPlugins;
113      })
114    ];
115
116    xdg.configFile."tmux/tmux.conf".text = ''
117      # ============================================= #
118      # Load plugins with Home Manager                #
119      # --------------------------------------------- #
120
121      ${(concatMapStringsSep "\n\n" (p: ''
122        # ${pluginName p}
123        # ---------------------
124        ${p.extraConfig or ""}
125        run-shell ${if types.package.check p then p.rtp else p.plugin.rtp}
126      '') cfg.plugins)}
127      # ============================================= #
128    '';
129  };
130
131in {
132  options = {
133    programs.tmux = {
134      aggressiveResize = mkOption {
135        default = false;
136        type = types.bool;
137        description = ''
138          Resize the window to the size of the smallest session for
139          which it is the current window.
140        '';
141      };
142
143      baseIndex = mkOption {
144        default = 0;
145        example = 1;
146        type = types.ints.unsigned;
147        description = "Base index for windows and panes.";
148      };
149
150      clock24 = mkOption {
151        default = false;
152        type = types.bool;
153        description = "Use 24 hour clock.";
154      };
155
156      customPaneNavigationAndResize = mkOption {
157        default = false;
158        type = types.bool;
159        description = ''
160          Override the hjkl and HJKL bindings for pane navigation and
161          resizing in VI mode.
162        '';
163      };
164
165      disableConfirmationPrompt = mkOption {
166        default = false;
167        type = types.bool;
168        description = ''
169          Disable confirmation prompt before killing a pane or window
170        '';
171      };
172
173      enable = mkEnableOption "tmux";
174
175      escapeTime = mkOption {
176        default = 500;
177        example = 0;
178        type = types.ints.unsigned;
179        description = ''
180          Time in milliseconds for which tmux waits after an escape is
181          input.
182        '';
183      };
184
185      extraConfig = mkOption {
186        type = types.lines;
187        default = "";
188        description = ''
189          Additional configuration to add to
190          {file}`tmux.conf`.
191        '';
192      };
193
194      historyLimit = mkOption {
195        default = 2000;
196        example = 5000;
197        type = types.ints.positive;
198        description = "Maximum number of lines held in window history.";
199      };
200
201      keyMode = mkOption {
202        default = defaultKeyMode;
203        example = "vi";
204        type = types.enum [ "emacs" "vi" ];
205        description = "VI or Emacs style shortcuts.";
206      };
207
208      mouse = mkEnableOption "mouse support";
209
210      newSession = mkOption {
211        default = false;
212        type = types.bool;
213        description = ''
214          Automatically spawn a session if trying to attach and none
215          are running.
216        '';
217      };
218
219      package = mkOption {
220        type = types.package;
221        default = pkgs.tmux;
222        defaultText = literalExpression "pkgs.tmux";
223        example = literalExpression "pkgs.tmux";
224        description = "The tmux package to install";
225      };
226
227      reverseSplit = mkOption {
228        default = false;
229        type = types.bool;
230        description = "Reverse the window split shortcuts.";
231      };
232
233      resizeAmount = mkOption {
234        default = defaultResize;
235        example = 10;
236        type = types.ints.positive;
237        description = "Number of lines/columns when resizing.";
238      };
239
240      sensibleOnTop = mkOption {
241        type = types.bool;
242        default = true;
243        description = ''
244          Run the sensible plugin at the top of the configuration. It
245          is possible to override the sensible settings using the
246          {option}`programs.tmux.extraConfig` option.
247        '';
248      };
249
250      prefix = mkOption {
251        default = null;
252        example = "C-a";
253        type = types.nullOr types.str;
254        description = ''
255          Set the prefix key. Overrules the "shortcut" option when set.
256        '';
257      };
258
259      shortcut = mkOption {
260        default = defaultShortcut;
261        example = "a";
262        type = types.str;
263        description = ''
264          CTRL following by this key is used as the main shortcut.
265        '';
266      };
267
268      terminal = mkOption {
269        default = defaultTerminal;
270        example = "screen-256color";
271        type = types.str;
272        description = "Set the $TERM variable.";
273      };
274
275      shell = mkOption {
276        default = defaultShell;
277        example = "\${pkgs.zsh}/bin/zsh";
278        type = with types; nullOr str;
279        description = "Set the default-shell tmux variable.";
280      };
281
282      secureSocket = mkOption {
283        default = pkgs.stdenv.isLinux;
284        type = types.bool;
285        description = ''
286          Store tmux socket under {file}`/run`, which is more
287          secure than {file}`/tmp`, but as a downside it doesn't
288          survive user logout.
289        '';
290      };
291
292      tmuxp.enable = mkEnableOption "tmuxp";
293
294      tmuxinator.enable = mkEnableOption "tmuxinator";
295
296      plugins = mkOption {
297        type = with types;
298          listOf (either package pluginModule) // {
299            description = "list of plugin packages or submodules";
300          };
301        description = ''
302          List of tmux plugins to be included at the end of your tmux
303          configuration. The sensible plugin, however, is defaulted to
304          run at the top of your configuration.
305        '';
306        default = [ ];
307        example = literalExpression ''
308          with pkgs; [
309            tmuxPlugins.cpu
310            {
311              plugin = tmuxPlugins.resurrect;
312              extraConfig = "set -g @resurrect-strategy-nvim 'session'";
313            }
314            {
315              plugin = tmuxPlugins.continuum;
316              extraConfig = '''
317                set -g @continuum-restore 'on'
318                set -g @continuum-save-interval '60' # minutes
319              ''';
320            }
321          ]
322        '';
323      };
324    };
325  };
326
327  config = mkIf cfg.enable (mkMerge ([
328    {
329      home.packages = [ cfg.package ]
330        ++ optional cfg.tmuxinator.enable pkgs.tmuxinator
331        ++ optional cfg.tmuxp.enable pkgs.tmuxp;
332    }
333
334    { xdg.configFile."tmux/tmux.conf".text = mkBefore tmuxConf; }
335    { xdg.configFile."tmux/tmux.conf".text = mkAfter cfg.extraConfig; }
336
337    (mkIf cfg.secureSocket {
338      home.sessionVariables = {
339        TMUX_TMPDIR = ''''${XDG_RUNTIME_DIR:-"/run/user/$(id -u)"}'';
340      };
341    })
342
343    (mkIf (cfg.plugins != [ ]) configPlugins)
344  ]));
345}