I need to find out how much garbage can be cleared when ‘nix-collect-garbage -d’ is run for a personal project but I haven’t been able to find any answer about this on documentations and DDG.
I’ve tried ‘du -sh /nix/store’ but it would print the size everything in /nix/store however ‘nix-collect-garbage -d’ does not remove everything when it’s executed so that’s not gonna work. I’ve also tried executing “nix-store --gc --print-dead | grep ‘^/nix*’” (‘–print-dead’ produces output that which contains store paths that are not live and will not be used, with respect to roots) and then executing “stat -c ‘%s’” on each path (‘stat -c “%s” $file_name’ produces output containing the bytes of the concerned file in bytes) however that doesn’t seem to work as well for some unknown reason as they all seem to add up to a few kilobytes (despite the actual amount that ‘nix-collect-garbage -d’ deletes being about 400MB).
I know that the code below is extremely shit but that’s because that’s not my main project but a separate script solely for trying to find out what I am doing wrong and how I can properly implement this in my main project.
Thanks for any help!
#!/usr/bin/env bash
`tmp() {
declare -a str byt
str=( $(nix-store --gc --print-dead 2>/dev/null | grep ‘/nix*’) )
for garbage in “${str[@]}”; do
byt+=( $( stat -c ‘%s’ $garbage ) )
done
total=0
for filesize in “${byt[@]}”; do
total=$(( $total + $filesize ))
done
convertedtotal=“$(( ( $total / 1024 ) /1024 ))MB”
echo “$total”
echo “$convertedtotal”
}
tmp