blob: c3921880cc97b0648e5823d91440322766eb21ec (
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
|
{ config, lib, pkgs, ... }:
with lib;
let
inherit (pkgs) stdenv;
cfg = config.programs.tmux;
tmux = pkgs.runCommand pkgs.tmux.name
{ buildInputs = [ pkgs.makeWrapper ]; }
''
source $stdenv/setup
mkdir -p $out/bin
makeWrapper ${pkgs.tmux}/bin/tmux $out/bin/tmux \
--set __ETC_BASHRC_SOURCED "" \
--set __ETC_ZPROFILE_SOURCED "" \
--set __ETC_ZSHENV_SOURCED "" \
--set __ETC_ZSHRC_SOURCED "" \
--add-flags -f --add-flags /etc/tmux.conf
'';
text = import ../lib/write-text.nix {
inherit lib;
mkTextDerivation = name: text: pkgs.writeText "tmux-options-${name}" text;
};
tmuxOptions = concatMapStringsSep "\n" (attr: attr.text) (attrValues cfg.tmuxOptions);
fzfTmuxSession = pkgs.writeScript "fzf-tmux-session" ''
#! ${stdenv.shell}
set -e
session=$(tmux list-sessions -F '#{session_name}' | fzf --query="$1" --exit-0)
tmux switch-client -t "$session"
'';
in {
options = {
programs.tmux.enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to configure tmux.
'';
};
programs.tmux.enableSensible = mkOption {
type = types.bool;
default = false;
example = true;
description = ''
Enable sensible configuration options for tmux.
'';
};
programs.tmux.enableMouse = mkOption {
type = types.bool;
default = false;
example = true;
description = ''
Enable mouse support for tmux.
'';
};
programs.tmux.enableFzf = mkOption {
type = types.bool;
default = false;
example = true;
description = ''
Enable fzf keybindings for selecting tmux sessions and panes.
'';
};
programs.tmux.enableVim = mkOption {
type = types.bool;
default = false;
example = true;
description = ''
Enable vim style keybindings for copy mode, and navigation of tmux panes.
'';
};
programs.tmux.tmuxOptions = mkOption {
internal = true;
type = types.attrsOf (types.submodule text);
default = {};
};
programs.tmux.tmuxConfig = mkOption {
type = types.lines;
default = "";
};
};
config = mkIf cfg.enable {
environment.systemPackages =
[ # Include wrapped tmux package.
tmux
];
environment.etc."tmux.conf".text = ''
${tmuxOptions}
${cfg.tmuxConfig}
source-file -q /etc/tmux.conf.local
'';
programs.tmux.tmuxOptions.login-shell.text = if stdenv.isDarwin then ''
set -g default-command "${pkgs.reattach-to-user-namespace}/bin/reattach-to-user-namespace ${config.environment.loginShell}"
'' else ''
set -g default-command "${config.environment.loginShell}"
'';
programs.tmux.tmuxOptions.sensible.text = mkIf cfg.enableSensible ''
set -g default-terminal "screen-256color"
setw -g aggressive-resize on
set -g base-index 1
set -g renumber-windows on
set -g status-keys emacs
set -s escape-time 0
bind c new-window -c '#{pane_current_path}'
bind % split-window -v -c '#{pane_current_path}'
bind '"' split-window -h -c '#{pane_current_path}'
# TODO: make these interactive
bind C new-session
bind S switch-client -l
# set -g status-utf8 on
# set -g utf8 on
'';
programs.tmux.tmuxOptions.mouse.text = mkIf cfg.enableMouse ''
set -g mouse on
setw -g mouse on
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
'';
programs.tmux.tmuxOptions.fzf.text = mkIf cfg.enableFzf ''
bind-key -n M-p run "tmux split-window -p 40 -c '#{pane_current_path}' 'tmux send-keys -t #{pane_id} \"$(fzf -m | paste -sd\\ -)\"'"
bind-key -n M-s run "tmux split-window -p 40 'tmux send-keys -t #{pane_id} \"$(${fzfTmuxSession})\"'"
'';
programs.tmux.tmuxOptions.vim.text = mkIf cfg.enableVim (''
setw -g mode-keys vi
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind s split-window -v -c '#{pane_current_path}'
bind v split-window -h -c '#{pane_current_path}'
bind -t vi-copy v begin-selection
'' + optionalString stdenv.isLinux ''
bind -t vi-copy y copy-selection
'' + optionalString stdenv.isDarwin ''
bind -t vi-copy y copy-pipe "${pkgs.reattach-to-user-namespace}/bin/reattach-to-user-namespace pbcopy"
'');
};
}
|