Skip to content

Commit 1ae1247

Browse files
feat: enhance tmux installation and session persistence configuration in claude-code module
- Added a function to install tmux using various package managers if not already present. - Improved validation for session persistence requirements, ensuring tmux is enabled when persistence is requested. - Streamlined the installation process for git and tmux plugins within the tmux session configuration.
1 parent 54b9737 commit 1ae1247

File tree

1 file changed

+68
-52
lines changed
  • registry/coder/modules/claude-code

1 file changed

+68
-52
lines changed

registry/coder/modules/claude-code/main.tf

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ resource "coder_script" "claude_code" {
115115
command -v "$1" >/dev/null 2>&1
116116
}
117117
118+
# Function to install tmux if needed
119+
install_tmux() {
120+
echo "Installing tmux..."
121+
if command_exists apt-get; then
122+
apt-get update && apt-get install -y tmux
123+
elif command_exists yum; then
124+
yum install -y tmux
125+
elif command_exists dnf; then
126+
dnf install -y tmux
127+
elif command_exists pacman; then
128+
pacman -S --noconfirm tmux
129+
elif command_exists apk; then
130+
apk add tmux
131+
else
132+
echo "Error: Unable to install tmux automatically. Package manager not recognized."
133+
exit 1
134+
fi
135+
}
136+
118137
# Check if the specified folder exists
119138
if [ ! -d "${var.folder}" ]; then
120139
echo "Warning: The specified folder '${var.folder}' does not exist."
@@ -186,38 +205,52 @@ resource "coder_script" "claude_code" {
186205
exit 1
187206
fi
188207
189-
# Configure tmux session persistence if enabled
190-
if [ "${var.experiment_tmux_session_persistence}" = "true" ] && [ "${var.experiment_use_tmux}" = "true" ]; then
191-
echo "Setting up tmux session persistence..."
192-
193-
# Check and install git if needed
194-
if ! command_exists git; then
195-
echo "Git not found, installing git..."
196-
if command_exists apt-get; then
197-
apt-get update && apt-get install -y git
198-
elif command_exists yum; then
199-
yum install -y git
200-
elif command_exists dnf; then
201-
dnf install -y git
202-
elif command_exists pacman; then
203-
pacman -S --noconfirm git
204-
elif command_exists apk; then
205-
apk add git
206-
else
207-
echo "Error: Unable to install git automatically. Package manager not recognized."
208-
echo "Please install git manually to enable session persistence."
209-
exit 1
210-
fi
211-
fi
212-
213-
# Install TPM and plugins
214-
mkdir -p ~/.tmux/plugins
215-
if [ ! -d ~/.tmux/plugins/tpm ]; then
216-
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
208+
# Validate session persistence requirements
209+
if [ "${var.experiment_tmux_session_persistence}" = "true" ] && [ "${var.experiment_use_tmux}" != "true" ]; then
210+
echo "Error: Session persistence requires tmux to be enabled."
211+
echo "Please set experiment_use_tmux = true when using session persistence."
212+
exit 1
213+
fi
214+
215+
# Install and configure tmux if enabled
216+
if [ "${var.experiment_use_tmux}" = "true" ]; then
217+
# Install tmux if not present
218+
if ! command_exists tmux; then
219+
install_tmux
217220
fi
218-
219-
# Configure .tmux.conf for persistence
220-
cat > ~/.tmux.conf << EOF
221+
222+
# Configure tmux session persistence if enabled
223+
if [ "${var.experiment_tmux_session_persistence}" = "true" ]; then
224+
echo "Setting up tmux session persistence..."
225+
226+
# Check and install git if needed
227+
if ! command_exists git; then
228+
echo "Git not found, installing git..."
229+
if command_exists apt-get; then
230+
apt-get update && apt-get install -y git
231+
elif command_exists yum; then
232+
yum install -y git
233+
elif command_exists dnf; then
234+
dnf install -y git
235+
elif command_exists pacman; then
236+
pacman -S --noconfirm git
237+
elif command_exists apk; then
238+
apk add git
239+
else
240+
echo "Error: Unable to install git automatically. Package manager not recognized."
241+
echo "Please install git manually to enable session persistence."
242+
exit 1
243+
fi
244+
fi
245+
246+
# Install TPM and plugins
247+
mkdir -p ~/.tmux/plugins
248+
if [ ! -d ~/.tmux/plugins/tpm ]; then
249+
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
250+
fi
251+
252+
# Configure .tmux.conf for persistence
253+
cat > ~/.tmux.conf << EOF
221254
# Claude Code tmux persistence configuration
222255
set -g @plugin 'tmux-plugins/tmux-resurrect'
223256
set -g @plugin 'tmux-plugins/tmux-continuum'
@@ -231,37 +264,19 @@ set -g @continuum-save-interval '${var.experiment_tmux_session_save_interval}'
231264
run '~/.tmux/plugins/tpm/tpm'
232265
EOF
233266
234-
# Install plugins
235-
~/.tmux/plugins/tpm/scripts/install_plugins.sh
236-
fi
237-
238-
# Validate session persistence requirements
239-
if [ "${var.experiment_tmux_session_persistence}" = "true" ] && [ "${var.experiment_use_tmux}" != "true" ]; then
240-
echo "Error: Session persistence requires tmux to be enabled."
241-
echo "Please set experiment_use_tmux = true when using session persistence."
242-
exit 1
243-
fi
244-
245-
# Run with tmux if enabled
246-
if [ "${var.experiment_use_tmux}" = "true" ]; then
247-
echo "Running Claude Code in the background with tmux..."
248-
249-
# Check if tmux is installed
250-
if ! command_exists tmux; then
251-
echo "Error: tmux is not installed. Please install tmux manually."
252-
exit 1
267+
# Install plugins
268+
~/.tmux/plugins/tpm/scripts/install_plugins.sh
253269
fi
254270
271+
echo "Running Claude Code in the background with tmux..."
255272
touch "$HOME/.claude-code.log"
256-
257273
export LANG=en_US.UTF-8
258274
export LC_ALL=en_US.UTF-8
259275
260276
# Create or attach to session (persistence handles restoration automatically)
261277
if ! tmux has-session -t claude-code 2>/dev/null; then
262278
tmux new-session -d -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions \"$CODER_MCP_CLAUDE_TASK_PROMPT\""
263279
fi
264-
265280
fi
266281
267282
# Run with screen if enabled
@@ -291,6 +306,7 @@ EOF
291306
echo "Adding 'acladd $(whoami)' to ~/.screenrc..." | tee -a "$HOME/.claude-code.log"
292307
echo "acladd $(whoami)" >> "$HOME/.screenrc"
293308
fi
309+
294310
export LANG=en_US.UTF-8
295311
export LC_ALL=en_US.UTF-8
296312

0 commit comments

Comments
 (0)