- Shell 95.5%
- Dockerfile 4.5%
|
|
||
|---|---|---|
| .forgejo/workflows | ||
| build-container | ||
| deployment | ||
| serve-container | ||
| README.md | ||
Nix binary cache
Containers that build a flake with Nix, push the resulting closures to an S3-compatible binary cache (Garage, MinIO, AWS S3), and serve that bucket over HTTP.
Build
cd build-container && podman build -f Containerfile -t nixos-cache-builder:latest .
Run
The entrypoint is build-flake. It takes a flake reference plus optional
attributes; everything else is environment variables. See build-flake --help.
With no attributes it builds all packages, devShells, checks, formatter,
NixOS configurations, Home Manager configurations and nix-darwin configurations.
Several flakes (or branches) can be built in one run. Any argument containing
: or starting with .// starts a new flake ref; the arguments after it are
its attributes. Use flake:nixpkgs rather than a bare registry name after the
first ref.
build-flake git+https://host/repo.git \
'git+https://host/repo.git?ref=release' nixosConfigurations.x.config.system.build.toplevel \
github:owner/other
FLAKE_REF may also hold several space-separated refs; FLAKE_ATTRS then
applies to each.
Besides the build outputs, every run pushes the sources to the same cache (set
NIX_CACHE_SOURCES=false to skip): each flake's source tree and locked inputs
(nix flake archive), and every fixed-output derivation of the build (fetched
src tarballs, git checkouts). They are ordinary signed store paths, so restore
one with nix copy --from <cache-url> /nix/store/...-source or
nix flake archive --from <cache-url> <flake-ref>. Expect the bucket to grow
and runs to take longer: every source is fetched on each run.
podman run --rm \
-e FLAKE_REF=github:NixOS/nixpkgs/nixos-unstable \
-e FLAKE_ATTRS=hello \
-e NIX_CACHE_BUCKET=nixos-cache-tst \
-e AWS_DEFAULT_REGION=garage \
-e S3_ENDPOINT=garage-s3.tnt-nixos--garage.svc.cluster.local:3900 \
-e S3_SCHEME=http \
-e AWS_ACCESS_KEY_ID=... \
-e AWS_SECRET_ACCESS_KEY=... \
nixos-cache-builder:latest
Kubernetes with Garage
deployment/overlays/tst contains the one-shot builder Job and the cache
server in namespace tnt-nixos--cache-tst. Both reach Garage through the
garage-s3 ClusterIP service in namespace tnt-nixos--garage, so no Ingress
is needed for the builder.
AWS_DEFAULT_REGIONmust equal Garage'ss3_region(garage); Garage rejects requests signed for another region.- Credentials are read from the mounted file at
/run/secrets/nix-cache/credentialsusing thegarageprofile. - Nix uses path-style requests whenever
endpointis set, which Garage supports.
Create the bucket and a key that can read, write and own it:
garage bucket create nixos-cache-tst
garage key create nix-cache
garage bucket allow --read --write --owner nixos-cache-tst --key nix-cache
Copy the credentials template, fill in the Garage key, and create the secret:
cp deployment/overlays/tst/garage-credentials.example deployment/overlays/tst/garage-credentials
# edit deployment/overlays/tst/garage-credentials
nix-store --generate-binary-cache-key nix-cache-1 /tmp/nix-cache-secret /tmp/nix-cache-public
kubectl -n tnt-nixos--cache-tst create secret generic nix-cache \
--from-file=credentials=deployment/overlays/tst/garage-credentials \
--from-file=signing-key=/tmp/nix-cache-secret \
--from-file=public-key=/tmp/nix-cache-public
kubectl apply -k deployment/overlays/tst
Serve the cache over HTTP
serve-container runs rclone's HTTP server in read-only mode over the bucket.
Build it:
cd serve-container && podman build -f Containerfile -t nixos-cache-server:latest .
The tst overlay deploys it as the nix-cache Deployment and Service, available
inside the cluster at
http://nix-cache.tnt-nixos--cache-tst.svc.cluster.local:8080. It mounts only
the credentials key from the nix-cache secret; for production, create a
read-only Garage key instead of reusing the builder key.
The server exposes the bucket exactly as Nix wrote it (nix-cache-info, narinfo
and nar files), so clients use it like any HTTP binary cache:
nix.settings.substituters = [ "http://nix-cache.tnt-nixos--cache-tst.svc.cluster.local:8080" ];
nix.settings.trusted-public-keys = [ "nix-cache-1:..." ];
The Service is ClusterIP only and unauthenticated. Add an Ingress or HTTPRoute
with TLS to publish it like cache.nixos.org. Alternatively set
require-sigs = false on clients if you deliberately run an unsigned test cache.