|
| 1 | +name: Nix cache push |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + |
| 10 | +env: |
| 11 | + NIX_CACHE_URL: https://nix.systemfsoftware.com |
| 12 | + NIX_CACHE_PUBLIC_KEY: nix-cache-1:wXA8I4DayNW9q6enUyQA/P/nT38pGSTLt374t3gsn7M= |
| 13 | + EXTRA_OUTPUTS: >- |
| 14 | + dprint |
| 15 | + dprint-unwrapped |
| 16 | +
|
| 17 | +jobs: |
| 18 | + push: |
| 19 | + name: push · ${{ matrix.system }} |
| 20 | + strategy: |
| 21 | + fail-fast: false |
| 22 | + matrix: |
| 23 | + include: |
| 24 | + - system: x86_64-linux |
| 25 | + runner: ubuntu-latest |
| 26 | + - system: aarch64-linux |
| 27 | + runner: ubuntu-24.04-arm |
| 28 | + runs-on: ${{ matrix.runner }} |
| 29 | + environment: nix-cache-push |
| 30 | + timeout-minutes: 90 |
| 31 | + steps: |
| 32 | + - id: start |
| 33 | + name: Record the job start |
| 34 | + run: echo "at=$(date +%s)" >> "$GITHUB_OUTPUT" |
| 35 | + - name: The R2 push values are set |
| 36 | + env: |
| 37 | + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 38 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 39 | + NIX_CACHE_R2_ENDPOINT: ${{ secrets.NIX_CACHE_R2_ENDPOINT }} |
| 40 | + run: | |
| 41 | + set -euo pipefail |
| 42 | + missing=0 |
| 43 | + for name in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY NIX_CACHE_R2_ENDPOINT; do |
| 44 | + if [ -z "${!name}" ]; then |
| 45 | + echo "::error title=nix-cache-push::${name} is not set in the nix-cache-push environment, so nothing was built or pushed" |
| 46 | + missing=1 |
| 47 | + fi |
| 48 | + done |
| 49 | + exit "$missing" |
| 50 | + - uses: DeterminateSystems/nix-installer-action@3138316df39ed29be04236d7ffc686fa525866aa |
| 51 | + with: |
| 52 | + extra-conf: | |
| 53 | + access-tokens = github.com=${{ github.token }} |
| 54 | + extra-substituters = ${{ env.NIX_CACHE_URL }} |
| 55 | + extra-trusted-public-keys = ${{ env.NIX_CACHE_PUBLIC_KEY }} |
| 56 | +
|
| 57 | + - name: The signing key is the cache's key |
| 58 | + env: |
| 59 | + NIX_CACHE_SIGNING_KEY: ${{ secrets.NIX_CACHE_SIGNING_KEY }} |
| 60 | + run: | |
| 61 | + set -euo pipefail |
| 62 | + : "${NIX_CACHE_SIGNING_KEY:?NIX_CACHE_SIGNING_KEY is not set}" |
| 63 | + umask 077 |
| 64 | + printf '%s' "$NIX_CACHE_SIGNING_KEY" > "$RUNNER_TEMP/signing-key" |
| 65 | + derived=$(nix --extra-experimental-features nix-command key convert-secret-to-public < "$RUNNER_TEMP/signing-key") |
| 66 | + if [ "$derived" != "$NIX_CACHE_PUBLIC_KEY" ]; then |
| 67 | + echo "::error title=nix-cache-key::NIX_CACHE_SIGNING_KEY does not derive the cache's public key; nothing was pushed" |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | +
|
| 71 | + - name: Build the push set from the pushed commit |
| 72 | + env: |
| 73 | + SYSTEM: ${{ matrix.system }} |
| 74 | + run: | |
| 75 | + set -euo pipefail |
| 76 | + pinned="github:${GITHUB_REPOSITORY}/${GITHUB_SHA}" |
| 77 | + read -ra extras <<< "$EXTRA_OUTPUTS" |
| 78 | + mapfile -t attrs < <(printf '%s\n' "${extras[@]}" | awk '!seen[$0]++') |
| 79 | + : > "$RUNNER_TEMP/out-paths" |
| 80 | + : > "$RUNNER_TEMP/build-failures" |
| 81 | + build() { |
| 82 | + if out=$(nix build --no-link --print-out-paths -L "${pinned}#$1"); then |
| 83 | + printf '%s\n' "$out" >> "$RUNNER_TEMP/out-paths" |
| 84 | + else |
| 85 | + echo "$1" >> "$RUNNER_TEMP/build-failures" |
| 86 | + echo "::error title=nix-build-failed::$1 did not build" |
| 87 | + fi |
| 88 | + } |
| 89 | + for attr in "${attrs[@]}"; do |
| 90 | + build "packages.${SYSTEM}.${attr}" |
| 91 | + done |
| 92 | +
|
| 93 | + - name: Push the closures and check the cache serves them |
| 94 | + env: |
| 95 | + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 96 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 97 | + NIX_CACHE_R2_ENDPOINT: ${{ secrets.NIX_CACHE_R2_ENDPOINT }} |
| 98 | + run: | |
| 99 | + set -euo pipefail |
| 100 | + echo "::add-mask::${NIX_CACHE_R2_ENDPOINT#https://}" |
| 101 | + mapfile -t outs < "$RUNNER_TEMP/out-paths" |
| 102 | + if [ "${#outs[@]}" -eq 0 ]; then |
| 103 | + echo "::error title=nix-cache-push::nothing built, so nothing was pushed" |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + cat > "$RUNNER_TEMP/check-served.sh" << 'EOF' |
| 107 | + path=$1 |
| 108 | + name=${path##*/} |
| 109 | + body=$(mktemp) |
| 110 | + code=$(curl -sS -m 30 --retry 2 -o "$body" -w "%{http_code}" "$NIX_CACHE_URL/${name%%-*}.narinfo" 2> /dev/null) || code=unreachable |
| 111 | + if [ "$code" != 200 ]; then |
| 112 | + echo "$path: HTTP $code" >> "$MISSES" |
| 113 | + elif ! grep -q "^Sig: nix-cache-1:" "$body"; then |
| 114 | + echo "$path: no signature from nix-cache-1" >> "$MISSES" |
| 115 | + fi |
| 116 | + rm -f "$body" |
| 117 | + EOF |
| 118 | + failed=0 |
| 119 | + if nix copy --to "s3://systemfsoftware-nix-cache?endpoint=${NIX_CACHE_R2_ENDPOINT#https://}®ion=auto&scheme=https&compression=zstd&multipart-upload=true&secret-key=${RUNNER_TEMP}/signing-key" "${outs[@]}" 2> "$RUNNER_TEMP/copy.err"; then |
| 120 | + export NIX_CACHE_URL MISSES="$RUNNER_TEMP/cache-misses" |
| 121 | + : > "$MISSES" |
| 122 | + nix-store --query --requisites "${outs[@]}" > "$RUNNER_TEMP/pushed-paths" |
| 123 | + xargs -P 16 -n 1 bash "$RUNNER_TEMP/check-served.sh" < "$RUNNER_TEMP/pushed-paths" |
| 124 | + if [ -s "$MISSES" ]; then |
| 125 | + failed=1 |
| 126 | + head -n 10 "$MISSES" | while read -r miss; do |
| 127 | + echo "::error title=nix-cache-verify::${miss}" |
| 128 | + done |
| 129 | + { |
| 130 | + echo "The cache does not serve, or has not signed, these pushed paths:" |
| 131 | + sed 's/^/- /' "$MISSES" |
| 132 | + } >> "$GITHUB_STEP_SUMMARY" |
| 133 | + else |
| 134 | + echo "Every one of the $(wc -l < "$RUNNER_TEMP/pushed-paths") pushed paths is served with a nix-cache-1 signature." >> "$GITHUB_STEP_SUMMARY" |
| 135 | + fi |
| 136 | + else |
| 137 | + failed=1 |
| 138 | + reason=$(sed -n '/error: ./{s/^[[:space:]]*//;p;q}' "$RUNNER_TEMP/copy.err" | cut -c1-300) |
| 139 | + echo "::error title=nix-cache-push::the push failed: ${reason:-nix copy failed}" |
| 140 | + echo "The push failed: ${reason:-nix copy failed}" >> "$GITHUB_STEP_SUMMARY" |
| 141 | + fi |
| 142 | + if [ -s "$RUNNER_TEMP/build-failures" ]; then |
| 143 | + failed=1 |
| 144 | + { |
| 145 | + echo "These did not build, so they were not pushed:" |
| 146 | + sed 's/^/- /' "$RUNNER_TEMP/build-failures" |
| 147 | + } >> "$GITHUB_STEP_SUMMARY" |
| 148 | + fi |
| 149 | + exit "$failed" |
| 150 | +
|
| 151 | + - name: Remove the signing key |
| 152 | + if: always() |
| 153 | + run: rm -f "$RUNNER_TEMP/signing-key" "$RUNNER_TEMP/copy.err" |
| 154 | + |
| 155 | + - name: Record the job's wall time |
| 156 | + if: always() |
| 157 | + env: |
| 158 | + START: ${{ steps.start.outputs.at }} |
| 159 | + SYSTEM: ${{ matrix.system }} |
| 160 | + run: | |
| 161 | + echo "nix cache push wall time (${SYSTEM}): $(( $(date +%s) - START ))s" >> "$GITHUB_STEP_SUMMARY" |
0 commit comments