Files
devvm/testdata/script/nix_cache.txtar
T
w3cjandClaude Opus 5.5 8e914b17c9 refactor(proxy): one typed, versioned config for every proxy, read as a private file
devvm _proxy runs with one argument, -config FILE: proxy.Config as JSON
(version 1), which a VM's proxy (vms/<hash>/run/proxy.json, written by
netpolicy.Proxy) and a build's (build-proxy/proxy.json, written by
backend.ProxyStarter) both use. It holds what the flags and the run
folder's sidecars held: the address, the VM, the password (proxy.auth),
the built-in policies and the user's files (proxy.policy), the logs, the
lifeline, the CA, the secrets, the user's CAs (proxy.cas), the company
proxy (proxy.upstream, now proxy.Upstream), upstream_unresolved, the Nix
cache and its cap, the links, and the SHA-256 of devvm's binary and CA
(proxy.bin, proxy.ca), which the proxy doesn't read. devvm writes it whole,
mode 600, before each start, and starts a running VM's proxy again when
the config it would start it with has another Sum. ReadConfig reads it as
the links' list is read (readPrivate): O_NOFOLLOW and O_NONBLOCK, fstat,
a regular file owned by the proxy's user that nobody else may read or
write, at most 1 MiB, of ConfigVersion, no unknown field, a password, a
loopback listen address. The VM's proxy is found by _proxy -config with
its own config's path. paths.VM names ProxyConfig, ProxyPID and
ProxyStatus.

Policy files are read only as regular files of at most 1 MiB
(proxy.ReadRegular, PolicyMax), in the proxy, devvm network and doctor, so
a FIFO or a link to /dev/zero can't hang them (ErrNotRegular, ErrTooLong;
ErrNotPrivate for the config and the links' list); the proxy keeps a
file's last rules and logs why once.

hostOK refuses a name with an empty label (evil.test.., evil..test,
.evil.test), so no form of a name gets past a deny the policy's own trim
would miss.

A build proxy that can't be spawned leaves no config behind.

Tests: config round trip and Sum; ReadConfig refusing a link, a FIFO, a
folder, group-readable and group-writable files, a long file, other
versions, unknown fields, two configs, no password, non-loopback
addresses, a CA without its key, a negative cap; ReadRegular and the
policy readers on a FIFO, /dev/zero and a big file without hanging; the
running proxy keeping its last rules; hostOK and requests with empty
labels (failing on the code before); Prepare's config, restarts on a
binary change, a missing config and one others may read; concurrent
config writes; the build proxy's config and its removal; proxy.txtar,
network.txtar, nix_cache.txtar and layout.txtar; e2e checks of the
config's mode, password and the proxy's arguments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 17:51:45 -07:00

90 lines
4.1 KiB
Plaintext

