![]() |
|
Pacman hooks crash course - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Computers (https://sinister.li/Forum-Computers) +--- Forum: Software & Programs (https://sinister.li/Forum-Software-Programs) +--- Thread: Pacman hooks crash course (/Thread-Pacman-hooks-crash-course) |
Pacman hooks crash course - Inori - 11-27-2017 Pacman, the package manager of choice for Arch Linux, provides functionality for hooks. For those unaware, a hook acts as a kind of system-wide (or larger, in the case of web hooks) callback that allowed for inter-program activity. Pacman hooks are extremely useful because they allow for intervention at any stage in the package management process. To start using hooks, we need to create a file for one in the following directory, which is the one pacman checks for hooks. Code: /etc/pacman.d/hooksIt's good practice to name the file after the target package, and the demonstration will be for the Linux kernel (and GRUB, but mainly Linux), so it will be named "linux.hook". The first thing we have to do is specify a trigger. When doing so, there are 3 fields we are required to specify: operation, type, and target. "Operation" is the thing we're doing to the package and it can be one of "Install", "Upgrade", or "Remove". The latter two are self-explanatory, but "Installation" is considered an upgrade if the package already exists on the system. This field can be specified multiple times if needed. "Type" is the type of target, either "File" or "Package". The denotation of "Package" is obvious, and "File" specifies a file owned by a specific package. Finally, "Target" specifies what files/packages our hook is targeting. Glob patterns are allowed for files. This can also be specified multiple times, as we'll see below. Using the above information, we can make the following trigger section (by the way, case is important): Code: [Trigger]
Operation=Install
Type=Package
Target=linux
Target=grubNext, we need an action. Actions can be specified multiple times and have two required fields - "When" and "Exec" - and several optional ones: "Description", "Depends", "AbortOnFail", and "NeedsTargets". The "When" field specifies when the action will be executed. It can be "PreTransaction" (before upgrade/installation/removal) or "PostTransaction" (after the fact). "Exec" is simply the shell command to execute. Now for the optional fields. "Description" is self-explanatory and recommended for legacy and documentation. "Depends" can be specified multiple times and specifies a package that needs to be installed for the hook to run (this will be taken into account for pacman's dependency resolution). "AbortOnFail" is only available for pre-transaction actions, and will cause the entire pacman operation to abort if the hook fails. It's boolean, so it takes no arguments/assignment. "NeedsTargets" is good for scripts, as the matched names of target files/packages will be piped to the "Exec" command through stdin. It's also boolean, so again no arguments. With that out of the way, our action is as follows (again, watch the case): Code: [Action]
Description=Update GRUB entries after upgrading kernel or GRUB package
When=PostTransaction
Exec=grub-mkconfig -o /boot/grub/grub.cfg
Depends=grubAll together, our hook file - "/etc/pacman.d/hooks/linux.hook" - looks like this: Code: [Trigger]
Operation=Install
Type=Package
Target=linux
Target=grub
[Action]
Description=Update GRUB entries after upgrading kernel or GRUB package
When=PostTransaction
Exec=grub-mkconfig -o /boot/grub/grub.cfg
Depends=grub |