@@ -54,12 +54,35 @@ variable "experiment_use_screen" {
5454 default = false
5555}
5656
57+ variable "experiment_use_tmux" {
58+ type = bool
59+ description = " Whether to use tmux instead of screen for running Claude Code in the background."
60+ default = false
61+ }
62+
5763variable "experiment_report_tasks" {
5864 type = bool
5965 description = " Whether to enable task reporting."
6066 default = false
6167}
6268
69+ variable "experiment_pre_install_script" {
70+ type = string
71+ description = " Custom script to run before installing Claude Code."
72+ default = null
73+ }
74+
75+ variable "experiment_post_install_script" {
76+ type = string
77+ description = " Custom script to run after installing Claude Code."
78+ default = null
79+ }
80+
81+ locals {
82+ encoded_pre_install_script = var. experiment_pre_install_script != null ? base64encode (var. experiment_pre_install_script ) : " "
83+ encoded_post_install_script = var. experiment_post_install_script != null ? base64encode (var. experiment_post_install_script ) : " "
84+ }
85+
6386# Install and Initialize Claude Code
6487resource "coder_script" "claude_code" {
6588 agent_id = var. agent_id
@@ -74,6 +97,14 @@ resource "coder_script" "claude_code" {
7497 command -v "$1" >/dev/null 2>&1
7598 }
7699
100+ # Run pre-install script if provided
101+ if [ -n "${ local . encoded_pre_install_script } " ]; then
102+ echo "Running pre-install script..."
103+ echo "${ local . encoded_pre_install_script } " | base64 -d > /tmp/pre_install.sh
104+ chmod +x /tmp/pre_install.sh
105+ /tmp/pre_install.sh
106+ fi
107+
77108 # Install Claude Code if enabled
78109 if [ "${ var . install_claude_code } " = "true" ]; then
79110 if ! command_exists npm; then
@@ -84,11 +115,52 @@ resource "coder_script" "claude_code" {
84115 npm install -g @anthropic-ai/claude-code@${ var . claude_code_version }
85116 fi
86117
118+ # Run post-install script if provided
119+ if [ -n "${ local . encoded_post_install_script } " ]; then
120+ echo "Running post-install script..."
121+ echo "${ local . encoded_post_install_script } " | base64 -d > /tmp/post_install.sh
122+ chmod +x /tmp/post_install.sh
123+ /tmp/post_install.sh
124+ fi
125+
87126 if [ "${ var . experiment_report_tasks } " = "true" ]; then
88127 echo "Configuring Claude Code to report tasks via Coder MCP..."
89128 coder exp mcp configure claude-code ${ var . folder }
90129 fi
91130
131+ # Handle terminal multiplexer selection (tmux or screen)
132+ if [ "${ var . experiment_use_tmux } " = "true" ] && [ "${ var . experiment_use_screen } " = "true" ]; then
133+ echo "Error: Both experiment_use_tmux and experiment_use_screen cannot be true simultaneously."
134+ echo "Please set only one of them to true."
135+ exit 1
136+ fi
137+
138+ # Run with tmux if enabled
139+ if [ "${ var . experiment_use_tmux } " = "true" ]; then
140+ echo "Running Claude Code in the background with tmux..."
141+
142+ # Check if tmux is installed
143+ if ! command_exists tmux; then
144+ echo "Error: tmux is not installed. Please install tmux manually."
145+ exit 1
146+ fi
147+
148+ touch "$HOME/.claude-code.log"
149+
150+ export LANG=en_US.UTF-8
151+ export LC_ALL=en_US.UTF-8
152+
153+ # Create a new tmux session in detached mode
154+ tmux new-session -d -s claude-code -c ${ var . folder } "claude --dangerously-skip-permissions"
155+
156+ # Send the prompt to the tmux session if needed
157+ if [ -n "$CODER_MCP_CLAUDE_TASK_PROMPT" ]; then
158+ tmux send-keys -t claude-code "$CODER_MCP_CLAUDE_TASK_PROMPT"
159+ sleep 5
160+ tmux send-keys -t claude-code Enter
161+ fi
162+ fi
163+
92164 # Run with screen if enabled
93165 if [ "${ var . experiment_use_screen } " = "true" ]; then
94166 echo "Running Claude Code in the background..."
@@ -149,20 +221,27 @@ resource "coder_app" "claude_code" {
149221 #!/bin/bash
150222 set -e
151223
152- if [ "${ var . experiment_use_screen } " = "true" ]; then
224+ export LANG=en_US.UTF-8
225+ export LC_ALL=en_US.UTF-8
226+
227+ if [ "${ var . experiment_use_tmux } " = "true" ]; then
228+ if tmux has-session -t claude-code 2>/dev/null; then
229+ echo "Attaching to existing Claude Code tmux session." | tee -a "$HOME/.claude-code.log"
230+ tmux attach-session -t claude-code
231+ else
232+ echo "Starting a new Claude Code tmux session." | tee -a "$HOME/.claude-code.log"
233+ tmux new-session -s claude-code -c ${ var . folder } "claude --dangerously-skip-permissions | tee -a \"$HOME/.claude-code.log\"; exec bash"
234+ fi
235+ elif [ "${ var . experiment_use_screen } " = "true" ]; then
153236 if screen -list | grep -q "claude-code"; then
154- export LANG=en_US.UTF-8
155- export LC_ALL=en_US.UTF-8
156- echo "Attaching to existing Claude Code session." | tee -a "$HOME/.claude-code.log"
237+ echo "Attaching to existing Claude Code screen session." | tee -a "$HOME/.claude-code.log"
157238 screen -xRR claude-code
158239 else
159- echo "Starting a new Claude Code session." | tee -a "$HOME/.claude-code.log"
160- screen -S claude-code bash -c 'export LANG=en_US.UTF-8; export LC_ALL=en_US.UTF-8; claude --dangerously-skip-permissions | tee -a "$HOME/.claude-code.log"; exec bash'
240+ echo "Starting a new Claude Code screen session." | tee -a "$HOME/.claude-code.log"
241+ screen -S claude-code bash -c 'claude --dangerously-skip-permissions | tee -a "$HOME/.claude-code.log"; exec bash'
161242 fi
162243 else
163244 cd ${ var . folder }
164- export LANG=en_US.UTF-8
165- export LC_ALL=en_US.UTF-8
166245 claude
167246 fi
168247 EOT
0 commit comments