Pacman hooks crash course 11-27-2017, 04:28 PM
#1
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.
It'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):
So our hook will be triggered when the linux and/or grub packages are either freshly installed or upgraded.
Next, 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):
This will update the system's grub configuration after an operation in which GRUB or the Linux kernel is upgraded. Since we need GRUB for this, we specify that with "Depends".
All together, our hook file - "/etc/pacman.d/hooks/linux.hook" - looks like this:
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
(This post was last modified: 11-27-2017, 04:29 PM by Inori.)
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.

















![[+]](https://sinister.li/images/modern/collapse_collapsed.png)