2

I want to start tmux server on sway startup. Therefore I created in sway.conf the following:

exec ~/.config/sway/scripts/start-up.sh

in which script I start tmux as such:

#!/usr/bin/env bash
# shellcheck disable=SC1091,SC2034
# set -e
# set -u

LOG=~/.sway-startup.log

# Save stdout and stderr to file
exec 3>&1 4>&2 >"$LOG" 2>&1

# Start tmux and make terminal on workspace 1.
echo "Start tmux, let it recreate the workspaces with resurrect"
tmux start-server
echo "Server started."
sleep 3

tmux list-sessions || {
    echo "ERROR: The exit-empty is not set to off, so the server directly exits!" >&2
}

# Create one session if not yet existing, detach from it.
tmux new-session -D -s Main-1

echo "Sessions are:"
tmux list-sessions || {
    echo "ERROR: Sessions should now be available restored from tmux-ressurect???!" >&2
}
echo "-----------"

echo "Finished"

In the tmux.conf I have set exit-empty false such that tmux does not quit if no session on `tmux start-server (weird behavior...).

I am really unsure if I need to block the script start-up.sh from exiting or not, because the log ~/.sway-startup.log from launching the script with swaymsg exec .../start-up.sh reports the following:

Start tmux, let it recreate the workspaces with resurrect
Server started.
Astrovim: 1 windows (created Sat Nov 25 21:40:20 2023)
Main-1: 1 windows (created Sat Nov 25 21:40:20 2023)
Main-2: 1 windows (created Sat Nov 25 21:40:20 2023)
NixOS: 1 windows (created Sat Nov 25 21:40:20 2023)
duplicate session: Main-1 ### <<<<<<<<<<<<<<<- thats because resurrect created the above sessions automatically. 
Sessions are:
Astrovim: 1 windows (created Sat Nov 25 21:40:20 2023)
Main-1: 1 windows (created Sat Nov 25 21:40:20 2023)
Main-2: 1 windows (created Sat Nov 25 21:40:20 2023)
NixOS: 1 windows (created Sat Nov 25 21:40:20 2023)
-----------
Finished

After that when I tmux a it reports there are no sessions. This is so weird, the server runs as I can see in btop (running under my user name) but also using tmux -S /run/user/1000/tmux-1000 a pointing to the socket does report no sessions?

I am really puzzled and dont know whats going on? How can I resolve this strange issue? Somehow this is permission problem when starting over swaymsg.

2
  • 2
    At the end of your script please add tmux display-message -p '#{socket_path}'. Restart and check the log. The point is tmux -S garbage a may show no sessions (and create garbage), so let's better be sure what the path of the socket is. Commented Nov 25, 2023 at 22:58
  • Thanks a lot that was the problem!
    – Gabriel
    Commented Nov 25, 2023 at 23:53

1 Answer 1

1

In NixOS using homemanager, some env variables are not yet sourced when this start-up.sh script runs. The one missing is TMUX_TMPDIR which is set afterwards.

I needed to write out this variable to be able to source it in the startup script by doing with home-manager:

  # We need this file to source in `~/.config/sway/scripts/start-up.sh`
  # to be able to properly start tmux because these variables are not
  # yet sourced.
  xdg.configFile."tmux/.tmux-env".text = ''
    TMUX_TMPDIR="${config.home.sessionVariables.TMUX_TMPDIR}"
  '';

So when sway started it wrote the default socket to /tmp/$USER-1000/default etc. which was not the directory which tmux was looking for after the sessionVariables were set.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .