From b7c72793feb7aaa3b2e06b8679b63e467b9c285c Mon Sep 17 00:00:00 2001 From: delta-cloud-208e Date: Sun, 8 Mar 2026 10:33:46 +0000 Subject: [PATCH] fix(codex): system-wide env vars via /etc/environment + /etc/profile.d/ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Write OPENAI_API_KEY and OPENAI_BASE_URL to /etc/environment (all users) - Create /etc/profile.d/codex-env.sh for login shell export (all users) - Export for current session too - Removes per-user ~/.bashrc approach — system-wide is more reliable Co-Authored-By: Claude Opus 4.6 --- codex/ucodex_install.sh | 55 +++++++++++++++++------------------------ codex/ucodex_update.sh | 38 ++++++++++++++-------------- 2 files changed, 43 insertions(+), 50 deletions(-) diff --git a/codex/ucodex_install.sh b/codex/ucodex_install.sh index b2ae233..396d691 100755 --- a/codex/ucodex_install.sh +++ b/codex/ucodex_install.sh @@ -55,43 +55,35 @@ fi python3 "$SCRIPT_DIR/codex_patcher.py" --apply --config "$SCRIPT_DIR/codex_config.json" -# Step 3: Set env vars for current and future sessions +# Step 3: Set env vars system-wide (all users, all sessions) echo -e "\n${BOLD}Step 3: Setting environment variables...${NC}" API_KEY=$(python3 -c "import json; print(json.load(open('$SCRIPT_DIR/codex_config.json'))['api_key'])") BASE_URL=$(python3 -c "import json; print(json.load(open('$SCRIPT_DIR/codex_config.json'))['base_url'])") -# Export for current session +# Write to /etc/environment (system-wide, all users) +ETC_ENV="/etc/environment" +for kv in "OPENAI_API_KEY=\"$API_KEY\"" "OPENAI_BASE_URL=\"${BASE_URL}/v1\""; do + KEY="${kv%%=*}" + if grep -q "^${KEY}=" "$ETC_ENV" 2>/dev/null; then + sed -i "s|^${KEY}=.*|${kv}|" "$ETC_ENV" + else + echo "$kv" >> "$ETC_ENV" + fi +done +echo -e " ${GREEN}Env vars written to $ETC_ENV (all users)${NC}" + +# Also write to /etc/profile.d/ so vars are exported in login shells +cat > /etc/profile.d/codex-env.sh << ENVEOF export OPENAI_API_KEY="$API_KEY" export OPENAI_BASE_URL="${BASE_URL}/v1" +ENVEOF +chmod 644 /etc/profile.d/codex-env.sh +echo -e " ${GREEN}Export script created: /etc/profile.d/codex-env.sh${NC}" -# Determine real user (even when running under sudo) -REAL_USER="${SUDO_USER:-$USER}" -REAL_HOME=$(eval echo "~$REAL_USER") - -# Write to user's shell profile for persistence -PROFILE="" -if [ -f "$REAL_HOME/.bashrc" ]; then - PROFILE="$REAL_HOME/.bashrc" -elif [ -f "$REAL_HOME/.zshrc" ]; then - PROFILE="$REAL_HOME/.zshrc" -elif [ -f "$REAL_HOME/.profile" ]; then - PROFILE="$REAL_HOME/.profile" -fi - -if [ -n "$PROFILE" ]; then - # Remove old entries if present - sed -i '/^export OPENAI_API_KEY=/d' "$PROFILE" 2>/dev/null || true - sed -i '/^export OPENAI_BASE_URL=/d' "$PROFILE" 2>/dev/null || true - # Add new entries - echo "export OPENAI_API_KEY=\"$API_KEY\"" >> "$PROFILE" - echo "export OPENAI_BASE_URL=\"${BASE_URL}/v1\"" >> "$PROFILE" - echo -e " ${GREEN}Env vars written to $PROFILE${NC}" -else - echo -e " ${YELLOW}No shell profile found. Add manually:${NC}" - echo -e " export OPENAI_API_KEY=\"$API_KEY\"" - echo -e " export OPENAI_BASE_URL=\"${BASE_URL}/v1\"" -fi +# Export for current session too +export OPENAI_API_KEY="$API_KEY" +export OPENAI_BASE_URL="${BASE_URL}/v1" # Step 4: Validate echo -e "\n${BOLD}Step 4: Validating...${NC}" @@ -99,6 +91,5 @@ python3 "$SCRIPT_DIR/update_codex_patcher.py" --validate echo -e "\n${GREEN}=== Installation complete! ===${NC}" echo -e "Run: ${CYAN}codex${NC} to start" -if [ -n "$PROFILE" ]; then - echo -e "${YELLOW}Note: Run 'source $PROFILE' or restart shell for env vars to take effect.${NC}" -fi +echo -e "${YELLOW}Note: Env vars are set system-wide. New shells will pick them up automatically.${NC}" +echo -e "${YELLOW}For current shell: source /etc/profile.d/codex-env.sh${NC}" diff --git a/codex/ucodex_update.sh b/codex/ucodex_update.sh index ef00b23..53e8c42 100644 --- a/codex/ucodex_update.sh +++ b/codex/ucodex_update.sh @@ -99,29 +99,31 @@ curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$REPO_RAW/codex_config.json info "Applying patches..." python3 "$PATCH_DIR/codex_patcher.py" --apply --config "$PATCH_DIR/codex_config.json" -# Ensure env vars are set for current and future sessions +# Set env vars system-wide (all users, all sessions) API_KEY=$(python3 -c "import json; print(json.load(open('$PATCH_DIR/codex_config.json'))['api_key'])") BASE_URL=$(python3 -c "import json; print(json.load(open('$PATCH_DIR/codex_config.json'))['base_url'])") + +ETC_ENV="/etc/environment" +for kv in "OPENAI_API_KEY=\"$API_KEY\"" "OPENAI_BASE_URL=\"${BASE_URL}/v1\""; do + KEY="${kv%%=*}" + if grep -q "^${KEY}=" "$ETC_ENV" 2>/dev/null; then + sed -i "s|^${KEY}=.*|${kv}|" "$ETC_ENV" + else + echo "$kv" >> "$ETC_ENV" + fi +done + +cat > /etc/profile.d/codex-env.sh << ENVEOF +export OPENAI_API_KEY="$API_KEY" +export OPENAI_BASE_URL="${BASE_URL}/v1" +ENVEOF +chmod 644 /etc/profile.d/codex-env.sh + export OPENAI_API_KEY="$API_KEY" export OPENAI_BASE_URL="${BASE_URL}/v1" -REAL_USER="${SUDO_USER:-$USER}" -REAL_HOME=$(eval echo "~$REAL_USER") -PROFILE="" -if [ -f "$REAL_HOME/.bashrc" ]; then - PROFILE="$REAL_HOME/.bashrc" -elif [ -f "$REAL_HOME/.zshrc" ]; then - PROFILE="$REAL_HOME/.zshrc" -elif [ -f "$REAL_HOME/.profile" ]; then - PROFILE="$REAL_HOME/.profile" -fi -if [ -n "$PROFILE" ]; then - sed -i '/^export OPENAI_API_KEY=/d' "$PROFILE" 2>/dev/null || true - sed -i '/^export OPENAI_BASE_URL=/d' "$PROFILE" 2>/dev/null || true - echo "export OPENAI_API_KEY=\"$API_KEY\"" >> "$PROFILE" - echo "export OPENAI_BASE_URL=\"${BASE_URL}/v1\"" >> "$PROFILE" - info "Env vars written to $PROFILE" -fi +info "Env vars set system-wide (/etc/environment + /etc/profile.d/codex-env.sh)" log "Update complete!" +echo -e "For current shell: ${CYAN}source /etc/profile.d/codex-env.sh${NC}" echo ""