Skip to content

Hear ye Archers - share your Pacman hooks

I've been using Arch for over a year now and have grown rather fond of it.
One of the things I found that help me manage day to day OS stuff are Pacman hooks.

Here're the hooks I use:

  1. Pug - Saves my Pacman and Aur package lists into Gists.
  2. Orphans - Runs /usr/bin/pacman -Qtdq to list orphan packages after every update.
  3. Pacman-cleanup - Keeps only the latest cache and the currently installed package.
  4. Archaudit - Runs /usr/bin/arch-audit to list vulnerable installed packages from Arch CVE Monitoring Team data.
  5. Informant - Prevents me from running updates if there's fresh Arch News since the last update. I use this with tmux-xpanes to manage multiple Arch install's without repetitive typing.

I'd love to hear what others are using!

​

EDIT: Found another cool hook: pacman-pstatus - A tool for being able to get a list of the packages and files which own them that have been deleted or replaced after package upgrades.

Edited 7 Nov 2019. We only hold this version.

247 points69 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
247 · 100% up
Kind
Text
ID
dsnu81

Author

16
posts
357
comments
2
media
View profile

75 comments

I'm from my phone, so can't share it atm but I have a hook that creates a btrfs snapshot of the system subvolume every time linux* || systemd* || glibc update. Most of the time if something critical breaks, they are the reason, so I always have the latest snapshot on me before a breakage.

Edit: the hook

[Trigger]
Operation=Upgrade
Operation=Remove
Type=Package
Target=linux*
Target=systemd*
Target=glibc

[Action]
Description=Create a system snapshot
Exec=/usr/bin/btrfs-backup create root "Critical package update"
When=PreTransaction
Depends=python
Depends=btrfs-progs

btrfs-backup is a python script I wrote for myself and I don't think it'll be of any use to anybody except me but nonetheless you can take a look at it here. It'll require a bit of rewriting to work with different subvolumes than the ones I use though

18 points

Better do a snapshot of the whole system before each update. That’s my hook. But keep in mind to delete old snapshots, I only save the last 3 snapshots. btrfs FTW

8 points
unknown author1 hidden

I dunno about hooks, but I always have this on:

[options]
ILoveCandy
9 points

Have it too, but 99% of the time can't even notice it everything goes so fast.. Exceptions are Go, Firefox and Kernel of course.

2 points

I use btrfs so I have snap-pac to take pre/post snapshots for every pacman transaction.

I also have it so that kernel modules are symlinked after a kernel upgrade so I don't have to restart right away:

