From cc21d84808dfd79736b3c44e72ea2c3a8e1476a6 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 30 Sep 2021 16:48:26 +0200 Subject: [PATCH] home: tmux: add tmux service The service definition is pretty awful, but it deals with the secure socket correctly... This avoids having a small lapse when starting the very first shell of a session, which must first launch the server. --- home/tmux/default.nix | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/home/tmux/default.nix b/home/tmux/default.nix index 8e0c901..0a8a355 100644 --- a/home/tmux/default.nix +++ b/home/tmux/default.nix @@ -5,6 +5,10 @@ in { options.my.home.tmux = with lib.my; { enable = mkDisableOption "tmux terminal multiplexer"; + + service = { + enable = mkDisableOption "tmux server service"; + }; }; config.programs.tmux = lib.mkIf cfg.enable { @@ -44,4 +48,30 @@ in bind-key -T copy-mode-vi 'y' send -X copy-selection-and-cancel ''; }; + + config.systemd.user.services.tmux = lib.mkIf cfg.service.enable { + Unit = { + Description = "tmux server"; + }; + + Install = { + WantedBy = [ "default.target" ]; + }; + + Service = + let + # Wrap `tmux` in a login shell and set the socket path + tmuxCmd = "${config.programs.tmux.package}/bin/tmux"; + socketExport = lib.optionalString + config.programs.tmux.secureSocket + ''export TMUX_TMPDIR=''${XDG_RUNTIME_DIR:-"/run/user/\$(id -u)"};''; + mkTmuxCommand = + c: "${pkgs.runtimeShell} -l -c '${socketExport} ${tmuxCmd} ${c}'"; + in + { + Type = "forking"; + ExecStart = mkTmuxCommand "new -d -s ambroisie"; + ExecStop = mkTmuxCommand "kill-server"; + }; + }; }