Skip to content

Instantly share code, notes, and snippets.

@oxalica
Created February 15, 2023 14:40
Show Gist options
  • Save oxalica/c7fd0ce85e305fef7dfca86902e5e25f to your computer and use it in GitHub Desktop.
Save oxalica/c7fd0ce85e305fef7dfca86902e5e25f to your computer and use it in GitHub Desktop.
Query all reachable nix store paths
#!/usr/bin/env bash
roots_file="$(mktemp)"
find /nix/var/nix/gcroots -type l | xargs realpath 2>/dev/null >"$roots_file"
sqlite3 --readonly --batch db.sqlite <<EOF
CREATE TEMPORARY TABLE roots (path TEXT);
.mode csv
.import $roots_file roots
WITH RECURSIVE closure(id) AS MATERIALIZED (
SELECT id
FROM roots
JOIN ValidPaths USING (path)
UNION
SELECT ValidPaths.id
FROM Refs
JOIN ValidPaths ON (reference = ValidPaths.id)
WHERE referrer = id
)
SELECT path
FROM closure
JOIN ValidPaths USING (id);
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment