diff --git a/.github/actions/install-nix/setup-nix.sh b/.github/actions/install-nix/setup-nix.sh index 221c88b64d..e3b05a9bde 100755 --- a/.github/actions/install-nix/setup-nix.sh +++ b/.github/actions/install-nix/setup-nix.sh @@ -35,6 +35,10 @@ cat >"$tmpdir/nix.conf" <<-EOF max-jobs = 5 EOF +if [[ -d /mnt/nix-store-disk ]]; then + echo 'extra-substituters = local?root=/mnt/nix-store-disk' >>"$tmpdir/nix.conf" +fi + if [[ -e /dev/kvm ]]; then sudo chown runner /dev/kvm sudo chmod 666 /dev/kvm diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index 47aba54a59..83932274d3 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -66,11 +66,16 @@ workflows: - 'actions/download-artifact@v8.0.2' - 'actions/upload-artifact@v7.0.2' - 'aws-actions/configure-aws-credentials@v6.3.0' + - 'useblacksmith/stickydisk@v1.8.0' '.github/workflows/nix-dependency-analysis.yml': - 'actions/checkout@v7.0.1' - 'aws-actions/configure-aws-credentials@v6.3.0' - 'peter-evans/create-or-update-comment@v5.0.0' - 'peter-evans/find-comment@v4.0.0' + '.github/workflows/nix-store-disk.yml': + - 'actions/checkout@v7.0.1' + - 'aws-actions/configure-aws-credentials@v6.3.0' + - 'useblacksmith/stickydisk@v1.8.0' '.github/workflows/public-integration-doorbell.yml': [] '.github/workflows/publish-migrations-prod.yml': - 'actions/checkout@v7.0.1' @@ -238,3 +243,8 @@ dependencies: commit: 'sha1-8a0cd651e5b2365bd640854a3fa5dfc70e4875f3' owner_id: 2242 repo_id: 204253808 + 'useblacksmith/stickydisk@v1.8.0': + ref: 'v1.8.0' + commit: 'sha1-61dac6a8aa5927f7d3343d9dbdc8153787d08a1c' + owner_id: 156274793 + repo_id: 899799214 diff --git a/.github/workflows/nix-ci-per-system.yml b/.github/workflows/nix-ci-per-system.yml index 85f5f3380c..d02adb8fc1 100644 --- a/.github/workflows/nix-ci-per-system.yml +++ b/.github/workflows/nix-ci-per-system.yml @@ -111,6 +111,14 @@ jobs: - name: Checkout Repo if: ${{ matrix.attr != '' }} uses: actions/checkout@v7.0.1 + - name: Mount nix store disk + if: ${{ matrix.attr != '' && runner.os == 'Linux' }} + continue-on-error: true + uses: useblacksmith/stickydisk@v1.8.0 + with: + key: nix-store-${{ runner.arch }} + path: /mnt/nix-store-disk + commit: false - name: Install nix if: ${{ matrix.attr != '' }} uses: $/.github/actions/install-nix @@ -140,6 +148,14 @@ jobs: - name: Checkout Repo if: ${{ matrix.attr != '' }} uses: actions/checkout@v7.0.1 + - name: Mount nix store disk + if: ${{ matrix.attr != '' && runner.os == 'Linux' }} + continue-on-error: true + uses: useblacksmith/stickydisk@v1.8.0 + with: + key: nix-store-${{ runner.arch }} + path: /mnt/nix-store-disk + commit: false - name: Install nix if: ${{ matrix.attr != '' }} uses: $/.github/actions/install-nix @@ -167,6 +183,14 @@ jobs: - name: Checkout Repo if: ${{ matrix.attr != '' }} uses: actions/checkout@v7.0.1 + - name: Mount nix store disk + if: ${{ matrix.attr != '' && runner.os == 'Linux' }} + continue-on-error: true + uses: useblacksmith/stickydisk@v1.8.0 + with: + key: nix-store-${{ runner.arch }} + path: /mnt/nix-store-disk + commit: false - name: Install nix if: ${{ matrix.attr != '' }} uses: $/.github/actions/install-nix diff --git a/.github/workflows/nix-store-disk.yml b/.github/workflows/nix-store-disk.yml new file mode 100644 index 0000000000..e25c8aafab --- /dev/null +++ b/.github/workflows/nix-store-disk.yml @@ -0,0 +1,54 @@ +name: Nix store disk + +# The only workflow that commits the disk. CI jobs mount it with commit: false. + +on: + push: + branches: + - develop + workflow_dispatch: + +permissions: + contents: read + +jobs: + refresh: + strategy: + fail-fast: false + matrix: + runner: [blacksmith-8vcpu-ubuntu-2404, blacksmith-8vcpu-ubuntu-2404-arm] + runs-on: ${{ matrix.runner }} + concurrency: nix-store-disk-${{ matrix.runner }} + steps: + - name: Checkout Repo + uses: actions/checkout@v7.0.1 + + - name: Install nix + uses: $/.github/actions/install-nix + + - name: Mount nix store disk + uses: useblacksmith/stickydisk@v1.8.0 + with: + key: nix-store-${{ runner.arch }} + path: /mnt/nix-store-disk + + - name: Substitute build inputs onto the disk + run: | + set -euo pipefail + disk=/mnt/nix-store-disk + store="local?root=$disk" + roots=$disk/nix/var/nix/gcroots/ci + paths=$RUNNER_TEMP/paths + sudo chown "$(id -u)" "$disk" + + nix eval --json ".#ciBuildInputs.$(nix config show system)" | jq --raw-output '.[]' >"$paths" + + xargs --arg-file="$paths" --no-run-if-empty \ + nix build --store "$store" --max-jobs 0 --keep-going --no-link \ + || echo "::warning::some build inputs could not be substituted" + + rm -rf "$roots" + mkdir -p "$roots" + xargs --arg-file="$paths" ln --symbolic --target-directory="$roots" + nix store gc --store "$store" + df --human-readable "$disk" diff --git a/flake.nix b/flake.nix index be7e638352..10c3a662b7 100644 --- a/flake.nix +++ b/flake.nix @@ -49,6 +49,7 @@ imports = [ nix/apps.nix nix/checks.nix + nix/ci-build-inputs.nix nix/config.nix nix/devShells.nix nix/fmt.nix diff --git a/nix/ci-build-inputs.nix b/nix/ci-build-inputs.nix new file mode 100644 index 0000000000..f5c54548ed --- /dev/null +++ b/nix/ci-build-inputs.nix @@ -0,0 +1,34 @@ +{ + self, + lib, + config, + ... +}: +let + # NixOS tests are lazy derivations, which hide drvAttrs. + buildAttrs = drv: drv.drvAttrs or drv.config.rawTestDerivation.drvAttrs or { }; + + directInputs = + drv: + lib.concatLists ( + lib.mapAttrsToList ( + path: context: + map (output: builtins.unsafeDiscardStringContext (import path).${output}.outPath) ( + context.outputs or [ ] + ) + ) (builtins.getContext (builtins.toJSON (buildAttrs drv))) + ); +in +{ + flake.ciBuildInputs = lib.genAttrs config.systems ( + system: + lib.unique ( + lib.concatMap directInputs ( + lib.concatMap (lib.collect lib.isDerivation) [ + self.checks.${system} + self.legacyPackages.${system} + ] + ) + ) + ); +}