Skip to content

Instantly share code, notes, and snippets.

@r-k-b
Last active October 21, 2025 09:20
Show Gist options
  • Save r-k-b/bc5e6eb91206a043affde2456bec072b to your computer and use it in GitHub Desktop.
Save r-k-b/bc5e6eb91206a043affde2456bec072b to your computer and use it in GitHub Desktop.
a 1-line DIY "LRU cache" GC for nix flat-file build caches
# assuming that the flat-file build cache is targeted like: `nix copy --all --to file:///srv/review-apps/buildcache`
# 1. Delete nar/*.nar.xz with old atime:
ls -la /srv/review-apps/buildcache/nar
| where accessed < ((date now) - 1wk)
| sort-by accessed
| each { |narxz|
print $"($narxz.accessed) .. ($narxz.name)";
try {
print $"rm ($narxz.name)"
rm $narxz.name
} catch { |e|
print $">>> delete xz failed: ($e | to nuon)"
}
}
# 2. Cleanup affected narinfos:
ls -la /srv/review-apps/buildcache/*.narinfo
| par-each -t 4 { |narinfo|
mut xz = {}
try {
$xz = open $narinfo.name | parse "{key}: {value}" | each {|kv| {$kv.key: $kv.value}} | into record;
} catch { |e|
print $"not a narinfo? ($narinfo.name) ($e | to nuon)"
return
}
if (echo $"/srv/review-apps/buildcache/($xz.URL)" | path exists) {
print $"OK: ($narinfo.name)"
} else {
print $"missing URL for: ($narinfo.name) [($xz.URL)]";
rm $narinfo.name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment