|
| 1 | +#!/bin/bash |
| 2 | +# /usr/libexec/mios/install-ai-clis.sh |
| 3 | +# |
| 4 | +# Install MiOS-default AI assistant CLIs (Claude Code + Gemini CLI) as |
| 5 | +# global npm packages. Both are Node.js CLIs distributed via npm, so |
| 6 | +# they don't fit RPM packaging. This script is invoked once by the |
| 7 | +# build-mios.ps1 overlay phase and is also operator-re-runnable any |
| 8 | +# time (idempotent: `npm install -g <pkg>` upgrades or installs as |
| 9 | +# needed). |
| 10 | +# |
| 11 | +# Reads the npm_globals list from mios.toml [packages.ai] -- operator |
| 12 | +# can extend by editing the list before `mios update`. |
| 13 | +# |
| 14 | +# Operator override: MIOS_SKIP_AI_CLIS=1 skips entirely. |
| 15 | + |
| 16 | +set -e |
| 17 | + |
| 18 | +if [ "${MIOS_SKIP_AI_CLIS:-0}" = "1" ]; then |
| 19 | + echo " [skip] MIOS_SKIP_AI_CLIS=1; not installing AI CLIs." |
| 20 | + exit 0 |
| 21 | +fi |
| 22 | + |
| 23 | +if ! command -v npm >/dev/null 2>&1; then |
| 24 | + echo " [warn] npm not installed; cannot install AI CLIs. Add 'nodejs' + 'npm' to [packages.ai].pkgs." |
| 25 | + exit 0 |
| 26 | +fi |
| 27 | + |
| 28 | +# Pull npm_globals from layered mios.toml (~/.config > /etc/mios > /usr/share/mios). |
| 29 | +# Vendor fallback if the toml lookup fails: the two operator-canonical |
| 30 | +# AI CLIs. |
| 31 | +_resolve_npm_globals() { |
| 32 | + local toml |
| 33 | + for toml in \ |
| 34 | + "${HOME:-/var/home/mios}/.config/mios/mios.toml" \ |
| 35 | + /etc/mios/mios.toml \ |
| 36 | + /usr/share/mios/mios.toml; do |
| 37 | + [ -r "$toml" ] || continue |
| 38 | + # Extract npm_globals = [ "...", "..." ] from [packages.ai] section. |
| 39 | + awk ' |
| 40 | + /^\[/ { |
| 41 | + line=$0; sub(/[[:space:]]*#.*$/, "", line) |
| 42 | + in_ai = (line == "[packages.ai]") ? 1 : 0 |
| 43 | + next |
| 44 | + } |
| 45 | + in_ai && /^[[:space:]]*npm_globals[[:space:]]*=[[:space:]]*\[/ { |
| 46 | + capturing = 1 |
| 47 | + # On the same line? |
| 48 | + if (match($0, /\[.*\]/)) { |
| 49 | + body = substr($0, RSTART+1, RLENGTH-2) |
| 50 | + print body |
| 51 | + capturing = 0 |
| 52 | + next |
| 53 | + } |
| 54 | + next |
| 55 | + } |
| 56 | + capturing { buf = buf $0 "\n" } |
| 57 | + capturing && /^[[:space:]]*\][[:space:]]*$/ { print buf; capturing = 0; in_ai = 0; exit } |
| 58 | + ' "$toml" | grep -oE '"[^"]+"' | tr -d '"' && return 0 |
| 59 | + done |
| 60 | + # Vendor fallback |
| 61 | + echo "@anthropic-ai/claude-code" |
| 62 | + echo "@google/gemini-cli" |
| 63 | +} |
| 64 | + |
| 65 | +# Ensure npm prefix is a system path that's on $PATH for all users. |
| 66 | +# Default `npm -g` prefix on Fedora is /usr/local; we use /usr/local |
| 67 | +# so installed bins land at /usr/local/bin/ (which IS on PATH). |
| 68 | +mkdir -p /usr/local/lib/node_modules |
| 69 | + |
| 70 | +echo " installing AI CLIs (npm -g) ..." |
| 71 | +_failed=0 |
| 72 | +_resolve_npm_globals | while read -r pkg; do |
| 73 | + [ -z "$pkg" ] && continue |
| 74 | + echo " -> $pkg" |
| 75 | + if ! npm install -g --silent "$pkg" 2>&1 | tail -3; then |
| 76 | + echo " [warn] failed: $pkg" |
| 77 | + _failed=$((_failed + 1)) |
| 78 | + fi |
| 79 | +done |
| 80 | + |
| 81 | +echo " done. Try: claude --version / gemini --version" |
0 commit comments