29642d33c40e

add offer_config_import: prompt to append ~/nina.nix into an existing imports list in /etc/nixos/configuration.nix (backs up first, awk-inserts as first list entry, idempotent, manual fallback); reformat post-install output into a spaced 'system-wide install' section with unprefixed out/hr helpers instead of mashed nina: lines

time
2026-06-05 16:26 UTC · 4 months ago
machine
koh2
agent
claude-opus-4-8
session
clean up nixos install output
parent
c89bd7b1bf37
1 file install.sh+91 −4
install.sh+91 −4
@@ -28,6 +28,11 @@2828 blank() { printf '\n'; }2929 is_nixos() { [ -e /etc/NIXOS ]; }3030 31+# Unprefixed output for the closing sections, where the "nina:" stutter on32+# every line hurts readability. `out` indents body text; `hr` draws a divider.33+out() { printf '  %s\n' "$1"; }34+hr()  { printf '  ────────────────────────────────────────────────\n'; }35+3136 fetch_text() {3237   if have curl;   then curl -fsSL "$1"3338   elif have wget; then wget -qO- "$1"@@ -187,6 +192,68 @@187192 188193 # ── generate ~/nina.nix ───────────────────────────────────────────────────────189194 195+# Offer to wire ~/nina.nix into /etc/nixos/configuration.nix. Returns 0 when the196+# import is in place (added now, or already present), 1 when the caller should197+# fall back to printing manual instructions. Conservative by design: only edits198+# an existing `imports = [ … ]` list, always backs the file up first, and never199+# touches anything without an explicit yes from the terminal.200+offer_config_import() {201+  config="$1"202+  nina_nix="$2"203+204+  [ -f "$config" ] && [ -r "$config" ] || return 1205+206+  # Already wired up — just remind how to apply it.207+  if grep -qF "$nina_nix" "$config" 2>/dev/null; then208+    out "Already imported in ${config}."209+    out "Apply with:  sudo nixos-rebuild switch"210+    blank211+    return 0212+  fi213+214+  # Need an existing imports list to extend; guessing where else to put it215+  # risks producing a config that won't evaluate.216+  grep -qE '^[[:space:]]*imports[[:space:]]*=[[:space:]]*\[' "$config" || return 1217+218+  # Read the answer from the terminal so it works under `curl … | sh`.219+  [ -r /dev/tty ] || return 1220+  printf '  add this import to %s now? [y/N] ' "$config" > /dev/tty221+  IFS= read -r ans < /dev/tty 2>/dev/null || ans=""222+  case "$ans" in223+    [Yy]*) ;;224+    *) return 1 ;;225+  esac226+227+  ts="$(date +%Y%m%d%H%M%S 2>/dev/null || echo backup)"228+  backup="${config}.nina-bak.${ts}"229+  sudo cp "$config" "$backup" 2>/dev/null \230+    || { blank; out "could not back up ${config} — add it manually instead."; blank; return 1; }231+232+  # Insert our path as the first entry of the first imports list.233+  tmp="$(mktemp /tmp/nina-conf.XXXXXX)"234+  awk -v p="$nina_nix" '235+    !done && /imports[[:space:]]*=[[:space:]]*\[/ { sub(/\[/, "[ " p); done=1 }236+    { print }237+  ' "$config" > "$tmp" 2>/dev/null238+239+  if ! sudo cp "$tmp" "$config" 2>/dev/null; then240+    rm -f "$tmp"241+    blank242+    out "could not write ${config}."243+    out "restore the backup with:  sudo cp ${backup} ${config}"244+    blank245+    return 1246+  fi247+  rm -f "$tmp"248+249+  blank250+  out "✓ added the import to ${config}"251+  out "  backup kept at  ${backup}"252+  out "  apply with      sudo nixos-rebuild switch"253+  blank254+  return 0255+}256+190257 generate_nixos_module() {191258   # Write ~/nina.nix without invoking shell substitution on the nix code inside.192259   cat > "$HOME/nina.nix" <<'NIXEOF'@@ -213,11 +280,31 @@213280   programs.nina.enable = true;214281 }215282 NIXEOF283+284+  nina_nix="$HOME/nina.nix"285+216286   blank217-  say "generated ~/nina.nix for system-wide installation:"218-  say "  1. add to /etc/nixos/configuration.nix:"219-  say "       imports = [ /home/$(id -un)/nina.nix ];"220-  say "  2. sudo nixos-rebuild switch"287+  hr288+  out "system-wide install  (optional)"289+  hr290+  blank291+  out "nina is installed for your user. To manage her with the rest of"292+  out "your system, a NixOS module has been written to:"293+  blank294+  out "    ${nina_nix}"295+  blank296+297+  if offer_config_import "/etc/nixos/configuration.nix" "$nina_nix"; then298+    return 0299+  fi300+301+  out "To enable it yourself, add this line to /etc/nixos/configuration.nix"302+  out "(inside the existing  imports = [ … ];  list):"303+  blank304+  out "    ${nina_nix}"305+  blank306+  out "then run:  sudo nixos-rebuild switch"307+  blank221308 }222309 223310 # ── Zig version check ─────────────────────────────────────────────────────────

view file