install.sh

12.3 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  # This function's stdout IS the source path (captured via command62  # substitution), so every diagnostic must go to stderr — otherwise the63  # caller's `cd "$srcdir"` receives the progress text too and fails.64  srcdir="$(mktemp -d /tmp/nina-src.XXXXXX)"65  say "fetching source from Kepr..." >&266  fetch_file "$FLAKE_URL" "${srcdir}/nina.tar.gz" \67    || die "could not download source archive — check your connection"68  tar -xzf "${srcdir}/nina.tar.gz" -C "$srcdir" --strip-components=1 \69    || die "could not extract source archive"70  printf '%s' "$srcdir"71}7273# ── binary install from Kepr ─────────────────────────────────────────────────7475install_binary() {76  say "fetching release metadata..."77  meta="$(fetch_text "$RELEASES_URL" 2>/dev/null)" || return 178  [ -n "$meta" ] || return 17980  if have python3; then81    version="$(printf '%s' "$meta" | python3 -c "82import sys, json83d = json.load(sys.stdin)84print(d.get('version') or '')85" 2>/dev/null)"86    dl_url="$(printf '%s' "$meta" | python3 -c "87import sys, json88d = json.load(sys.stdin)89print((d.get('platforms') or {}).get('${platform}', {}).get('url') or '')90" 2>/dev/null)"91    cksum="$(printf '%s' "$meta" | python3 -c "92import sys, json93d = json.load(sys.stdin)94p = (d.get('platforms') or {}).get('${platform}', {})95raw = p.get('blake3') or p.get('checksum') or ''96print(raw[len('blake3:'):] if raw.startswith('blake3:') else raw)97" 2>/dev/null)"98  elif have jq; then99    version="$(printf '%s' "$meta" | jq -r '.version // empty')"100    dl_url="$(printf '%s' "$meta" | jq -r ".platforms.\"${platform}\".url // empty")"101    cksum_raw="$(printf '%s' "$meta" | jq -r ".platforms.\"${platform}\".blake3 // .platforms.\"${platform}\".checksum // empty")"102    cksum="${cksum_raw#blake3:}"103  else104    version="$(printf '%s' "$meta" | grep -o '"version":"[^"]*"' | sed 's/"version":"//;s/"//')"105    plat_block="$(printf '%s' "$meta" | grep -o "\"${platform}\":{[^}]*}" | head -1)"106    dl_url="$(printf '%s' "$plat_block" | grep -o '"url":"[^"]*"' | sed 's/"url":"//;s/"//')"107    cksum=""108  fi109110  [ -n "$dl_url" ] || return 1111  [ -n "$version" ] || version="latest"112113  say "downloading nina ${version} for ${platform}..."114  tmpfile="$(mktemp /tmp/nina.XXXXXX)"115  fetch_file "$dl_url" "$tmpfile" || { rm -f "$tmpfile"; return 1; }116117  if [ -n "$cksum" ] && have b3sum; then118    actual="$(b3sum --no-names "$tmpfile" 2>/dev/null | awk '{print $1}')"119    if [ "$actual" != "$cksum" ]; then120      say "checksum mismatch — aborting"121      rm -f "$tmpfile"122      return 1123    fi124    say "checksum verified"125  fi126127  chmod +x "$tmpfile"128  mkdir -p "$INSTALL_DIR"129  mv "$tmpfile" "${INSTALL_DIR}/nina"130  say "installed nina ${version} → ${INSTALL_DIR}/nina"131  return 0132}133134# ── NixOS: nix profile install (primary) ─────────────────────────────────────135136nixos_profile_install() {137  have nix || return 1138  say "installing nina to ~/.nix-profile/bin/nina via nix profile..."139  blank140  if nix profile install "${FLAKE_URL}#nina"; then141    return 0142  fi143  blank144  say "nix profile install failed. Common causes:"145  say "  flakes not enabled — add to /etc/nix/nix.conf or ~/.config/nix/nix.conf:"146  say "    experimental-features = nix-command flakes"147  say "  once nina is installed: nina setup enables this automatically"148  blank149  return 1150}151152# ── NixOS: nix-shell source build (fallback) ──────────────────────────────────153154nixos_shell_build() {155  have nix-shell || return 1156  say "fetching source and building nina using nix-shell..."157  blank158  srcdir="$(fetch_source)"159160  # Try the user's channel first; fall back to a pinned nixpkgs-unstable if161  # zig_0_16 is not yet in the channel (common on NixOS 24.11 stable).162  build_cmd="cd '${srcdir}' && zig build -Doptimize=ReleaseSafe"163  if nix-shell -p zig_0_16 --run "$build_cmd" 2>/dev/null; then164    :165  else166    say "zig_0_16 not in channel — fetching from nixpkgs-unstable..."167    nix-shell \168      -I nixpkgs=https://releases.nixos.org/nixos/unstable/nixos-unstable/nixexprs.tar.xz \169      -p zig_0_16 \170      --run "$build_cmd" \171      || die "build failed — see output above"172  fi173174  mkdir -p "$INSTALL_DIR"175  cp "${srcdir}/zig-out/bin/nina" "${INSTALL_DIR}/nina"176  chmod +x "${INSTALL_DIR}/nina"177  blank178  say "installed from source → ${INSTALL_DIR}/nina"179  say "note: binary built outside nix — adopt ~/nina.nix into configuration.nix"180  say "      to make nina fully nix-managed (nina goodbye handles removal either way)"181}182183# ── generate ~/nina.nix ───────────────────────────────────────────────────────184185generate_nixos_module() {186  # Write ~/nina.nix without invoking shell substitution on the nix code inside.187  cat > "$HOME/nina.nix" <<'NIXEOF'188# ~/nina.nix — generated by the nina installer189#190# Declares nina as a NixOS system package.191#192# Add to /etc/nixos/configuration.nix:193#   imports = [ /home/YOUR_USER/nina.nix ];194# Then: sudo nixos-rebuild switch195#196# To update:  nina update  (or re-run the installer)197# To remove:  nina goodbye  (or delete this file, then nixos-rebuild switch)198#199# For flake-based configurations, add nina as a flake input instead:200#   inputs.nina.url = "https://kepr.uk/nina/archive/HEAD.tar.gz";201#   # in your nixosModules list: nina.nixosModules.default202#   # then set: programs.nina.enable = true;203{ ... }:204{205  imports = [206    "${builtins.fetchTarball "https://kepr.uk/nina/archive/HEAD.tar.gz"}/nixos-module.nix"207  ];208  programs.nina.enable = true;209}210NIXEOF211  blank212  say "generated ~/nina.nix for system-wide installation:"213  say "  1. add to /etc/nixos/configuration.nix:"214  say "       imports = [ /home/$(id -un)/nina.nix ];"215  say "  2. sudo nixos-rebuild switch"216}217218# ── Zig version check ─────────────────────────────────────────────────────────219220check_zig() {221  if ! have zig; then222    blank223    say "Zig not found. Building nina requires Zig 0.${MIN_ZIG_MINOR}+."224    blank225    say "  Download: https://ziglang.org/download/"226    if is_nixos; then227      say "  NixOS:    nix-shell -p zig_0_16 --run 'sh install.sh'"228    fi229    blank230    die "Zig 0.${MIN_ZIG_MINOR}+ required"231  fi232233  zig_ver="$(zig version 2>/dev/null)"234  major="$(printf '%s' "$zig_ver" | cut -d. -f1)"235  minor="$(printf '%s' "$zig_ver" | cut -d. -f2)"236  ok=0237  [ "$major" -ge 1 ] 2>/dev/null && ok=1238  [ "$major" -eq 0 ] 2>/dev/null && [ "$minor" -ge "$MIN_ZIG_MINOR" ] 2>/dev/null && ok=1239240  if [ "$ok" -ne 1 ]; then241    blank242    say "found zig ${zig_ver}, but nina requires Zig 0.${MIN_ZIG_MINOR}+."243    if is_nixos; then244      say "  run: nix-shell -p zig_0_16 --run 'sh install.sh'"245    else246      say "  update: https://ziglang.org/download/"247    fi248    blank249    die "Zig 0.${MIN_ZIG_MINOR}+ required; found ${zig_ver}"250  fi251252  say "zig ${zig_ver} found"253}254255# ── generic source build (non-NixOS) ─────────────────────────────────────────256257build_from_source() {258  say "building from source..."259  check_zig260261  if [ -f "build.zig" ] && [ -f "src/main.zig" ]; then262    srcdir="."263  else264    srcdir="$(fetch_source)"265  fi266267  (cd "$srcdir" && zig build -Doptimize=ReleaseSafe) \268    || die "build failed — see output above"269270  mkdir -p "$INSTALL_DIR"271  cp "${srcdir}/zig-out/bin/nina" "${INSTALL_DIR}/nina"272  chmod +x "${INSTALL_DIR}/nina"273  say "installed from source → ${INSTALL_DIR}/nina"274}275276# ── PATH reminder ─────────────────────────────────────────────────────────────277278check_path() {279  case ":${PATH}:" in280    *":${INSTALL_DIR}:"*) ;;281    *)282      blank283      say "${INSTALL_DIR} is not in your PATH. Add it:"284      say "  export PATH=\"\${PATH}:${INSTALL_DIR}\""285      say "  echo 'export PATH=\"\${PATH}:${INSTALL_DIR}\"' >> ~/.bashrc"286      if is_nixos; then287        say "  NixOS: add to environment.sessionVariables in configuration.nix"288      fi289      ;;290  esac291}292293# ── enable nix experimental features ──────────────────────────────────────────294# nina needs nix-command + flakes to search and install. If they're off, offer to295# turn them on via the freshly-installed `nina setup`. Read from /dev/tty so the296# prompt works even under `curl … | sh`, where stdin is the script itself.297298maybe_setup_features() {299  have nix || return 0300  # `nix eval` itself requires nix-command, so this doubles as the feature probe.301  nix eval --expr '1' >/dev/null 2>&1 && return 0302303  NINA="${INSTALL_DIR}/nina"304  [ -x "$NINA" ] || NINA="nina"305306  blank307  say "nina needs nix's experimental features (nix-command and flakes) to"308  say "search and install packages."309310  if [ -r /dev/tty ]; then311    printf 'nina: enable them now? [Y/n] ' > /dev/tty312    IFS= read -r ans < /dev/tty 2>/dev/null || ans="y"313    case "$ans" in314      [Nn]*) say "skipped — enable later with:  nina setup"; return 0 ;;315    esac316    "$NINA" setup || say "automatic setup failed — run:  nina setup"317  else318    say "run  nina setup  to enable them"319  fi320}321322# ── closing message ──────────────────────────────────────────────────────────323324farewell() {325  printf '\n'326  printf '  thank you!! running nina update occasionally will keep me in tip top shape.\n'327  printf '  get started by running nina hello\n'328  printf '\n'329  printf '  if nina turns out not to be for you, nina goodbye will remove her cleanly.\n'330  printf '\n'331}332333# ── main ──────────────────────────────────────────────────────────────────────334335banner336337if is_nixos; then338  say "NixOS detected"339  if nixos_profile_install; then340    generate_nixos_module341    maybe_setup_features342    farewell343    exit 0344  fi345  nixos_shell_build346  generate_nixos_module347  check_path348  maybe_setup_features349  farewell350  exit 0351fi352353# macOS / generic Linux: download binary, fall back to source build354if install_binary; then355  check_path356  maybe_setup_features357  farewell358  exit 0359fi360361say "no pre-built binary for ${platform} — falling back to source build"362build_from_source363check_path364maybe_setup_features365farewell