Skip to content

Commit 8adff57

Browse files
author
Jay Kumar
committed
fix(registry/coder-labs/modules/codex): restore PATH persistence and auth.json seeding
Issue 1: Port ensure_codex_in_path and add_path_to_shell_profiles from claude-code v5. Symlinks codex into CODER_SCRIPT_BIN_DIR and persists the binary dir to shell profiles (bash, zsh, fish). Called after both the install and skip-install branches. Issue 3: Restore add_auth_json from v4. Writes OPENAI_API_KEY to ~/.codex/auth.json when AI Gateway is not enabled, fixing 401s on Codex versions where the env var alone is insufficient. Issue 6: Add README note warning that coder_app commands re-execute on pane reconnect; recommend coder_script for one-shot prompts.
1 parent 73dc281 commit 8adff57

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

registry/coder-labs/modules/codex/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ resource "coder_app" "codex" {
6363
}
6464
```
6565

66+
> [!NOTE]
67+
> The `coder_app` command re-executes on every pane reconnect. This works for interactive `codex` (which stays alive), but one-shot commands like `codex exec` will re-run each time. For one-shot prompts, use a `coder_script` (runs once at startup) and a `coder_app` that attaches to the existing session (e.g. via tmux/screen).
68+
6669
### Usage with AI Gateway
6770

6871
[AI Gateway](https://coder.com/docs/ai-coder/ai-gateway) is a Premium Coder feature that provides centralized LLM proxy management. Requires Coder >= 2.30.0.

registry/coder-labs/modules/codex/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ locals {
144144
ARG_ENABLE_AI_GATEWAY = tostring(var.enable_ai_gateway)
145145
ARG_AIBRIDGE_CONFIG = var.enable_ai_gateway ? base64encode(local.aibridge_config) : ""
146146
ARG_MODEL_REASONING_EFFORT = var.model_reasoning_effort
147+
ARG_OPENAI_API_KEY = var.openai_api_key != "" ? base64encode(var.openai_api_key) : ""
147148
})
148149
module_dir_name = ".coder-modules/coder-labs/codex"
149150
}

registry/coder-labs/modules/codex/scripts/install.sh.tftpl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ARG_ADDITIONAL_MCP_SERVERS=$(echo -n '${ARG_ADDITIONAL_MCP_SERVERS}' | base64 -d
1616
ARG_ENABLE_AI_GATEWAY='${ARG_ENABLE_AI_GATEWAY}'
1717
ARG_AIBRIDGE_CONFIG=$(echo -n '${ARG_AIBRIDGE_CONFIG}' | base64 -d)
1818
ARG_MODEL_REASONING_EFFORT='${ARG_MODEL_REASONING_EFFORT}'
19+
ARG_OPENAI_API_KEY=$(echo -n '${ARG_OPENAI_API_KEY}' | base64 -d)
1920

2021
echo "--------------------------------"
2122
printf "codex_version: %s\n" "$${ARG_CODEX_VERSION}"
@@ -25,9 +26,55 @@ printf "install_codex: %s\n" "$${ARG_INSTALL}"
2526
printf "model_reasoning_effort: %s\n" "$${ARG_MODEL_REASONING_EFFORT}"
2627
echo "--------------------------------"
2728

29+
function add_path_to_shell_profiles() {
30+
local path_dir="$1"
31+
32+
for profile in "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.bashrc" "$HOME/.zprofile" "$HOME/.zshrc"; do
33+
if [ -f "$${profile}" ]; then
34+
if ! grep -q "$${path_dir}" "$${profile}" 2> /dev/null; then
35+
echo "export PATH=\"\$PATH:$${path_dir}\"" >> "$${profile}"
36+
echo "Added $${path_dir} to $${profile}"
37+
fi
38+
fi
39+
done
40+
41+
local fish_config="$HOME/.config/fish/config.fish"
42+
if [ -f "$${fish_config}" ]; then
43+
if ! grep -q "$${path_dir}" "$${fish_config}" 2> /dev/null; then
44+
echo "fish_add_path $${path_dir}" >> "$${fish_config}"
45+
echo "Added $${path_dir} to $${fish_config}"
46+
fi
47+
fi
48+
}
49+
50+
function ensure_codex_in_path() {
51+
local CODEX_BIN=""
52+
if command -v codex > /dev/null 2>&1; then
53+
CODEX_BIN=$(command -v codex)
54+
elif [ -x "$HOME/.npm-global/bin/codex" ]; then
55+
CODEX_BIN="$HOME/.npm-global/bin/codex"
56+
fi
57+
58+
if [ -z "$${CODEX_BIN}" ] || [ ! -x "$${CODEX_BIN}" ]; then
59+
echo "Warning: Could not find codex binary after install"
60+
return
61+
fi
62+
63+
local CODEX_DIR
64+
CODEX_DIR=$(dirname "$${CODEX_BIN}")
65+
66+
if [ -n "$${CODER_SCRIPT_BIN_DIR:-}" ] && [ ! -e "$${CODER_SCRIPT_BIN_DIR}/codex" ]; then
67+
ln -s "$${CODEX_BIN}" "$${CODER_SCRIPT_BIN_DIR}/codex"
68+
echo "Created symlink: $${CODER_SCRIPT_BIN_DIR}/codex -> $${CODEX_BIN}"
69+
fi
70+
71+
add_path_to_shell_profiles "$${CODEX_DIR}"
72+
}
73+
2874
function install_codex() {
2975
if [ "$${ARG_INSTALL}" != "true" ]; then
3076
echo "Skipping Codex installation as per configuration."
77+
ensure_codex_in_path
3178
return
3279
fi
3380

@@ -61,6 +108,7 @@ function install_codex() {
61108
$PKG_INSTALL "@openai/codex"
62109
fi
63110
printf "%s Installed Codex CLI: %s\n" "$${BOLD}" "$(codex --version)"
111+
ensure_codex_in_path
64112
}
65113

66114
function write_minimal_default_config() {
@@ -124,6 +172,23 @@ function setup_workdir() {
124172
fi
125173
}
126174

175+
function add_auth_json() {
176+
if [ "$${ARG_ENABLE_AI_GATEWAY}" = "true" ] || [ -z "$${ARG_OPENAI_API_KEY}" ]; then
177+
return
178+
fi
179+
180+
local auth_path="$HOME/.codex/auth.json"
181+
mkdir -p "$(dirname "$${auth_path}")"
182+
183+
cat << EOF > "$${auth_path}"
184+
{
185+
"OPENAI_API_KEY": "$${ARG_OPENAI_API_KEY}"
186+
}
187+
EOF
188+
echo "Seeded auth.json with API key"
189+
}
190+
127191
install_codex
128192
populate_config_toml
129193
setup_workdir
194+
add_auth_json

0 commit comments

Comments
 (0)