Creating useful Pacman hooks

You could script that in manually but I think it would be a lot easier and more effective to use something like logrotate


Oh nice that's already included in Manjaro. Thanks!

1 Like

So something as simple as this, then?

/home/yochanan/package_lists {
	rotate 1

exists with pamac a hook for clear packages cache ...

"File" deprecated , Path is for next version


Problem -> Description


Manjarowish : Kde-Gaming edition

Operation = Install
Operation = Upgrade
Type = File
Target = /boot/grub/grub.cfg

Description = Edit configfile path for grub-btrfs.cfg in grub.cfg
When = PostTransaction
Exec = /usr/bin/sed -i s's/\${prefix}/\/\@\/boot\/grub/' /boot/grub/grub.cfg

Can it be done that way? :slight_smile:

fix some error but wrong place here

Does not work :frowning: after

sudo update-grub 
Operation = Install
Operation = Upgrade
Operation = Remove
Type = Path
Target = /boot/grub/grub.cfg

Description = Edit configfile path for grub-btrfs.cfg in grub.cfg
When = PostTransaction
Exec = /usr/bin/sed -i 's/\${prefix}/\/\@\/boot\/grub/' /boot/grub/grub.cfg

Where the man hooks, ahhh

man alpm-hooks

found in

man pacman

, always difficult to find if you do not know what you are searching :smiley:

Type = Package
Target = update-grub


with Path (or File) not write the first "/" (as NoExtract in pacman.conf)
Target = boot/grub/grub.cfg

~ >>> sudo cat /boot/grub/grub.cfg | grep 'configfile '                                                                                                  
[sudo] Passwort fΓΌr sgs: 
        configfile "${prefix}/grub-btrfs.cfg"
~ >>> sudo update-grub                                                                                                                                   
Generating grub configuration file ...
Found theme: /usr/share/grub/themes/manjaro/theme.txt
Found linux image: /boot/vmlinuz-5.5-x86_64
Found initrd image: /boot/intel-ucode.img /boot/initramfs-5.5-x86_64.img
Found initrd fallback image: /boot/initramfs-5.5-x86_64-fallback.img
Found linux image: /boot/vmlinuz-5.4-x86_64
Found initrd image: /boot/intel-ucode.img /boot/initramfs-5.4-x86_64.img
Found initrd fallback image: /boot/initramfs-5.4-x86_64-fallback.img
Found Manjaro i3wm (19.0.0) on /dev/sda2
###### - Grub-btrfs: Snapshot detection started - ######
# Info: Separate boot partition not detected 
# Found snapshot: 2020-02-15 21:59:29 | timeshift-btrfs/snapshots/2020-02-15_21-59-29/@
# Found snapshot: 2020-02-15 17:06:04 | timeshift-btrfs/snapshots/2020-02-15_17-06-03/@
# Found snapshot: 2020-02-15 16:41:55 | snapshots/@home_2020-02-15
# Found snapshot: 2020-02-15 16:33:37 | snapshots/@_2020-02-15
# Found snapshot: 2020-02-15 06:18:07 | timeshift-btrfs/snapshots/2020-02-15_06-18-07/@
# Found snapshot: 2020-02-15 05:52:55 | timeshift-btrfs/snapshots/2020-02-15_05-52-55/@
# Found snapshot: 2020-02-14 21:03:07 | timeshift-btrfs/snapshots/2020-02-14_21-03-07/@
# Found 7 snapshot(s)
###### - Grub-btrfs: Snapshot detection ended   - ######
Found memtest86+ image: /boot/memtest86+/memtest.bin
~ >>> sudo cat /boot/grub/grub.cfg | grep 'configfile '                                                                                                  
        configfile "${prefix}/grub-btrfs.cfg"

Some more mistakes :wink: inside

I am sooooooo stupid :wink:

Pacman hooks are hooking into pacman .. one needs to install/remove/update packages.

$ sudo update-grub

Calls a script to generate a new grub config - this is no pacman action at all: no pacman hook will be triggered.


yes ! :laughing: :scream:

pacman -Qo /boot/grub/grub.cfg
error: No package owns /boot/grub/grub.cfg

or ? create same command in /etc/grub.d/99-btrfs

1 Like
~ >>> sudo sed -i 's/\${prefix}/\/\@\/boot\/grub/' /boot/grub/grub.cfg                                                             [2]
~ >>> sudo cat /boot/grub/grub.cfg | grep 'configfile '                                                                               
        configfile "/@/boot/grub/grub-btrfs.cfg"

Thank you both :slight_smile:
@freggel.doe and @papajoke

alias grubbtrfs='sudo sed -i "s/\${prefix}/\/\@\/boot\/grub/" /boot/grub/grub.cfg'

So, for pacman test I need a fake package :wink: or wait :smiley: :wink:

No - you're just eager to get something to work your way :slight_smile:

1 Like

as writed,
you can test your "dangerous" code in /etc/grub.d/99-btrfs but I have no idea if we can change grub.cfg at this moment ...

1 Like

The main problem is 41_snapshots-btrfs

excerpt (end) of this

### Start ###
printf "###### - Grub-btrfs: Snapshot detection started - ######\n" >&2 ;
# if btrfs prog isn't installed, exit
# Delete existing config
#rm -f --preserve-root "/boot/$grub_directory/grub-btrfs.cfg"
> "/boot/$grub_directory/grub-btrfs.cfg"
# Create mount point then mounting
[[ ! -d $gbgmp ]] && mkdir -p $gbgmp
mount -o subvolid=5 /dev/disk/by-uuid/$root_uuid $gbgmp/
# Count menuentries
# Count snapshots
# detect uuid requirement
# Detects if /boot is a separate partition
if [[ "$override_boot_partition_detection" == "true" ]]; then
	printf "# Info: Override boot partition detection : enable \n" >&2 ;
	if [[ "$root_uuid" != "$boot_uuid" ]]; then
		printf "# Info: Separate boot partition detected \n" >&2 ;
		printf "# Info: Separate boot partition not detected \n" >&2 ;
# unmounting mount point
umount $gbgmp
# Show warn, menuentries exceeds 250 entries
[[ $count_warning_menuentries -ge 250 ]] && printf "# Generated ${count_warning_menuentries} total GRUB entries. You might experience issues loading snapshots menu in GRUB.\n" >&2 ;
# printf "# menuentries = $count_warning_menuentries \n" >&2 ;
# Show total found snapshots
if [[ "${show_total_snap_found}" = "true" && ! -z "${count_limit_snap}" && "${count_limit_snap}" != "0" ]]; then
	printf "# Found ${count_limit_snap} snapshot(s)\n" >&2 ;
# if no snapshot found, exit
if [[ "${count_limit_snap}" = "0" || -z "${count_limit_snap}" ]]; then
	print_error "No snapshots found."
root_grub="$(make_system_path_relative_to_its_root /boot/$grub_directory)"
# Make a submenu in GRUB (grub.cfg)
cat << EOF
submenu '${submenuname}' {
	configfile "\${prefix}/grub-btrfs.cfg"
printf "###### - Grub-btrfs: Snapshot detection ended   - ######\n" >&2 ;
### End ###

It work for other, but I must put the path /@/boot/grub inside for ${prefix}

Needs advice on some hook :yum:

Usually if i need to launch some scripts i do something like that:

Exec = /usr/bin/bash -c '; || echo "[!] Error"'

But what if one of those scripts need to run not as root to execute successfully?
Since hooks are executed as root by default, i'm not sure what's the right way to launch script from hook, as if it was executed as $USER...

I'm not sure there is a straight forward way of doing that. I'm not saying it can't be done, but I have no idea how you might accomplish that.

It might be possible to call sudo as your user to execute in a non privileged manner.


I think you're right, i was surfing and found that, i assume it's kinda what you mean...

Looks like it works, let's say you want to execute as your current user in hook, then you do:

Exec = /usr/bin/bash -c 'sudo -u "$SUDO_USER"; || echo "[!] Error"'

At least on surface it looks like it works properly :slight_smile:

P.S. Never knew of $SUDO_USER before, pretty handy for some tasks :upside_down_face:

P.P.S. Yes, it definitely works as intended :partying_face:

1 Like

Yes I use that method a lot when writing services, but I was skeptical it would work in a hook.

Are you positive using:


is executing as "user". I would have thought that method would execute as root. Usually I simply substitute my own user name when writing a service.


Define substitute :slight_smile: please.

I use your 50_pacman-list.hook

Exec = /bin/sh -c 'pacman -Qqen  > "/home/sgs/.local/bin/pacman-list/$(date +%Y-%m-%d@%H:%M)_native.log";chown -R sgs:sgs /home/sgs/.local/bin/pacman-list  2> /dev/null; exit'

is fine and I have no root files in ~/

I try

Exec = /bin/sh -c 'sudo -u "sgs" pacman -Qqen  > "/home/sgs/.local/bin/pacman-list/$(date +%Y-%m-%d@%H:%M)_native.log" 2> /dev/null; exit'

failed to root entry

-rw-r--r-- 1 sgs  sgs  3,7K 21.04.2020 19:06 2020-04-21@19:06_native.log
-rw-r--r-- 1 root root 3,7K 21.04.2020 21:49 2020-04-21@21:49_native.log

it must be



     -u user, --user=user
                 Run the command as a user other than the default target user (usually root).  The user may be either a user name or a numeric
                 user-ID (UID) prefixed with the β€˜#’ character (e.g., #0 for UID 0).  When running commands as a UID, many shells require that
                 the β€˜#’ be escaped with a backslash (β€˜\’).  Some security policies may restrict UIDs to those listed in the password database.
                 The sudoers policy allows UIDs that are not in the password database as long as the targetpw option is not set.  Other security
                 policies may not support this.

or without quotes?? :wink:

As always, I need simple samples :smiley:

Forum kindly sponsored by