Pacman Hooks: Declarative Automation for Arch Linux Updates
Pacman hooks automate repetitive post‑install tasks like initramfs regeneration on Arch Linux. This blog shows a concrete hook for kernel updates, explains the transaction model, and highlights failure modes you should test before relying on them.
25 Jan 2026, 11:48 UTC

The problem: manual steps after every kernel update
Arch Linux’s rolling release model means frequent kernel upgrades. Each upgrade typically requires regenerating the initramfs and updating bootloader entries—steps that are easy to forget and tedious to run by hand. Pacman hooks solve this by letting you declare actions that run automatically during package transactions, keeping system state consistent without external cron jobs or wrapper scripts.
How hooks fit into the transaction
Hooks are declarative event handlers stored in two locations:
/usr/share/libalpm/hooks– shipped with packages (read‑only for users)/etc/pacman.d/hooks– user‑defined hooks (safe from package overwrites)
Each hook file uses a simple INI‑like format with three required sections:
- Trigger – defines
When(e.g.,Install,Upgrade,Remove,PostTransaction) andTargetpackage globs. - Action – specifies
Description,Exec(command run as root), and optionalNeedsTargets. - Optional –
AbortOnFailto halt the transaction on error.
Because hooks execute inside the pacman transaction, their output appears in the pacman log, making the entire update reproducible and auditable.
Worked example: auto‑regenerate initramfs on kernel changes
Create a custom hook that runs mkinitcpio -P whenever the linux or linux-lts packages are installed or upgraded.
- Ensure the hook directory exists:
sudo mkdir -p /etc/pacman.d/hooks - Write the hook file
/etc/pacman.d/hooks/90-mkinitcpio.hook:[Trigger] Type = Package Operation = Install Operation = Upgrade Target = linux Target = linux-lts [Action] Description = Regenerating initramfs for all kernels When = PostTransaction Exec = /usr/bin/mkinitcpio -P AbortOnFail - Verify syntax and permissions (hook files must be readable by root):
The dry‑run shows the hook would fire without making changes.sudo pacman -Sy --dry-run linux 2>&1 | grep -A2 -B2 '90-mkinitcpio' - Run a real update to confirm:
Checksudo pacman -S linux/var/log/pacman.logfor lines like:[ALPM] running '90-mkinitcpio.hook'... [ALPM] Regenerating initramfs for all kernels [ALPM] completed '90-mkinitcpio.hook'
Trade‑offs and limitations
- Failure halts the transaction if
AbortOnFailis set. A brokenmkinitcpiorun can leave the system without a valid initramfs, so test hooks on a non‑critical package first. - Minimal environment – hooks run with a restricted
PATH(typically/usr/bin:/bin), no interactive terminal, and working directory set to/. Always use absolute paths inExec. - Version sensitivity – the hook schema (supported
Whenvalues,Targetglobbing) depends on the installedpacman/libalpmversion. Older systems may lackPostTransactionorNeedsTargets. - Ordering is not guaranteed across multiple hooks for the same trigger. If you need strict sequencing, combine steps into a single hook or a wrapper script.
Actionable next steps
- List existing hooks to understand what’s already automated:
find /usr/share/libalpm/hooks /etc/pacman.d/hooks -name '*.hook' -exec echo '=== {} ===' \; -exec cat {} \; - Create a benign test hook to verify the mechanism on your system:
Then runsudo tee /etc/pacman.d/hooks/99-test.hook >/dev/null <<'EOF' [Trigger] Type = Package Operation = Install Operation = Upgrade Target = * [Action] Description = Test hook fired When = PostTransaction Exec = /usr/bin/logger 'pacman hook test triggered' EOFsudo pacman -S --needed base-develand checkjournalctl -t pacmanfor the log entry. - Adapt the example hook for other repetitive tasks: updating
fwupdmetadata, rebuildingmandb, or syncing a local package repository.
Pacman hooks give you a transparent, version‑controlled way to codify the "after‑install" steps that keep an Arch system bootable and consistent. Start with the initramfs example, verify it in your logs, and then extend the pattern to any deterministic post‑transaction work.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.