install.sh

10.9 KB · raw

1#!/usr/bin/env sh2# nina install script3# Run:  curl -fsSL kepr.uk/nina/install.sh | sh4# Or:   NINA_INSTALL_DIR=/usr/local/bin sh install.sh56set -e78FLAKE_URL="https://kepr.uk/nina/archive/HEAD.tar.gz"9RELEASES_URL="https://kepr.uk/api/nina/releases/latest"10INSTALL_DIR="${NINA_INSTALL_DIR:-$HOME/.local/bin}"11MIN_ZIG_MINOR=161213os="$(uname -s)"14arch="$(uname -m)"15case "${os}-${arch}" in16  Linux-x86_64)               platform="linux-x86_64"  ;;17  Linux-aarch64|Linux-arm64)  platform="linux-aarch64" ;;18  Darwin-arm64)               platform="macos-aarch64" ;;19  Darwin-x86_64)              platform="macos-x86_64"  ;;20  *)21    printf 'nina: unsupported platform %s-%s\n' "$os" "$arch" >&222    exit 1 ;;23esac2425have()  { command -v "$1" >/dev/null 2>&1; }26say()   { printf 'nina: %s\n' "$1"; }27die()   { say "error: $1" >&2; exit 1; }28blank() { printf '\n'; }29is_nixos() { [ -e /etc/NIXOS ]; }3031fetch_text() {32  if have curl;   then curl -fsSL "$1"33  elif have wget; then wget -qO- "$1"34  else die "curl or wget required"; fi35}3637fetch_file() {38  if have curl;   then curl -fsSL "$1" -o "$2"39  elif have wget; then wget -qO  "$2" "$1"40  else die "curl or wget required"; fi41}4243# ── banner ────────────────────────────────────────────────────────────────────4445banner() {46  cat <<'BANNER'4748   _ __   _  _ __    __ _49  | '_ \ (_)| '_ \  / _` |50  | | | || || | | || (_| |51  |_| |_||_||_| |_| \__,_|5253  NixOS Intuitive Navigation Assistant  ·  kepr.uk/nina5455BANNER56}5758# ── fetch source archive ──────────────────────────────────────────────────────5960fetch_source() {61  srcdir="$(mktemp -d /tmp/nina-src.XXXXXX)"62  say "fetching source from Kepr..."63  fetch_file "$FLAKE_URL" "${srcdir}/nina.tar.gz" \64    || die "could not download source archive — check your connection"65  tar -xzf "${srcdir}/nina.tar.gz" -C "$srcdir" --strip-components=1 \66    || die "could not extract source archive"67  printf '%s' "$srcdir"68}6970# ── binary install from Kepr ─────────────────────────────────────────────────7172install_binary() {73  say "fetching release metadata..."74  meta="$(fetch_text "$RELEASES_URL" 2>/dev/null)" || return 175  [ -n "$meta" ] || return 17677  if have python3; then78    version="$(printf '%s' "$meta" | python3 -c "79import sys, json80d = json.load(sys.stdin)81print(d.get('version') or '')82" 2>/dev/null)"83    dl_url="$(printf '%s' "$meta" | python3 -c "84import sys, json85d = json.load(sys.stdin)86print((d.get('platforms') or {}).get('${platform}', {}).get('url') or '')87" 2>/dev/null)"88    cksum="$(printf '%s' "$meta" | python3 -c "89import sys, json90d = json.load(sys.stdin)91p = (d.get('platforms') or {}).get('${platform}', {})92raw = p.get('blake3') or p.get('checksum') or ''93print(raw[len('blake3:'):] if raw.startswith('blake3:') else raw)94" 2>/dev/null)"95  elif have jq; then96    version="$(printf '%s' "$meta" | jq -r '.version // empty')"97    dl_url="$(printf '%s' "$meta" | jq -r ".platforms.\"${platform}\".url // empty")"98    cksum_raw="$(printf '%s' "$meta" | jq -r ".platforms.\"${platform}\".blake3 // .platforms.\"${platform}\".checksum // empty")"99    cksum="${cksum_raw#blake3:}"100  else101    version="$(printf '%s' "$meta" | grep -o '"version":"[^"]*"' | sed 's/"version":"//;s/"//')"102    plat_block="$(printf '%s' "$meta" | grep -o "\"${platform}\":{[^}]*}" | head -1)"103    dl_url="$(printf '%s' "$plat_block" | grep -o '"url":"[^"]*"' | sed 's/"url":"//;s/"//')"104    cksum=""105  fi106107  [ -n "$dl_url" ] || return 1108  [ -n "$version" ] || version="latest"109110  say "downloading nina ${version} for ${platform}..."111  tmpfile="$(mktemp /tmp/nina.XXXXXX)"112  fetch_file "$dl_url" "$tmpfile" || { rm -f "$tmpfile"; return 1; }113114  if [ -n "$cksum" ] && have b3sum; then115    actual="$(b3sum --no-names "$tmpfile" 2>/dev/null | awk '{print $1}')"116    if [ "$actual" != "$cksum" ]; then117      say "checksum mismatch — aborting"118      rm -f "$tmpfile"119      return 1120    fi121    say "checksum verified"122  fi123124  chmod +x "$tmpfile"125  mkdir -p "$INSTALL_DIR"126  mv "$tmpfile" "${INSTALL_DIR}/nina"127  say "installed nina ${version} → ${INSTALL_DIR}/nina"128  return 0129}130131# ── NixOS: nix profile install (primary) ─────────────────────────────────────132133nixos_profile_install() {134  have nix || return 1135  say "installing nina to ~/.nix-profile/bin/nina via nix profile..."136  blank137  if nix profile install "${FLAKE_URL}#nina"; then138    return 0139  fi140  blank141  say "nix profile install failed. Common causes:"142  say "  flakes not enabled — add to /etc/nix/nix.conf or ~/.config/nix/nix.conf:"143  say "    experimental-features = nix-command flakes"144  say "  once nina is installed: nina setup enables this automatically"145  blank146  return 1147}148149# ── NixOS: nix-shell source build (fallback) ──────────────────────────────────150151nixos_shell_build() {152  have nix-shell || return 1153  say "fetching source and building nina using nix-shell..."154  blank155  srcdir="$(fetch_source)"156157  # Try the user's channel first; fall back to a pinned nixpkgs-unstable if158  # zig_0_16 is not yet in the channel (common on NixOS 24.11 stable).159  build_cmd="cd '${srcdir}' && zig build -Doptimize=ReleaseSafe"160  if nix-shell -p zig_0_16 --run "$build_cmd" 2>/dev/null; then161    :162  else163    say "zig_0_16 not in channel — fetching from nixpkgs-unstable..."164    nix-shell \165      -I nixpkgs=https://releases.nixos.org/nixos/unstable/nixos-unstable/nixexprs.tar.xz \166      -p zig_0_16 \167      --run "$build_cmd" \168      || die "build failed — see output above"169  fi170171  mkdir -p "$INSTALL_DIR"172  cp "${srcdir}/zig-out/bin/nina" "${INSTALL_DIR}/nina"173  chmod +x "${INSTALL_DIR}/nina"174  blank175  say "installed from source → ${INSTALL_DIR}/nina"176  say "note: binary built outside nix — adopt ~/nina.nix into configuration.nix"177  say "      to make nina fully nix-managed (nina goodbye handles removal either way)"178}179180# ── generate ~/nina.nix ───────────────────────────────────────────────────────181182generate_nixos_module() {183  # Write ~/nina.nix without invoking shell substitution on the nix code inside.184  cat > "$HOME/nina.nix" <<'NIXEOF'185# ~/nina.nix — generated by the nina installer186#187# Declares nina as a NixOS system package.188#189# Add to /etc/nixos/configuration.nix:190#   imports = [ /home/YOUR_USER/nina.nix ];191# Then: sudo nixos-rebuild switch192#193# To update:  nina update  (or re-run the installer)194# To remove:  nina goodbye  (or delete this file, then nixos-rebuild switch)195#196# For flake-based configurations, add nina as a flake input instead:197#   inputs.nina.url = "https://kepr.uk/nina/archive/HEAD.tar.gz";198#   # in your nixosModules list: nina.nixosModules.default199#   # then set: programs.nina.enable = true;200{ ... }:201{202  imports = [203    "${builtins.fetchTarball "https://kepr.uk/nina/archive/HEAD.tar.gz"}/nixos-module.nix"204  ];205  programs.nina.enable = true;206}207NIXEOF208  blank209  say "generated ~/nina.nix for system-wide installation:"210  say "  1. add to /etc/nixos/configuration.nix:"211  say "       imports = [ /home/$(id -un)/nina.nix ];"212  say "  2. sudo nixos-rebuild switch"213}214215# ── Zig version check ─────────────────────────────────────────────────────────216217check_zig() {218  if ! have zig; then219    blank220    say "Zig not found. Building nina requires Zig 0.${MIN_ZIG_MINOR}+."221    blank222    say "  Download: https://ziglang.org/download/"223    if is_nixos; then224      say "  NixOS:    nix-shell -p zig_0_16 --run 'sh install.sh'"225    fi226    blank227    die "Zig 0.${MIN_ZIG_MINOR}+ required"228  fi229230  zig_ver="$(zig version 2>/dev/null)"231  major="$(printf '%s' "$zig_ver" | cut -d. -f1)"232  minor="$(printf '%s' "$zig_ver" | cut -d. -f2)"233  ok=0234  [ "$major" -ge 1 ] 2>/dev/null && ok=1235  [ "$major" -eq 0 ] 2>/dev/null && [ "$minor" -ge "$MIN_ZIG_MINOR" ] 2>/dev/null && ok=1236237  if [ "$ok" -ne 1 ]; then238    blank239    say "found zig ${zig_ver}, but nina requires Zig 0.${MIN_ZIG_MINOR}+."240    if is_nixos; then241      say "  run: nix-shell -p zig_0_16 --run 'sh install.sh'"242    else243      say "  update: https://ziglang.org/download/"244    fi245    blank246    die "Zig 0.${MIN_ZIG_MINOR}+ required; found ${zig_ver}"247  fi248249  say "zig ${zig_ver} found"250}251252# ── generic source build (non-NixOS) ─────────────────────────────────────────253254build_from_source() {255  say "building from source..."256  check_zig257258  if [ -f "build.zig" ] && [ -f "src/main.zig" ]; then259    srcdir="."260  else261    srcdir="$(fetch_source)"262  fi263264  (cd "$srcdir" && zig build -Doptimize=ReleaseSafe) \265    || die "build failed — see output above"266267  mkdir -p "$INSTALL_DIR"268  cp "${srcdir}/zig-out/bin/nina" "${INSTALL_DIR}/nina"269  chmod +x "${INSTALL_DIR}/nina"270  say "installed from source → ${INSTALL_DIR}/nina"271}272273# ── PATH reminder ─────────────────────────────────────────────────────────────274275check_path() {276  case ":${PATH}:" in277    *":${INSTALL_DIR}:"*) ;;278    *)279      blank280      say "${INSTALL_DIR} is not in your PATH. Add it:"281      say "  export PATH=\"\${PATH}:${INSTALL_DIR}\""282      say "  echo 'export PATH=\"\${PATH}:${INSTALL_DIR}\"' >> ~/.bashrc"283      if is_nixos; then284        say "  NixOS: add to environment.sessionVariables in configuration.nix"285      fi286      ;;287  esac288}289290# ── closing message ──────────────────────────────────────────────────────────291292farewell() {293  printf '\n'294  printf '  thank you!! running nina update occasionally will keep me in tip top shape.\n'295  printf '  get started by running nina hello\n'296  printf '\n'297  printf '  if nina turns out not to be for you, nina goodbye will remove her cleanly.\n'298  printf '\n'299}300301# ── main ──────────────────────────────────────────────────────────────────────302303banner304305if is_nixos; then306  say "NixOS detected"307  if nixos_profile_install; then308    generate_nixos_module309    farewell310    exit 0311  fi312  nixos_shell_build313  generate_nixos_module314  check_path315  farewell316  exit 0317fi318319# macOS / generic Linux: download binary, fall back to source build320if install_binary; then321  check_path322  farewell323  exit 0324fi325326say "no pre-built binary for ${platform} — falling back to source build"327build_from_source328check_path329farewell