Files
w3cjandClaude Opus 5.5 6f680a6f8b feat(proxy): a shared, read-only Nix cache that only the host fills
VMs with network policy and the nix layer share their downloads from
cache.nixos.org: the proxy answers http://devvm.proxy/nix/ itself (a
reserved endpoint, never forwarded), and the guest setup puts it before
https://cache.nixos.org in Nix's substituters, trusted-public-keys left
as Nix's own (cache.nixos.org-1). Each VM keeps its own store and daemon;
only downloads are shared, and no VM can change what another gets.

The proxy (internal/proxy/nixcache.go, -nix-cache DIR and
-nix-cache-max BYTES):
- takes only GET and HEAD, no query, nothing escaped, of
  nix-cache-info, <32 nix32>.narinfo and nar/<52 nix32>.nar with no
  suffix or .xz, .zst or .bz2 (nixPath, FuzzNixPath); 400 otherwise;
- makes up nix-cache-info (priority 30, before cache.nixos.org's 40);
- fetches from https://cache.nixos.org alone, when the VM's policy
  allows cache.nixos.org:443, through the floor or the company proxy,
  with the system's roots and the user's CAs, following no redirect,
  sending nothing of the VM's request;
- keeps a narinfo when its StorePath is the path asked for, its URL is
  of the pattern and named by its own NarHash or FileHash, and a Sig by
  the pinned cache.nixos.org-1 Ed25519 key verifies over
  1;StorePath;NarHash;NarSize;References (parseNarinfo, against a real
  narinfo and NAR of cache.nixos.org's in testdata/nix/);
- fetches a NAR only after a narinfo it checked names it, never for a
  HEAD, once however many ask and at most four at once (fetchOnce), and
  keeps it when its SHA-256 and size are the narinfo's FileHash and
  FileSize; a cached copy of another size is dropped;
- answers 404 to anything that fails, keeping nothing, so Nix goes to
  cache.nixos.org itself; logs each request as a nix-cache event
  (hit, fetched, or why not), which devvm log shows as cache;
- writes files whole and renames them in, 600 in folders of 700.

The cap: the nix-cache.size retention setting, 10 GiB when off
(state.NixCacheDefault, proxy.NixCacheDefault), with state.ParseSize
(500M, 20G, 1T). Under a flock on .lock every proxy takes a file's room
before writing it (reserve, in whole blocks), deleting the least
recently used files (by mtime, which a hit sets) down to nine tenths of
the cap; a NAR being fetched is made its full size at once and counts,
so a fetch the in-flight files leave no room for is refused. devvm gc
trims to the cap, devvm gc --nix-cache empties it, and the daily
retention run trims it (proxy.TrimNixCache, NixCacheUsage). devvm space
has a Nix cache row (GC: what's over the cap, FREED BY gc --nix-cache,
RETAIN nix-cache.size), devvm images says what it takes of its cap.

netpolicy passes -nix-cache and -nix-cache-max only for images with the
nix layer (nixCacheArgs; a retention file it can't read warns and uses
the default). The guest setup writes /etc/nix/devvm-cache.conf through
!include in nix.conf only with the nix layer and policy on, removes it
otherwise, and restarts nix-daemon when it changes. VMs without network
policy get no shared cache: they fetch from cache.nixos.org directly,
and nothing new runs or listens on the host for them.

Review (security-audit skill, on nixcache.go, its callers and the
setup): a NAR could be kept under another NAR's name, as the URL isn't
signed (fixed: the URL must be named by the narinfo's own hashes, and a
hit of another size is dropped); files being fetched didn't count
against the cap and blocks weren't counted, so one VM's parallel
requests could pass it (fixed: reserve, full-size temp files, block
counts, fetchOnce); a HEAD downloaded a NAR (fixed). Ruled out: stored
VM input, paths out of the folder, another upstream, parser
differences, cross-VM visibility beyond timing, a VM's own nix.conf.
Open, documented: timing tells a VM whether some VM fetched a public
path, and a VM can fill the cache to its cap.

Messages: gc_nix_cache_over, gc_nix_cache, deleted_nix_cache,
images_nix_cache; retention_deleted gains nix_files. Paths:
Dirs.NixCache (structure limit for Dirs to 40). Flags: devvm gc
--nix-cache; the CLI reference regenerated.

Tests: TestNixPath, TestNix32, TestParseNarinfo, TestNixCacheServes,
TestNixCacheRefuses, TestNixCachePolicy, TestNixCacheFetches,
TestTrimNixCache, TestNixCacheCap, FuzzNixPath, TestParseSize,
TestRetention, TestNixCacheArgs, TestFormatEvent; nix_cache.txtar (the
proxy's endpoint and refusals, space, images, gc, gc --nix-cache,
retention); e2e nix: the VM's substituters and keys, hello through the
cache, fetched by the host (600/700), a VM's writes and other requests
refused and nothing kept, a second VM's hits fetching nothing, no cache
after devvm network off.

Docs: the design note (internals/design.md), internals/network.md's The
shared Nix cache, the VM's setup, logs and flags, internals/security.md,
internals/images.md, safety/network.md's Nix and Logs,
using/toolchains.md's Nix projects, using/images.md, using/space.md,
using/settings.md's Retention, internal/proxy/README.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 22:56:53 -07:00
..