# Nix Binary Cache

import { Aside } from '@astrojs/starlight/components';

Avrea provides a Nix binary cache endpoint that acts as a substituter. Store
paths built on one run are available to all subsequent runs without rebuilding.

<Aside type="caution" title="Work in progress">
Nix binary cache support is functional but the setup is not yet fully automatic.
Most CI environments use `install-nix-action`, which overwrites `nix.conf` on
install. You need to re-add the substituter configuration after Nix is installed.
This will get easier in a future update.
</Aside>

## On Avrea runners

The Nix substituter needs to be configured after Nix is installed, because
`install-nix-action` overwrites `nix.conf`. Add a step to append the cache
configuration:

```yaml title="workflow.yml"
jobs:
  build:
    runs-on: avrea-ubuntu-latest
    steps:
      - uses: actions/checkout@v7

      - name: Configure Avrea nix binary cache
        run: |
          cat << 'NIXCONF' | sudo tee -a /etc/nix/nix.conf > /dev/null
          trusted-users = root runner
          substituters = http://cache.avrea.com:8290/nix-build/cache https://cache.nixos.org
          trusted-substituters = http://cache.avrea.com:8290/nix-build/cache https://cache.nixos.org
          require-sigs = false
          NIXCONF
          sudo systemctl restart nix-daemon

      - name: Build
        run: nix build --no-link .#default

      - name: Push to Avrea binary cache
        run: nix copy --to http://cache.avrea.com:8290/nix-build/cache .#default
```

<Aside>
The `nix copy` step pushes newly built store paths to the cache. Use a flake
reference (e.g. `.#default`) rather than `./result` so the command works with
`--no-link`. Paths already in the cache are substituted automatically on future
builds.
</Aside>

## Manual setup

```yaml
- name: Configure Avrea nix binary cache
  run: |
    cat << 'NIXCONF' | sudo tee -a /etc/nix/nix.conf > /dev/null
    trusted-users = root runner
    substituters = http://cache.avrea.com:8290/nix-build/cache https://cache.nixos.org
    trusted-substituters = http://cache.avrea.com:8290/nix-build/cache https://cache.nixos.org
    require-sigs = false
    NIXCONF
    sudo systemctl restart nix-daemon

- run: nix build --no-link .#default
- run: nix copy --to http://cache.avrea.com:8290/nix-build/cache .#default
```