Show closure size of garbage collector roots

Is there some tool or command to list the summarized closure size on all garbage collector roots?

1 Like

With the help of [1] I could come up with something myself:

for p in /nix/var/nix/gcroots/per-user/florian/*
do
   echo -n $p" ⇒ "
   nix-store -q --requisites $p | sort -uf | xargs du -ch | tail -1
done

[1] nixos - How to get the size of a Nix derivation? - Unix & Linux Stack Exchange

1 Like

We can also use the already built in functionality of nix path-info -S to get the closure size of a store path. Additionally we can use GNU find to get the all gcroots (not just for your user). Bonus formatting of the resulting size with GNU numfmt.

find /nix/var/nix/gcroots/ -type l -readable \
  | xargs nix path-info -S \
  | awk '{ sum += $2; }; END { print sum }' \
  | numfmt --to=iec --suffix=B --format="%.2f"
2 Likes

This summation to a single total is counting a lot of stuff multiple times, as there will be quite a bit shared between system derivations, and probably is also hiding the information originally wanted (which gc roots should I delete to try and clean up).

Also minor formatting nit, the final closing ` should not be there (it is presumably a stray markdown formatting quote)

1 Like

Right, we’d need to plug nix-store --requisites and sort -u in between and then use just nix path-info -s to ignore the closure size.

find /nix/var/nix/gcroots/ -type l -readable \
  | xargs nix-store -q --requisites \
  | sort -u \
  | xargs nix path-info -s \
  | awk '{ sum += $2; }; END { print sum }' \
  | numfmt --to=iec --suffix=B --format="%.2f"
1 Like

Thanks @sternenseemann
Inspired from your command I came up with this script, which shows all gc roots and their size (which are most likely overlapping.

for link in $(find /nix/var/nix/gcroots/ -type l -readable)
do
  res="${res}
$(readlink "${link}") $(nix path-info -Sh "${link}")"
done
echo "$res" | sort -h -k3,3

``
1 Like

nix-du outputs a graph that shows the size attached to each gc root and the amount of space shared between them.