fix: wrapper scripts for all CLIs — env vars work immediately without source

Problem: `sudo bash install.sh` runs in child process, `export` inside it
never reaches the user's current shell. /etc/environment and /etc/profile.d/
only work for NEW sessions. So `codex`/`gemini`/`qwen` fail with
"Missing environment variable" right after install.

Solution: wrapper scripts that auto-source env file before exec'ing binary.
- codex: /usr/local/bin/codex (wrapper) -> /usr/local/bin/.codex-bin (real)
- gemini: /usr/local/bin/gemini (wrapper) -> node .../dist/index.js
- qwen: /usr/local/bin/qwen (wrapper) -> node .../dist/index.js

Works immediately in ANY shell, no manual `source` needed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
delta-cloud-208e
2026-03-08 10:51:56 +00:00
parent be048ee873
commit d37772d67c
6 changed files with 260 additions and 98 deletions

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# UnlimitedCoding — Codex CLI Installer
# Downloads Codex binary from GitHub + applies config patches
# Uses wrapper script so env vars work immediately in any shell.
#
# Usage:
# curl -fsSL -H "Authorization: token TOKEN" \
@@ -12,6 +13,10 @@ GITEA_TOKEN="${GITEA_TOKEN:-cadffcb0a6a3be728ac1ff619bb40c86588f6837}"
REPO_RAW="https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/codex"
GITHUB_API="https://api.github.com/repos/openai/codex/releases/latest"
CODEX_BIN="/usr/local/bin/.codex-bin"
CODEX_WRAPPER="/usr/local/bin/codex"
ENV_FILE="/etc/profile.d/codex-env.sh"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
@@ -66,9 +71,16 @@ if [ -z "$LATEST_VER" ]; then
fi
info "Latest version: $LATEST_VER ($BINARY_SUFFIX)"
# Migrate: if old codex is a real binary (not our wrapper), move it
if [ -f "$CODEX_WRAPPER" ] && file "$CODEX_WRAPPER" | grep -q "ELF"; then
info "Migrating existing binary to $CODEX_BIN..."
mv -f "$CODEX_WRAPPER" "$CODEX_BIN"
fi
# Get current version (from real binary)
OLD_VER="not installed"
if command -v codex &>/dev/null; then
OLD_VER=$(codex --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "unknown")
if [ -f "$CODEX_BIN" ]; then
OLD_VER=$("$CODEX_BIN" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "unknown")
info "Current version: $OLD_VER"
fi
@@ -90,14 +102,13 @@ else
fi
pkill -9 -x "codex" 2>/dev/null || true
CODEX_PATH=$(which codex 2>/dev/null || echo "/usr/local/bin/codex")
chmod +x "$BINARY_FILE"
mv -f "$BINARY_FILE" "$CODEX_PATH"
mv -f "$BINARY_FILE" "$CODEX_BIN"
rm -rf "$TEMP_BIN"
hash -r 2>/dev/null || true
NEW_VER=$(codex --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "unknown")
log "Binary installed: $OLD_VER $NEW_VER"
NEW_VER=$("$CODEX_BIN" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "unknown")
log "Binary installed: $OLD_VER -> $NEW_VER"
fi
# ---- Step 2: Download and apply patches ----
@@ -116,14 +127,14 @@ info "Applying patches..."
python3 "$INSTALL_DIR/codex_patcher.py" --apply --config "$INSTALL_DIR/codex_config.json"
log "Patches applied"
# ---- Step 3: Set env vars system-wide (all users, all sessions) ----
# ---- Step 3: Set env vars system-wide ----
echo -e "\n${BOLD}Step 3: Setting environment variables...${NC}"
API_KEY=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/codex_config.json'))['api_key'])")
BASE_URL=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/codex_config.json'))['base_url'])")
# Write to /etc/environment (all users, all sessions including cron)
# /etc/environment (all users, all sessions including cron)
ETC_ENV="/etc/environment"
for kv in "OPENAI_API_KEY=\"$API_KEY\"" "OPENAI_BASE_URL=\"${BASE_URL}/v1\""; do
KEY="${kv%%=*}"
@@ -133,34 +144,42 @@ for kv in "OPENAI_API_KEY=\"$API_KEY\"" "OPENAI_BASE_URL=\"${BASE_URL}/v1\""; do
echo "$kv" >> "$ETC_ENV"
fi
done
log "Env vars written to $ETC_ENV (all users)"
log "Env vars written to $ETC_ENV"
# Write to /etc/profile.d/ for export in login shells
cat > /etc/profile.d/codex-env.sh << ENVEOF
# /etc/profile.d/ (login shells)
cat > "$ENV_FILE" << ENVEOF
export OPENAI_API_KEY="$API_KEY"
export OPENAI_BASE_URL="${BASE_URL}/v1"
ENVEOF
chmod 644 /etc/profile.d/codex-env.sh
log "Export script created: /etc/profile.d/codex-env.sh"
chmod 644 "$ENV_FILE"
log "Env file: $ENV_FILE"
# Export for current session
export OPENAI_API_KEY="$API_KEY"
export OPENAI_BASE_URL="${BASE_URL}/v1"
# ---- Step 4: Create wrapper (auto-loads env) ----
# ---- Step 4: Verify ----
echo -e "\n${BOLD}Step 4: Creating wrapper...${NC}"
echo -e "\n${BOLD}Step 4: Verifying...${NC}"
cat > "$CODEX_WRAPPER" << 'WRAPPER_EOF'
#!/usr/bin/env bash
# Auto-generated wrapper — loads env vars before running codex binary
[ -f /etc/profile.d/codex-env.sh ] && . /etc/profile.d/codex-env.sh
exec /usr/local/bin/.codex-bin "$@"
WRAPPER_EOF
chmod +x "$CODEX_WRAPPER"
log "Wrapper: $CODEX_WRAPPER -> $CODEX_BIN"
if command -v codex &>/dev/null; then
# ---- Step 5: Verify ----
echo -e "\n${BOLD}Step 5: Verifying...${NC}"
if codex --version &>/dev/null; then
VER=$(codex --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "unknown")
log "Codex CLI v$VER"
log "Codex CLI v$VER (wrapper OK)"
else
err "codex binary not found in PATH"
err "codex wrapper not working"
exit 1
fi
echo ""
echo -e "${GREEN}${BOLD}=== Installation complete! ===${NC}"
echo -e "Run: ${CYAN}codex${NC} to start"
echo -e "${YELLOW}Note: Env vars are set system-wide. New shells pick them up automatically.${NC}"
echo -e "${YELLOW}For current shell: source /etc/profile.d/codex-env.sh${NC}"
echo -e "${YELLOW}Env vars auto-loaded by wrapper. Works in any shell immediately.${NC}"