为什么在 sway 启动时通过“swaymsg”启动“tmux”会导致 tmux 无法访问?

为什么在 sway 启动时通过“swaymsg”启动“tmux”会导致 tmux 无法访问?

我想tmux在 Sway 启动时启动服务器。因此我创建了sway.conf以下内容:

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

我在哪个脚本中启动 tmux:

#!/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"

tmux.confset exit-empty false这样的情况,如果“tmux start-server”上没有会话,tmux 就不会退出(奇怪的行为......)。

我真的不确定是否需要阻止脚本退出,因为启动脚本的start-up.sh日志 报告如下:~/.sway-startup.logswaymsg exec .../start-up.sh

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

之后当我tmux a 据报道no sessions。这太奇怪了,服务器运行时如我所见btop(以我的用户名运行),但也使用tmux -S /run/user/1000/tmux-1000 a指向套接字的报告no sessions

我真的很困惑,不知道发生了什么事?我该如何解决这个奇怪的问题?不知何故,这是重新开始时的权限问题swaymsg

答案1

在使用 homemanager 的 NixOS 中,此脚本运行时某些环境变量尚未获取start-up.sh。缺少的是TMUX_TMPDIR之后设置的变量。

我需要写出这个变量,以便能够通过执行以下操作在启动脚本中获取它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}"
  '';

因此,当 sway 启动时,它将default套接字写入/tmp/$USER-1000/default等,而这并不是在设置 sessionVariables 之后 tmux 正在寻找的目录。

相关内容