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