# The shared Nix cache: devvm _proxy with nix_cache in its config answers http://devvm.proxy/nix/
# itself; devvm space, devvm images and devvm gc show what it takes, and devvm
# gc deletes its least recently used files while it's over its cap
# (nix-cache.size), or all of it with --nix-cache.
env CACHE=$XDG_DATA_HOME/devvm/cache/nix-cache
# The proxy makes the folder, the user's alone, and answers nix-cache-info
# itself. What the cache doesn't take is refused, a NAR no checked narinfo
# names isn't fetched, and nothing is answered without the VM's password.
free_port PORT
exec sh -c 'printf ''{"version": 1, "listen": "127.0.0.1:%s", "password": "k", "policies": ["nix.policy"], "log": "events.jsonl", "nix_cache": "cache", "nix_cache_max": 1000}\n'' "$PORT" > nix.json && chmod 600 nix.json'
! exec devvm _proxy -config nix.json &p&
exec sh $WORK/waitfor 'nc -z 127.0.0.1 $PORT'
exec curl -sS -x http://devvm:[email protected]:$PORT http://devvm.proxy/nix/nix-cache-info
stdout '^Priority: 30$'
stdout '^StoreDir: /nix/store$'
exec curl -sS -o /dev/null -w '%{http_code}' -x http://devvm:[email protected]:$PORT -X POST -d x http://devvm.proxy/nix/nix-cache-info
stdout '^400$'
exec curl -sS -o /dev/null -w '%{http_code}' -x http://devvm:[email protected]:$PORT --path-as-is http://devvm.proxy/nix/nar/../aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo
stdout '^400$'
exec curl -sS -o /dev/null -w '%{http_code}' -x http://devvm:[email protected]:$PORT http://devvm.proxy/nix/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo?x
stdout '^400$'
exec curl -sS -o /dev/null -w '%{http_code}' -x http://devvm:[email protected]:$PORT http://devvm.proxy/nix/nar/0000000000000000000000000000000000000000000000000000.nar.xz
stdout '^404$'
exec curl -sS -o /dev/null -w '%{http_code}' -x http://127.0.0.1:$PORT http://devvm.proxy/nix/nix-cache-info
stdout '^407$'
exec ls -ld cache
stdout '^drwx------'
exec ls -ld cache/nar
stdout '^drwx------'
kill p
wait p
grep '"event":"nix-cache".*"reason":"not found: no checked narinfo names it"' events.jsonl
grep '"event":"nix-cache".*"reason":"refused: ' events.jsonl
exec sh -c 'ls cache cache/nar'
! stdout narinfo
# nix-cache.size takes sizes as devvm space shows them.
! exec devvm retention nix-cache.size=2k
stderr '^devvm: retention_bad '
exec devvm retention nix-cache.size=128K
stdout '^==> retention_saved '
# 192K in the cache, over its 128K cap: devvm space says so, and the daily
# retention run deletes the least recently used file. Sizes are whole
# blocks, which the cap counts.
mkdir $CACHE/nar
exec sh -c 'head -c 65536 /dev/zero > "$CACHE/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo" && head -c 131072 /dev/zero > "$CACHE/nar/bbbb.nar.xz" && touch -t 202001010000 "$CACHE/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo"'
exec devvm space --json
stdout '"what": "Nix cache",\n\s+"count": 2,\n\s+"size": 196608,\n\s+"sized": true,\n\s+"kept": "remade",\n\s+"gc": 65536,\n\s+"rest": "gc --nix-cache",\n\s+"retain": "128K",\n\s+"retain_frees": 65536'
exec devvm space
stdout '^Nix cache .* gc --nix-cache +128K \(64K\)$'
exec devvm ls
stderr '^retention_deleted exports=0 nix_files=1 orphans=0 tools=0$'
! exists $CACHE/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo
exists $CACHE/nar/bbbb.nar.xz
# Over its cap again: devvm images says what it takes, and devvm gc deletes
# the least recently used file.
exec sh -c 'head -c 65536 /dev/zero > "$CACHE/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo" && touch -t 202001010000 "$CACHE/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo"'
exec devvm images
stderr '^images_nix_cache cap=128K size=192K$'
exec devvm gc --force
stdout '^gc_nix_cache_over cap=128K size=192K$'
stdout '^==> deleted_nix_cache count=1 size=64K$'
! exists $CACHE/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.narinfo
exists $CACHE/nar/bbbb.nar.xz
exec devvm gc --force
stdout '^==> nothing_to_clean$'
# --nix-cache: all of it.
exec devvm gc --force --nix-cache
stdout '^gc_nix_cache count=1 size=128K$'
stdout '^==> deleted_nix_cache count=1 size=128K$'
! exists $CACHE/nar/bbbb.nar.xz
exists $CACHE
exec devvm images
! stderr images_nix_cache
-- nix.policy --
cache.nixos.org
-- waitfor --
# waitfor CMD: CMD every 0.1 s until it succeeds, for 30 s at most.
i=0
until sh -c "$1"; do
i=$((i + 1))
[ "$i" -lt 300 ] || exit 1
sleep 0.1
done