diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 0617aff..0e5a218 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -52,31 +52,4 @@ jobs: env: HARMONIA_SSH_KEY: ${{ secrets.HARMONIA_SSH_KEY }} CI_IFD_REQUIRED: ${{ steps.ifd.outputs.required }} - run: | - set -o pipefail - nix_options=(--option allow-import-from-derivation "$CI_IFD_REQUIRED") - - echo "::group::Collect cache garbage" - ci/push-to-cache.sh --gc - echo "::endgroup::" - - nix eval "${nix_options[@]}" --json --apply "builtins.attrNames" .#ci.x86_64-linux | jq -cr '.[]' | while read job; do - echo "::group::Build $job" - nix build "${nix_options[@]}" --no-link .#ci.x86_64-linux."$job" - echo "::endgroup::" - - echo "::group::Cache $job" - ci/push-to-cache.sh "$(nix eval "${nix_options[@]}" --raw .#ci.x86_64-linux."$job")" - echo "::endgroup::" - done - - if [[ "$CI_IFD_REQUIRED" == true ]]; then - echo "::group::Check flake" - # The target closures are available for the evaluation-time reference graphs. - nix flake check "${nix_options[@]}" - echo "::endgroup::" - fi - - echo "Building and caching CI derivation" - nix build "${nix_options[@]}" --no-link .#ciDrv.x86_64-linux - UPDATE_PROFILE=1 ci/push-to-cache.sh "$(nix eval "${nix_options[@]}" --raw .#ciDrv.x86_64-linux)" + run: ci/build-and-cache.sh diff --git a/ci/build-and-cache.sh b/ci/build-and-cache.sh new file mode 100755 index 0000000..0981949 --- /dev/null +++ b/ci/build-and-cache.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +# Evaluate, build and cache every attribute of `.#ci.`, then point the +# cache's `nixfiles` profile at a link farm of the results. +# +# CI_IFD_REQUIRED=true allows import from derivation and runs the flake check +# after the builds, when the evaluation-time reference graphs are available. +set -euo pipefail + +system=x86_64-linux +allow_ifd="${CI_IFD_REQUIRED:-false}" + +# Every evaluation worker first pays for a shared evaluation of the flake (a +# few GiB), so keep workers alive for several jobs. workers * memory is the +# evaluation budget; it must leave room on the runner for Gitea and for builds +# started by import from derivation. +eval_workers="${CI_EVAL_WORKERS:-4}" +eval_worker_memory="${CI_EVAL_WORKER_MEMORY:-6144}" + +nix_options=(--option allow-import-from-derivation "$allow_ifd") +work="$(mktemp -d)" +failed=0 + +echo "::group::Collect cache garbage" +ci/push-to-cache.sh --gc +echo "::endgroup::" + +echo "::group::Evaluate" +nix_eval_jobs="$(nix build --no-link --print-out-paths ".#nixpkgs.unstable.$system.nix-eval-jobs")/bin/nix-eval-jobs" +"$nix_eval_jobs" "${nix_options[@]}" \ + --workers "$eval_workers" --max-memory-size "$eval_worker_memory" \ + --gc-roots-dir "$work/gc-roots" \ + --flake ".#ci.$system" > "$work/jobs.json" || failed=1 +echo "::endgroup::" + +# Workflow commands are single lines, so escape the multi-line errors +jq -r 'select(.error) | + "::error title=Evaluation of \(.attrPath[0]) failed::" + + (.error | gsub("\u001b\\[[0-9;]*m"; "") | + gsub("%"; "%25") | gsub("\r"; "%0D") | gsub("\n"; "%0A"))' "$work/jobs.json" +jq -s -e 'any(.error)' "$work/jobs.json" > /dev/null && failed=1 + +jq -s 'map(select(.drvPath) | { (.attrPath[0]): .outputs.out }) | add // { }' \ + "$work/jobs.json" > "$work/outputs.json" +mapfile -t installables < <(jq -r 'select(.drvPath) | .drvPath + "^out"' "$work/jobs.json") + +# One build lets independent jobs build in parallel +if (( ${#installables[@]} )); then + echo "::group::Build" + nix build --keep-going --no-link "${installables[@]}" || failed=1 + echo "::endgroup::" +fi + +built=() +while IFS=$'\t' read -r job path; do + if nix-store --check-validity "$path" 2> /dev/null; then + built+=("$path") + else + echo "::error title=Build of $job failed::See the build log above." + fi +done < <(jq -r 'to_entries[] | "\(.key)\t\(.value)"' "$work/outputs.json") + +if (( ${#built[@]} )); then + echo "::group::Cache" + ci/push-to-cache.sh "${built[@]}" + echo "::endgroup::" +fi + +if (( failed )); then + exit 1 +fi + +if [[ "$allow_ifd" == true ]]; then + echo "::group::Check flake" + nix flake check "${nix_options[@]}" + echo "::endgroup::" +fi + +# Build the link farm from the known outputs rather than evaluating them again +echo "::group::Update cache profile" +farm_drv="$(nix eval --impure --raw ".#nixpkgs.unstable.$system" --apply "pkgs: + (pkgs.linkFarm \"ci\" (builtins.mapAttrs (_: builtins.storePath) + (builtins.fromJSON (builtins.readFile \"$work/outputs.json\")))).drvPath")" +farm="$(nix build --no-link --print-out-paths "$farm_drv^out")" +UPDATE_PROFILE=1 ci/push-to-cache.sh "$farm" +echo "::endgroup::" diff --git a/ci/push-to-cache.sh b/ci/push-to-cache.sh index 45e9bdd..21dee7e 100755 --- a/ci/push-to-cache.sh +++ b/ci/push-to-cache.sh @@ -31,12 +31,15 @@ if [ "${1-}" = "--gc" ]; then exit fi -path="$1" +if [ -n "${UPDATE_PROFILE-}" ] && [ $# -ne 1 ]; then + echo "UPDATE_PROFILE needs exactly one path" >&2 + exit 1 +fi -echo "Pushing $path to cache..." -nix copy --no-check-sigs --to "$STORE_URI" "$path" +echo "Pushing $# path(s) to cache..." +nix copy --no-check-sigs --to "$STORE_URI" "$@" -if [ -n "$UPDATE_PROFILE" ]; then +if [ -n "${UPDATE_PROFILE-}" ]; then echo "Updating profile..." - remote_cmd nix-env -p "$REMOTE_STORE"/nix/var/nix/profiles/nixfiles --set "$path" + remote_cmd nix-env -p "$REMOTE_STORE"/nix/var/nix/profiles/nixfiles --set "$1" fi diff --git a/docs/architecture.md b/docs/architecture.md index 44dc2e2..3eb1903 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -51,8 +51,7 @@ Per platform (`eachDefaultSystem`), the flake produces: - `devShells.default` — the `numtide/devshell` from [`devshell/`](../devshell) (see [deployment.md](deployment.md)). - `ci.` — one attr per buildable thing (`system-`, `home-` with `@` mangled - to `-at-`, `package-`, plus `shell`), consumed by CI; `ciDrv` is a `linkFarm` of all of - them. + to `-at-`, `package-`, plus `shell`), consumed by CI. ## Systems: `systemOpts` and `mkSystem` diff --git a/docs/deployment.md b/docs/deployment.md index e4e209c..f269886 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -302,26 +302,40 @@ GitHub/Gitea Actions workflows live in [`.gitea/workflows/`](../.gitea/workflows On pushes to `master`, this installs Determinate Nix on the runner (via `DeterminateSystems/determinate-nix-action`, configured with the same performance settings and -Harmonia substituter as the boxes). CI normally runs `nix flake check --no-build` first and -keeps import from derivation disabled. It builds every attribute of `.#ci.x86_64-linux`: -systems as `system-`, homes as `home-` (with `@` changed to `-at-`), packages as -`package-`, and the development `shell`. Each result is pushed to the -Harmonia cache with [`ci/push-to-cache.sh`](../ci/push-to-cache.sh). Before the first push, the -workflow deletes cache-profile generations older than its retention period, then collects -unreachable paths so a full cache cannot prevent collection from being reached. Paths pushed by -`slop build` survive collection through their own GC roots (see -[`slop-broker.md`](slop-broker.md#binary-cache)). +Harmonia substituter as the boxes). A newer push cancels a run still in progress. CI normally +runs `nix flake check --no-build` first and keeps import from derivation disabled. It then runs +[`ci/build-and-cache.sh`](../ci/build-and-cache.sh), which builds every attribute of +`.#ci.x86_64-linux`: systems as `system-`, homes as `home-` (with `@` changed to +`-at-`), packages as `package-`, and the development `shell`. + +The script works in stages: + +- **Collect garbage.** It deletes cache-profile generations older than their retention period, + then collects unreachable paths so a full cache cannot prevent collection from being reached. + Paths pushed by `slop build` survive collection through their own GC roots (see + [`slop-broker.md`](slop-broker.md#binary-cache)). +- **Evaluate.** [`nix-eval-jobs`](https://github.com/nix-community/nix-eval-jobs) evaluates the + attributes in a pool of workers. Each worker reuses one evaluation of the flake for several + jobs, and the worker count and per-worker memory bound (`CI_EVAL_WORKERS`, + `CI_EVAL_WORKER_MEMORY`) cap the evaluation's memory on the runner. +- **Build.** One `nix build --keep-going` builds every evaluated derivation, so a failing job + doesn't stop the others. +- **Cache.** Every output that built is pushed to the Harmonia cache with + [`ci/push-to-cache.sh`](../ci/push-to-cache.sh). Any evaluation or build failure fails the run + at this point. +- **Update the profile.** A `linkFarm` of all the outputs, built from their known store paths + rather than a second evaluation, is pushed with `UPDATE_PROFILE=1`. That updates the + `nixfiles` profile on the cache box. When `.#ciIfdReasons.x86_64-linux` lists an active exception, CI logs a warning with the reason -and allows import from derivation for the target builds. It runs a build-capable flake -check afterward. CI checks [the shared runtime replacement list](../lib/runtime-dependency-replacements.nix) +and allows import from derivation for the evaluation. The evaluation workers then wait on those +builds in parallel. CI runs a build-capable flake check after caching and before updating the +profile. CI checks [the shared runtime replacement list](../lib/runtime-dependency-replacements.nix) against the nixpkgs channels used by its systems and standalone homes. A matching entry requires reading generated store reference graphs during evaluation. -It then builds `.#ciDrv.x86_64-linux`, a `linkFarm` of all CI attributes, and pushes it with -`UPDATE_PROFILE=1`. That updates the `nixfiles` profile on the cache box. The SSH store uses -`/var/lib/harmonia`, `HARMONIA_SSH_KEY`, and pinned `ci/known_hosts`; clients use -`https://nix-cache.nul.ie` through `lib.my.c.nix.cache`. +The SSH store uses `/var/lib/harmonia`, `HARMONIA_SSH_KEY`, and pinned `ci/known_hosts`; clients +use `https://nix-cache.nul.ie` through `lib.my.c.nix.cache`. ### `installer.yaml` diff --git a/flake.nix b/flake.nix index 18f7ede..3144321 100644 --- a/flake.nix +++ b/flake.nix @@ -303,7 +303,6 @@ homes' // systems' // packages' // { inherit shell; }; - ciDrv = pkgs.linkFarm "ci" ci; ciIfdReasons = let