Files
devvm/testdata/script/nix_cache.txtar
T
w3cjandClaude Opus 5.5 299ddb1476 refactor(state): image records in images/<key>/, rebuild counters per backend and --from, tool image records, cache/
- Image records are images/<key>/record.json (JSON, unknown fields
  ignored; name, chain, from, digest, libc, built). state.Images lists
  the folders; AnyImage needs a record.json; RemoveImage deletes the
  folder. Lima's cache cleaner reads records through state.Images.
- Rebuild counters are images/rebuilds.json, keyed by (backend, from as
  given, name), changed under images/locks/rebuilds. image.Inputs takes
  them through an image.Rebuilds interface. A rebuild on one backend no
  longer outdates the others' images, and two --from references that
  wrote the same counter name (node:24, node-24) stay apart.
  paths.RebuildFile, RebuildName and unsafeName are gone.
- Tool images: <file>.sha256, .target and .idle become one <file>.json
  (sha256, libc, format, backend, built, idle_since; state.ToolImage).
  BACKEND and LIBC read the record; the fallbacks (format matching,
  a VM's target, the current target) go, with toolTargets, vmToolTargets,
  formatBackend and gc's backendTools (toolsOn does it).
  Share.IdleFor owns reading idle_since.
- cache/ (Dirs.Cache) holds mise/, nix-cache/ and from-digests.json.
  With the cache gone, from.Recorded takes the newest base record's
  digest for the reference, so no key changes.
- Key check: the rebuild counter is the one input that moved and changes
  keys (deliberately: per backend and exact from). The --from digest
  moved to cache/ and stays an input, kept stable by the records'
  fallback. Records, mise and the Nix cache are outputs.
- Tests: state's image, rebuild, digest and tool record tests; image key
  cases per backend and exact from; build.TestRebuildIsPerBackend;
  from.TestRecordedWithoutTheCache; cli.TestToolsOn; rebuild.txtar
  (Docker's and smolvm's rebuilds keep Lima's and the other --from's
  bases), gc_caches.txtar and from.txtar (a missing cache/); the
  testscript command json_set; e2e reads the JSON records.
- Docs: space, images, settings (retention), internals images,
  overview, network, security, contributing (json_set).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 07:37:36 -07:00

98 lines
4.2 KiB
Plaintext

# The shared Nix cache: devvm _proxy -nix-cache 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
exec devvm _proxy --help
stdout '-nix-cache '
stdout '-nix-cache-max'
# 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 devvm _proxy -listen 127.0.0.1:$PORT -auth-file key -policy nix.policy -log events.jsonl -nix-cache cache -nix-cache-max 1000 &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, and nothing of a backend's own run (--backend).
exec devvm gc --force --backend lima --nix-cache
! stdout nix_cache
exists $CACHE/nar/bbbb.nar.xz
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
-- key --
k
-- 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