Making a Nix binary cache useful to other people
This playbook is not specific to Ledgeline. Copy it into any flake repo that wants
nix run github:you/project to download rather than compile. Substitute your
own cache name for <CACHE> and project name for <PROJ>.
Written 2026-08-04 against Cachix + GitHub Actions. Pricing and free-tier terms change, so re-check anything with a number in it.
The two jobs a cache does
A binary cache serves two audiences with opposite characteristics:
| CI dependency layer | Consumer release artifacts | |
|---|---|---|
| Size | large (often GBs) | small (tens of MB) |
| Churn | every lockfile/toolchain bump | only on release |
| Downloads | every CI run, so always warm | rare, often cold for weeks |
| Value if lost | a slow CI run | your install instructions stop working |
Cachix’s free tier garbage-collects least-recently-used. Under LRU the frequently used dependency layer survives and the rarely downloaded release artifact is evicted first, which is the opposite of what you want. LRU deletes the release artifacts first, and the missing paths make a working CI push look broken.
Step 1: work out what consumers resolve to
nix build .#<PROJ> in CI often builds something no user ever installs. List
what each user command resolves to:
| User command | Flake attribute |
|---|---|
nix build github:you/proj | packages.<system>.default |
nix profile install github:you/proj | packages.<system>.default |
nix run github:you/proj | apps.<system>.default (often a different derivation) |
nix develop | devShells.<system>.default |
Check each system separately. A flake that overrides default on darwin (with an .app
bundle, for example) has a different graph there than on Linux. Print the
evaluated paths rather than reading them off the flake source:
for s in x86_64-linux aarch64-darwin; do
nix eval --raw ".#packages.$s.default.outPath"; echo
nix eval --raw ".#apps.$s.default.program"; echo
done
A closure is not everything reachable from the attribute
Nix computes runtime references by scanning build outputs for store-path
strings. If a wrapper cps a binary into place rather than symlinking it,
the original derivation’s store path is not a reference, and pushing the
wrapper does not push the binary. Check it:
outer=$(nix eval --raw .#someWrapper.outPath)
inner=$(nix eval --raw .#theBinary.outPath)
nix path-info -r "$outer" | grep -qF "$inner" \
&& echo "in closure — pushing the wrapper is enough" \
|| echo "NOT in closure — must push it separately"
Name anything that fails that check explicitly when you push and pin.
Step 2: build the consumer artifacts in CI
CI can only push what it built. If packages.default differs by
platform, add a platform-guarded build step:
- name: Build the real distributable
if: runner.os == 'macOS'
run: nix build -L --no-link .#macDist
--no-link keeps a previously-created result symlink pointing where later
steps expect it.
Step 3: push, filtering out what you don’t need
- uses: cachix/cachix-action@v17
with:
name: <CACHE>
authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}
pushFilter: '-(source|vendor)$'
The action’s post-job daemon uploads everything the job built, so put this on every job, not only the build job.
pushFilteris a regex over store-path names. Source trees and vendor dirs waste quota, because consumers substituting a finished output never need them. Caveat from the docs: a filtered path is still uploaded if it is part of another path’s closure.- You do not need to filter nixpkgs. Cachix has skipped paths available
from
cache.nixos.orgby default since 2020 (configurable upstream list since Dec 2025). If your cache is full, your build products filled it.
Keep an explicit push step alongside the daemon so reviewers can see what gets
pushed, and so you can limit it to push events:
- name: Push to Cachix
if: github.event_name == 'push'
env:
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
run: |
if [ -z "${CACHIX_AUTH_TOKEN:-}" ]; then
echo "No CACHIX_AUTH_TOKEN (fork or unset) — skipping cache push."
exit 0
fi
attrs=(.#<PROJ>)
if [ "$RUNNER_OS" = "macOS" ]; then
attrs+=(.#macDist .#theBinary) # per the closure check in Step 1
fi
nix build --json "${attrs[@]}" \
| jq -r '.[].outputs | to_entries[].value' \
| cachix push <CACHE>
Always check that the token is non-empty. Fork PRs have no secrets, and
without the check every outside contributor’s PR fails. Use an if
block rather than [ ... ] && attrs+=(...): under bash -e a trailing &&
whose test fails makes the whole step exit non-zero.
Step 4: pin the consumer artifacts
- name: Pin release artifacts (exempt from GC)
if: github.event_name == 'push'
env:
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
run: |
if [ -z "${CACHIX_AUTH_TOKEN:-}" ]; then
echo "No CACHIX_AUTH_TOKEN — skipping pins."
exit 0
fi
sys=$(nix eval --raw --impure --expr builtins.currentSystem)
cachix pin <CACHE> "<PROJ>-$sys" \
"$(nix build --no-link --print-out-paths .#<PROJ>)" --keep-revisions 2
cachix pin CACHE-NAME PIN-NAME STORE-PATH [--keep-days N | --keep-revisions N | --keep-forever]
- Pinned paths are exempt from garbage collection. Pinning is what fixes the eviction problem.
- A pin name identifies the current release artifact for one system. Cachix
keeps
--keep-revisionsrevisions of each name, so re-pinning the same name on every push keeps a short history instead of growing forever. - Prefix pin names with the project if the cache is shared across repos.
- Derive
$sysinstead of hardcoding it, because runner architectures change. - Do not pin the dependency layer. It is the biggest thing you push and it changes constantly. CI keeps it warm by itself, and losing it costs one slow build, so let LRU evict it. Pinning it would use up the quota the pins are meant to protect.
Step 5: tell consumers how to opt in
Pushing alone is not enough. Nix ignores substituters declared in a flake’s
nixConfig unless the flake is trusted, so a stranger running your install
command builds from source and never knows why.
In the README, either use --accept-flake-config:
nix run --accept-flake-config github:you/project
or give them the permanent form for ~/.config/nix/nix.conf:
extra-substituters = https://<CACHE>.cachix.org
extra-trusted-public-keys = <CACHE>.cachix.org-1:AAAA...
Put this caveat in the README too. substituters is a trusted
setting. On a multi-user install, a user not in trusted-users gets
ignoring untrusted flake configuration setting 'extra-substituters' and
builds from source regardless of which method they used.
A cache is plain signed HTTP, so the Nix implementation does not matter.
Determinate Nix, upstream Nix, Lix and nix-darwin all read it the same way.
You do not need to push to several caches to support different machines.
The one exception is FlakeHub Cache, which requires determinate-nixd login.
That requirement also makes it a poor choice for public distribution.
Step 6: verify from outside, without building
Store paths are content-addressed, so evaluating a revision on any machine gives the same paths CI built. You can check coverage in seconds:
REV=github:you/project/<commit-sha>
for s in x86_64-linux aarch64-darwin; do
p=$(nix eval --raw "$REV#packages.$s.default.outPath")
code=$(curl -s -o /dev/null -w '%{http_code}' \
"https://<CACHE>.cachix.org/$(basename "$p" | cut -d- -f1).narinfo")
echo "$code $s $(basename "$p")"
done
200 means a consumer gets a download. 404 means they compile.
To check an entire dependency graph, which catches an unpushed dependency layer:
nix derivation show -r "$REV#packages.x86_64-linux.default" > /tmp/drvs.json
jq -r '.derivations | to_entries[] | select(.key|test("<PROJ>")) | .value.outputs.out.path' /tmp/drvs.json \
| sort -u | while read -r o; do
echo "$(curl -s -o /dev/null -w '%{http_code}' \
"https://<CACHE>.cachix.org/$(basename "$o" | cut -d- -f1).narinfo") $(basename "$o")"
done
Reading the result
A 404 for a path CI demonstrably pushed means eviction, not push failure.
Before touching the workflow, confirm push is working by grepping a CI log for
copying path '...' from 'https://<CACHE>.cachix.org' (a pull proves the
round-trip) and for Nothing to push - all store paths are already on Cachix
(proves auth). If both appear, the workflow is fine and the problem is
retention. Check the cache’s usage and GC page.
When to move off the free tier
Try pins and pushFilter first. For a small project that is usually enough.
If you still run out of space, these are the options:
| Option | Free tier | Public consumers? | Notes |
|---|---|---|---|
| Second Cachix cache for CI churn, public cache as its upstream | — | ✅ | Avoids double storage. Whether the 5 GB limit is per cache or per account is unverified, so confirm with Cachix. |
| Garnix | $0, 1,500 CI min/mo, no published cap, ~3mo retention | ✅ no auth for public repos | CI + cache in one. Darwin is opt-in. The default build set is Linux-only. |
| Cloudflare R2 | 10 GB, $0 egress | ✅ | ~$0/mo at small scale. Needs a custom domain (r2.dev rate-limits), and you must upload nix-cache-info yourself, because nix copy writes only StoreDir. You own GC. |
| Attic (self-hosted) | free software | ✅ | Still 0.1.0 with no releases, and maintenance is patchy. You run and maintain the server. |
| FlakeHub Cache | none | ⚠️ requires login | Breaks anonymous nix run. |
cache-nix-action / magic-nix-cache | GHA cache, 10 GB/repo | ❌ impossible | Needs a per-job token and has no anonymous endpoint, so it only works inside CI. GitHub Free cannot buy more than 10 GB. |
Checklist
- Enumerated what
nix build/nix run/nix profile installresolve to, per system - Closure-checked any wrapper that
cps rather than symlinks - CI builds those attributes (platform-guarded where they differ)
pushFilteron everycachix-actionblock- Token-absent guard on every push/pin step (fork PRs)
- Consumer artifacts pinned; dependency layer not pinned
- README documents
--accept-flake-configand thetrusted-userscaveat - Verified from outside with the
narinfocheck after the first green run