Skip to content

Instantly share code, notes, and snippets.

@luukvbaal
Last active February 20, 2024 01:52
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save luukvbaal/2c697b5e068471ee989bff8a56507142 to your computer and use it in GitHub Desktop.
Save luukvbaal/2c697b5e068471ee989bff8a56507142 to your computer and use it in GitHub Desktop.
yay paccache hooks
[Trigger]
Operation = Remove
Type = Package
Target = *
[Action]
Description = Clearing cache...
When = PostTransaction
Exec = /home/<user>/.local/bin/tools/removehook
[Trigger]
Operation = Upgrade
Type = Package
Target = *
[Action]
Description = Clearing cache...
When = PostTransaction
Exec = /home/<user>/.local/bin/tools/upgradehook
#!/bin/sh
yaycache="$(find "$HOME"/.cache/yay -maxdepth 1 -type d | awk '{ print "-c " $1 }' | tail -n +2)"
yayremoved=$(/usr/bin/paccache -ruvk0 $yaycache | sed '/\.cache\/yay/!d' | cut -d \' -f2 | rev | cut -d / -f2- | rev)
[ -z $yayremoved ] || echo "==> Remove all uninstalled package folders" &&
echo $yayremoved | xargs -rt rm -r
#!/bin/sh
yaycache="$(find "$HOME"/.cache/yay -maxdepth 1 -type d | awk '{ print "-c " $1 }' | tail -n +2)"
echo "==> Keep last 2 installed versions"
/usr/bin/paccache -rvk2 -c /var/cache/pacman/pkg $yaycache
@Morganamilo
Copy link

yaycache="$(yay -Pg | jq .buildDir)" ;)

@luukvbaal
Copy link
Author

luukvbaal commented May 28, 2019

Unfortunately paccache doesn't work recursively as far as I know hence why I pass all sub-folders in yay buildDir.
But I guess the hard-coded directory could be replaced by $(yay -Pg | jq -r .buildDir) if you want, e.g. yaycache="$(find $(yay -Pg | jq -r .buildDir) -maxdepth 1 -type d | awk '{ print "-c " $1 }' | tail -n +2)"

@Morganamilo
Copy link

Honestly the better option is to just set PKGDIR and have paccache clean that.

@luukvbaal
Copy link
Author

Yes I guess it is, thanks.

@GavinTao1219
Copy link

These hooks are great! Thank you!

@Mayurifag
Copy link

Is there any fast way to add this into system? Maybe, AUR package or smth like that?

@luukvbaal
Copy link
Author

@Mayurifag in theory yeah but that would require a separate package for each aur helper or some sort of config file to indicate the cache directory. Doesn't get you much further than just manually installing the scripts so I don't feel for it much.

Copy link

ghost commented Dec 2, 2021

Wouldn't it be more efficient and extensible to use the $HOME variable rather than /home/<user>/ each time?

@luukvbaal
Copy link
Author

For the scripts, sure. The hook files however are run as root so that's not going to work.

@Anuskuss
Copy link

Anuskuss commented Aug 8, 2023

This is what I've been using:

[Trigger]
Operation = Upgrade
Operation = Remove
Type = Package
Target = *

[Action]
Description = Clearing cache...
When = PostTransaction
Exec = /usr/bin/bash -c 'yay=$(find /home/$SUDO_USER/.cache/yay -mindepth 1 -maxdepth 1 -type d -exec echo -c {} \;); paccache -rk2 -c /var/cache/pacman/pkg $yay; paccache -ruk0 $yay; find /home/$SUDO_USER/.cache/yay -mindepth 1 -maxdepth 1 -type d -exec bash -c "compgen -G {}/*.pkg.tar.zst > /dev/null || rm -r {}" \;'

Edit: Actually now that I'm thinking about it, parsing paccache -ru isn't reliable because it will remove e.g. /home/user/.cache/yay/mutter-performance/mutter-performance-docs-1:44.3-2-x86_64.pkg.tar.zst (because mutter-performance-docs is not installed) thus removing /home/user/.cache/yay/mutter-performance even though mutter-performance is installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment