garbage-collect now properly displays space freed up from clearing trash can, also optimises the nix store as well

This commit is contained in:
2024-11-26 15:18:25 -05:00
parent a6a4d84ae7
commit 5bfbb541c5
2 changed files with 22 additions and 16 deletions

View File

@@ -11,7 +11,16 @@ pkgs.writeShellApplication {
nix
];
text = ''
#!/run/current-system/sw/bin/bash
convert_size() {
units=("MB" "GB" "TB" "PB")
size=$1
divisions=0
while [ "$(echo "$size >= 1024.0" | bc -l)" -eq 1 ]; do
size=$(echo "scale=2; $size / 1024" | bc -l)
divisions=$((divisions + 1))
done
echo -e "\033[1;4;38;2;166;227;161m$size ''${units[divisions]}\033[0m"
}
echo "This will delete all unused paths in the nix store and delete any files in the gtrash folder."
echo -e "\033[1;4;38;2;243;139;168mThis process is irreversible.\033[0m Are you sure?"
@@ -25,25 +34,28 @@ pkgs.writeShellApplication {
output=$(nix-collect-garbage | tee /dev/tty)
nix_freed=$(echo "$output" | grep -oP '\d+(\.\d+)? MiB freed' | cut -d' ' -f1)
echo "Done, nix-collect-garbage freed up $(convert_size "$nix_freed")"
# Get the size of the trash folder before deleting
if [ "$(ls -A ~/.local/share/Trash/files/ 2>/dev/null)" ]; then
if [ "$(find ~/.local/share/Trash/files/ 2>/dev/null)" ]; then
echo "Deleting trash files..."
echo "Found $(find ~/.local/share/Trash/files/ | wc -l) files in trash can."
rm_freed=$(du -sm ~/.local/share/Trash/files | awk '{print $1}')
/run/current-system/sw/bin/rm -rf ~/.local/share/Trash/files/*
sudo /run/current-system/sw/bin/rm -rf ~/.local/share/Trash/files
mkdir -p ~/.local/share/Trash/files
echo "Done, deleting trash freed up $(convert_size "$rm_freed")"
else
rm_freed="0"
fi
total_freed=$(echo "$nix_freed + $rm_freed" | bc)
total_converted=$(convert_size "$total_freed")
units=("MB" "GB" "TB" "PB")
divisions=0
while [ "$(echo "$total_freed >= 1024.0" | bc -l)" -eq 1 ]; do
total_freed=$(echo "scale=2; $total_freed / 1024" | bc -l)
divisions=$((divisions + 1))
done
echo "Optimizing nix store..."
nix store optimise
echo -e "System cleaning complete, freed \033[1;4;38;2;166;227;161m$total_converted\033[0m in total"
echo -e "System cleaning complete, freed \033[1;4;38;2;166;227;161m$total_freed ''${units[$divisions]}\033[0m in total"
scheck && runbg aplay "$HOME/assets/sound/sys/rm.wav"
'';