Skip to content

Commit 4e8637c

Browse files
committed
tmux: use idempotent new-session to avoid duplicate sessions
When both system-level (e.g., NixOS programs.tmux) and home-manager tmux configs set newSession = true, tmux would create two sessions on startup since both /etc/tmux.conf and ~/.config/tmux/tmux.conf contain the new-session command. Change new-session to new-session -A -s 0, which attaches to session "0" if it exists, otherwise creates it. This makes the command idempotent so multiple configs can safely enable newSession.
1 parent ccd22c1 commit 4e8637c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

modules/programs/tmux.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ let
5757
# We need to set default-shell before calling new-session
5858
set -g default-shell "${cfg.shell}"
5959
''}
60-
${optionalString cfg.newSession "new-session"}
60+
# Use -A to make new-session idempotent: attach if session "0" exists,
61+
# otherwise create it. This prevents duplicate sessions when multiple
62+
# configs (e.g., system and user) both enable newSession.
63+
${optionalString cfg.newSession "new-session -A -s 0"}
6164
6265
${optionalString cfg.reverseSplit ''
6366
bind -N "Split the pane into two, left and right" v split-window -h

tests/modules/programs/tmux/emacs-with-plugins.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ set -g default-terminal "screen"
33
set -g base-index 0
44
setw -g pane-base-index 0
55

6-
new-session
6+
# Use -A to make new-session idempotent: attach if session "0" exists,
7+
# otherwise create it. This prevents duplicate sessions when multiple
8+
# configs (e.g., system and user) both enable newSession.
9+
new-session -A -s 0
710

811
bind -N "Split the pane into two, left and right" v split-window -h
912
bind -N "Split the pane into two, top and bottom" s split-window -v

tests/modules/programs/tmux/vi-all-true.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ set -g default-terminal "screen"
33
set -g base-index 0
44
setw -g pane-base-index 0
55

6-
new-session
6+
# Use -A to make new-session idempotent: attach if session "0" exists,
7+
# otherwise create it. This prevents duplicate sessions when multiple
8+
# configs (e.g., system and user) both enable newSession.
9+
new-session -A -s 0
710

811
bind -N "Split the pane into two, left and right" v split-window -h
912
bind -N "Split the pane into two, top and bottom" s split-window -v

0 commit comments

Comments
 (0)