1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-30 06:59:45 +01:00

tmux: Add extraConfigBeforePlugins to tmux.config

Adds the possibility to add config before the plugins are loaded
This commit is contained in:
Gabriel Nützi 2023-11-17 19:01:55 +01:00
parent 898a1cef2d
commit a163b13a28
3 changed files with 117 additions and 70 deletions

View file

@ -1,9 +1,16 @@
{ config, lib, pkgs, ... }: {
with lib; config,
let lib,
pkgs,
...
}:
with lib; let
cfg = config.programs.tmux; cfg = config.programs.tmux;
pluginName = p: if types.package.check p then p.pname else p.plugin.pname; pluginName = p:
if types.package.check p
then p.pname
else p.plugin.pname;
pluginModule = types.submodule { pluginModule = types.submodule {
options = { options = {
@ -26,7 +33,10 @@ let
defaultTerminal = "screen"; defaultTerminal = "screen";
defaultShell = null; defaultShell = null;
boolToStr = value: if value then "on" else "off"; boolToStr = value:
if value
then "on"
else "off";
tmuxConf = '' tmuxConf = ''
${optionalString cfg.sensibleOnTop '' ${optionalString cfg.sensibleOnTop ''
@ -70,13 +80,16 @@ let
L resize-pane -R ${toString cfg.resizeAmount} L resize-pane -R ${toString cfg.resizeAmount}
''} ''}
${if cfg.prefix != null then '' ${
if cfg.prefix != null
then ''
# rebind main key: ${cfg.prefix} # rebind main key: ${cfg.prefix}
unbind C-${defaultShortcut} unbind C-${defaultShortcut}
set -g prefix ${cfg.prefix} set -g prefix ${cfg.prefix}
bind -N "Send the prefix key through to the application" \ bind -N "Send the prefix key through to the application" \
${cfg.prefix} send-prefix ${cfg.prefix} send-prefix
'' else ''
else
optionalString (cfg.shortcut != defaultShortcut) '' optionalString (cfg.shortcut != defaultShortcut) ''
# rebind main key: C-${cfg.shortcut} # rebind main key: C-${cfg.shortcut}
unbind C-${defaultShortcut} unbind C-${defaultShortcut}
@ -84,7 +97,8 @@ let
bind -N "Send the prefix key through to the application" \ bind -N "Send the prefix key through to the application" \
${cfg.shortcut} send-prefix ${cfg.shortcut} send-prefix
bind C-${cfg.shortcut} last-window bind C-${cfg.shortcut} last-window
''} ''
}
${optionalString cfg.disableConfirmationPrompt '' ${optionalString cfg.disableConfirmationPrompt ''
bind-key -N "Kill the current window" & kill-window bind-key -N "Kill the current window" & kill-window
@ -93,7 +107,11 @@ let
set -g mouse ${boolToStr cfg.mouse} set -g mouse ${boolToStr cfg.mouse}
setw -g aggressive-resize ${boolToStr cfg.aggressiveResize} setw -g aggressive-resize ${boolToStr cfg.aggressiveResize}
setw -g clock-mode-style ${if cfg.clock24 then "24" else "12"} setw -g clock-mode-style ${
if cfg.clock24
then "24"
else "12"
}
set -s escape-time ${toString cfg.escapeTime} set -s escape-time ${toString cfg.escapeTime}
set -g history-limit ${toString cfg.historyLimit} set -g history-limit ${toString cfg.historyLimit}
''; '';
@ -104,8 +122,9 @@ let
hasBadPluginName = p: !(hasPrefix "tmuxplugin" (pluginName p)); hasBadPluginName = p: !(hasPrefix "tmuxplugin" (pluginName p));
badPlugins = filter hasBadPluginName cfg.plugins; badPlugins = filter hasBadPluginName cfg.plugins;
in { in {
assertion = badPlugins == [ ]; assertion = badPlugins == [];
message = ''Invalid tmux plugin (not prefixed with "tmuxplugins"): '' message =
''Invalid tmux plugin (not prefixed with "tmuxplugins"): ''
+ concatMapStringsSep ", " pluginName badPlugins; + concatMapStringsSep ", " pluginName badPlugins;
}) })
]; ];
@ -119,8 +138,13 @@ let
# ${pluginName p} # ${pluginName p}
# --------------------- # ---------------------
${p.extraConfig or ""} ${p.extraConfig or ""}
run-shell ${if types.package.check p then p.rtp else p.plugin.rtp} run-shell ${
'') cfg.plugins)} if types.package.check p
then p.rtp
else p.plugin.rtp
}
'')
cfg.plugins)}
# ============================================= # # ============================================= #
''; '';
}; };
@ -206,7 +230,7 @@ in {
keyMode = mkOption { keyMode = mkOption {
default = defaultKeyMode; default = defaultKeyMode;
example = "vi"; example = "vi";
type = types.enum [ "emacs" "vi" ]; type = types.enum ["emacs" "vi"];
description = "VI or Emacs style shortcuts."; description = "VI or Emacs style shortcuts.";
}; };
@ -300,7 +324,8 @@ in {
plugins = mkOption { plugins = mkOption {
type = with types; type = with types;
listOf (either package pluginModule) // { listOf (either package pluginModule)
// {
description = "list of plugin packages or submodules"; description = "list of plugin packages or submodules";
}; };
description = '' description = ''
@ -308,7 +333,7 @@ in {
configuration. The sensible plugin, however, is defaulted to configuration. The sensible plugin, however, is defaulted to
run at the top of your configuration. run at the top of your configuration.
''; '';
default = [ ]; default = [];
example = literalExpression '' example = literalExpression ''
with pkgs; [ with pkgs; [
tmuxPlugins.cpu tmuxPlugins.cpu
@ -331,12 +356,18 @@ in {
config = mkIf cfg.enable (mkMerge [ config = mkIf cfg.enable (mkMerge [
{ {
home.packages = [ cfg.package ] home.packages =
[cfg.package]
++ optional cfg.tmuxinator.enable pkgs.tmuxinator ++ optional cfg.tmuxinator.enable pkgs.tmuxinator
++ optional cfg.tmuxp.enable pkgs.tmuxp; ++ optional cfg.tmuxp.enable pkgs.tmuxp;
} }
{ xdg.configFile."tmux/tmux.conf".text = mkBefore tmuxConf; } {xdg.configFile."tmux/tmux.conf".text = mkBefore tmuxConf;}
{xdg.configFile."tmux/tmux.conf".text = mkAfter cfg.extraConfig;}
{
# xdg.configFile."tmux/tmux.conf".text = mkIf (cfg.extraConfigBeforePlugins != "") (mkBefore cfg.extraConfigBeforePlugins);
}
(mkIf cfg.secureSocket { (mkIf cfg.secureSocket {
home.sessionVariables = { home.sessionVariables = {
@ -344,11 +375,6 @@ in {
}; };
}) })
{ (mkIf (cfg.plugins != []) configPlugins)
xdg.configFile."tmux/tmux.conf".text =
mkAfter cfg.extraConfigBeforePlugins;
}
(mkIf (cfg.plugins != [ ]) configPlugins)
{ xdg.configFile."tmux/tmux.conf".text = cfg.extraConfig; }
]); ]);
} }

View file

@ -1,15 +1,17 @@
# ============================================= # # ============================================= #
# Start with defaults from the Sensible plugin # # Start with defaults from the Sensible plugin #
# --------------------------------------------- # # --------------------------------------------- #
run-shell @sensible_rtp@ run-shell @tmuxplugin_sensible_rtp@
# ============================================= # # ============================================= #
set -g default-terminal "screen" set -g default-terminal "screen"
set -g base-index 0 set -g base-index 0
setw -g pane-base-index 0 setw -g pane-base-index 0
new-session
bind -N "Split the pane into two, left and right" v split-window -h
bind -N "Split the pane into two, top and bottom" s split-window -v
set -g status-keys emacs set -g status-keys emacs
@ -21,12 +23,12 @@ set -g mode-keys emacs
set -g mouse on set -g mouse off
setw -g aggressive-resize off setw -g aggressive-resize on
setw -g clock-mode-style 12 setw -g clock-mode-style 24
set -s escape-time 500 set -s escape-time 500
set -g history-limit 2000 set -g history-limit 2000
set -g mouse off
# ============================================= # # ============================================= #
# Load plugins with Home Manager # # Load plugins with Home Manager #
# --------------------------------------------- # # --------------------------------------------- #

View file

@ -1,28 +1,47 @@
{ config, lib, pkgs, ... }: {
config,
lib,
pkgs,
...
}:
with lib; { with lib; {
config = { config = {
programs.tmux = { programs.tmux = {
aggressiveResize = true;
clock24 = true;
enable = true; enable = true;
mouse = true; keyMode = "emacs";
extraConfigBeforePlugins = '' newSession = true;
set -g mouse off reverseSplit = true;
'';
plugins = with pkgs.tmuxPlugins; [ logging ]; plugins = with pkgs.tmuxPlugins; [
logging
];
}; };
nixpkgs.overlays = [ nixpkgs.overlays = [
(self: super: { (self: super: {
tmuxPlugins = super.tmuxPlugins // { tmuxPlugins =
sensible = super.tmuxPlugins.sensible // { rtp = "@sensible_rtp@"; }; super.tmuxPlugins
// {
logging =
super.tmuxPlugins.logging
// {
rtp = "@tmuxplugin_logging_rtp@";
};
sensible =
super.tmuxPlugins.sensible
// {
rtp = "@tmuxplugin_sensible_rtp@";
};
}; };
}) })
]; ];
nmt.script = '' nmt.script = ''
assertFileExists home-files/.config/tmux/tmux.conf assertFileExists home-files/.config/tmux/tmux.conf
assertFileContent home-files/.config/tmux/tmux.conf \ assertFileContent home-files/.config/tmux/tmux.conf ${./emacs-with-plugins.conf}
${./mouse-enabled.conf}
''; '';
}; };
} }