#!/usr/bin/env bash
# CI eval tier — same coverage as `nix flake check --no-build`, bounded
# memory. One evaluator process walking every output accumulates ~6.0 GB
# peak RSS on this repo (measured 2026-07-12, eval-cache off): that
# OOM'd the whole 4 GB VPS behind git.bemagri.xyz, and can never fit the
# runner's 2 GB container cap. Evaluating ONE output per fresh nix
# process caps the peak at the largest single output (~0.93 GB,
# downstream-template-system): the heap is freed when each process
# exits. Wall time is worse (shared nixpkgs re-eval per process);
# irrelevant next to the memory cap. nomarchy-live is skipped by
# decision (see the loop below); with it out, the worst output is
# hardware-toggles at 0.99 GB. Used by .gitea/workflows/check.yml
# and bump.yml; runs anywhere (`bash tools/ci-eval.sh`).
set -euo pipefail

sys=${1:-x86_64-linux}

list() {
  nix eval --raw ".#$1" \
    --apply 'a: builtins.concatStringsSep "\n" (builtins.attrNames a)'
}

evaluate() {
  echo "eval: $1"
  nix eval --raw ".#$1" >/dev/null
}

for a in $(list "checks.$sys"); do
  evaluate "checks.$sys.$a.drvPath"
done
for c in $(list nixosConfigurations); do
  # nomarchy-live (the installer ISO) is deliberately NOT CI-gated
  # (Bernardo, 2026-07-12): the ISO is built and distributed manually,
  # so its guard lives with that manual flow (local `nix flake check` /
  # the ISO build itself) — and it is the one output whose EVAL alone
  # (~2.7 GB) cannot fit the runner's 2 GB cap (Actions run 315: every
  # other output green, this one OOM-killed).
  if [ "$c" = "nomarchy-live" ]; then
    echo "skip: nixosConfigurations.$c (manual ISO flow — see comment)"
    continue
  fi
  evaluate "nixosConfigurations.$c.config.system.build.toplevel.drvPath"
done
for h in $(list homeConfigurations); do
  evaluate "homeConfigurations.$h.activationPackage.drvPath"
done
echo "ci-eval: all outputs evaluated."