[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = linux

[Action]
Description = Symlinking old kernel modules...
When = PostTransaction
Exec = /usr/bin/bash -c "find /usr/lib/modules -xtype l -delete; ln -sv /.snapshots/$(snapper -c root list | awk 'END{print $1}')/snapshot/usr/lib/modules/$(uname -r) /usr/lib/modules/"

The following checks for packages not in a sync database (I use aurutils so I keep all packages installed in a local database). This tells me if a maintainer silently removed a package from a repository:

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

[Action]
Description = Checking for packages not in sync databases...
When = PostTransaction
Exec = /usr/bin/bash -c "for i in $(pacman -Qqm); do echo '-->' $i; done"

The following tells me if I accidentally did a partial upgrade (just do pacman -Syu and choose "no" and then install package - that's a partial upgrade!) and have packages that need to be updated:

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

[Action]
Description = Checking for packages that need to be upgraded...
When = PostTransaction
Exec = /usr/bin/bash -c "for i in $(pacman -Qqu); do echo '-->' $i; done"

Backup boot partition before upgrading kernel (isn't on btrfs so snap-pac won't back up):

[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = linux

[Action]
Description = Backing up /boot...
When = PreTransaction
Exec = /usr/bin/rsync -avzq --delete /boot /.bootbackup
9 points

I don't really have a hook, but I have a neat script that goes through all the installed packages and produces a list of the *least* used ones, giving you an idea of what to uninstall if you are running out of space (or just want to debloat your install).

https://gist.github.com/mtekman/9769fa3eb28dd0dbdd1e8ce802157e95

6 points

This sounds interesting.
Does it need configuring, I took a quick look at it but it prints? Compatible OS not detected

1 points

https://github.com/saber-nyan/kernel-modules-hook : Avoid missing modules when updating the kernel

4 points

Nice list, I just added the Informant hook, as I often get complacent and forego checking the news before updating, and when I do it is only with some of the "core" packages like linux, gcc, systemd, etc.

4 points

Sweet, might be nice to vote on the aur and star it on Github to encourage the dev.

2 points

Excellent list of hooks, op, and in the comments.

I wanted to use snapper but am not keen to change my ext4 system to btrfs just yet. So I use timeshift with the timeshift-autosnap hook that takes a snapshot before every upgrade.

4 points

Yea, I don't see myself making the move off ext4 for my daily driver/gaming desktop either.

2 points

I have the following hook which runs after pacman's mirrorlist is updated. The hook uses reflector to automatically filter the new list to https mirrors in the US, sorting them so that the fastest one is prioritized the highest:

[Trigger]
Operation = Upgrade
Type = Package
Target = pacman-mirrorlist

[Action]
Description = Updating pacman-mirrorlist with reflector and removing pacnew...
When = PostTransaction
Depends = reflector
Exec = /bin/sh -c "reflector --country 'United States' --latest 200 --protocol https --age 24 --sort rate --save /etc/pacman.d/mirrorlist; rm -f /etc/pacman.d/mirrorlist.pacnew"
4 points

This one is awesome - was about to suggest to put it on Arch Wiki, but it already is there, of course! Reflector - Pacman Hook - ArchWiki

P.S.: Shouldn't it run sudo pacman -Syyu after this? Pacman could be out of sync if the mirrors have been changed.

2 points

Are orphaned packages common? I thought they were automatically removed if a package no longer needs them

3 points

They're not, but can accumulate over time if not pruned. My hook just prints them out, to remove them via hook was asked by /u/Newton_Gimmick and seems to have derailed the conversation.

5 points

I have a hook that gives a message if the booted kernel has been updated.

/etc/pacman.d/hooks/99-z-kernel-reboot.hook

[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = File
Target = usr/lib/modules/*

[Action]
Description = Check for upgrade of running kernel
When = PostTransaction
Exec = /bin/bash -c "[[ -f \"/proc/modules\" && ! -d \"/usr/lib/modules/$(uname -r)\" ]] && printf '==> WARNING: %s\n  -> %s\n' 'Running kernel has been updated or removed.' 'A reboot is required.' || true"
3 points

Pug is really cool but I'm not sure if it's wise to publish that information. Is there a way to make it use a local git repository?

2 points

This isn't a hook but since you mentioned Pacman-cleanup, you may find downgrade useful if you ever need to downgrade a package and it's not in your cache

EDIT:

LOL just installed pacman-cleanup-hook and this happened:

==> finished: 2636 packages removed (disk space saved: 18.73 GiB)
2 points

Hehe, yea, it's what got me interested in pacman hooks as I don't have a large SSD.

I've used downgrade, tried it first when I didn't make an Nvidia driver hook to recompile on new kernels and mistakenly thought it was a driver issue.

1 points

So how do I use orphans hook? I'd like to auto delete all orphans after each update...can you do that?

1 points

Read https://www.archlinux.org/pacman/alpm-hooks.5.htmlCreate a file named orphans.hook and place it in /usr/share/libalpm/hooks/ like so (updated):

# cat /usr/share/libalpm/hooks/orphans.hook

[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = PackageTarget = *

[Action]
Depends = coreutils
When = PostTransaction
Exec = /usr/bin/bash -c "/usr/bin/pacman -Qtd || /usr/bin/echo '==> No orphaned packages found.'"
#Exec = /usr/bin/bash -c "[[ if $(/usr/bin/pacman -Qtdq) = 0 ]] && /usr/bin/pacman -Rns $(/usr/bin/pacman -Qtdq) || /usr/bin/echo '==> No orphaned packages found.'"

5 points
unknown author8 hidden

Deleted before we saved it.

2 points

You could but I feel managing packages should be done around pacman hooks instead of on a schedule.
That does the exact same thing through a bash script but also needs two systemd files.

2 points

This is not wise. An AUR maintainer may orphan a package just because he/she doesn’t want to maintain it anymore so others can adopt it, even though the package is perfectly fine, in which case it may be re-adopted soon.

-12 points

An orphan package is a package that was installed as a dependency, but no longer has the dependent package installed.

21 points

No, you're confusing maintainer abandoned aur packages with orphan packages which are not the same. Typically I would trim orphans to save SSD space.

6 points

So I added some of these hooks, but since I just upgraded my system I'm not entirely sure if they're running or not. More specifically, I want one of them (kernel-reboot.hook) to always run whenever I execute pacman, whether or not there were any upgrade or changes that time. Let's say I upgraded the kernel last time, didn't reboot, and now I've upgraded again. I want to be reminded that I should reboot every time I run pacman, basically.

However, I can't find any trigger that does this in the documentation. Do pacman hooks only ever execute if there was some change to the system? And just not at all otherwise?

1 points

That's not the job of the hook, you'll have to write something twisted like that yourself.

The hook checks for kernel upgrades first, take a look at what it's doing -

https://github.com/saber-nyan/kernel-modules-hook/blob/master/10-linux-modules-post.hook
https://github.com/saber-nyan/kernel-modules-hook/blob/master/10-linux-modules-pre.hook

What are you hoping to achieve with unnecessary reboots?

if you need reminders, there are other options, on Gnome you could use an extension like - https://extensions.gnome.org/extension/1010/archlinux-updates-indicator/

1 points

I’m not trying to reboot more than necessary, I’m trying to remind myself more often that I in fact need to reboot, because I’ve already upgraded my kernel.

But yeah, I’ve realized too that there’s just no mechanism for that.

1 points

I have an MSI with both an integrated Intel graphics card and an Nvidia GPU. Usually Nvidia is only used if I need an external monitor, so nouveau module is not always loaded.

I have a pre-installation hook that prints an alert message on the terminal if the module is loaded, and stops execution until the user acknowledges it.

Otherwise, if DKMS tries to remove the nouveau module during the upgrade process the whole system will froze, probably needing a live USB to boot and finish upgrade from arch-chroot.

1 points

That could be useful for notebook installs, post a link?

1 points

Sure, I was on the move when I wrote the previous message:

FILE: /usr/share/libalpm/hooks/65-nouveau.hook

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

[Action]
Description = Checking if nouveau driver is loaded on the system
When = PreTransaction
Exec = /usr/local/sbin/alert-nouveau-loaded
AbortOnFail

FILE: /usr/local/sbin/alert-nouveau-loaded

#!/bin/bash
#
# Alert user if nouveau driver is loaded. Used by a pacman hook.


if lsmod | grep -q nouveau; then
	echo "#############################################"
	echo "# MODULE NOUVEAU IS LOADED ON THE SYSTEM!!! #"
	echo "#############################################"
	echo
	echo "Messing with kernel modules while this one is loaded"
	echo "on the system could cause a kernel panic!"
	echo
	echo "Do you want to continue running pacman? (write: 'YES I DO')"

	# Allows us to read user input below, assigns stdin to keyboard
	exec < /dev/tty
	read -r INPUT
	# Close stdin
	exec <&-
	
	if [[ $INPUT == "YES I DO" ]]; then
		exit 0	
	fi
	exit 3
fi
1 points
unknown author

Deleted before we saved it.

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.