Skip to content

Instantly share code, notes, and snippets.

@jpsutton
Last active September 2, 2023 23:09
Show Gist options
  • Save jpsutton/77226485ba832182fe2e1410fb2c09c3 to your computer and use it in GitHub Desktop.
Save jpsutton/77226485ba832182fe2e1410fb2c09c3 to your computer and use it in GitHub Desktop.
yay-wrapper
#!/bin/bash
# Flags to enable/disable various functionality (set flag to zero to disable)
UPDATE_MIRRORLIST=1
UPDATE_KEYRING=1
USE_POWERPILL=1
REPORT_CACHE_STATS=1
# Config elements
MIRRORLIST="/etc/pacman.d/mirrorlist"
MAX_MIRRORLIST_AGE=9
MIRROR_COUNT=10
MIRROR_COUNTRY=US
# Check for missing kernel module directory
if [ ! -d /lib/modules/$(uname -r) ]; then
echo "WARNING: Your kernel has been upgraded since you last booted. Consider rebooting when it is convenient." >&2
fi
# Check the age of the mirrorlist file in days (configurable)
if [ "$UPDATE_MIRRORLIST" -ne 0 ]; then
mirrorlist_age=$((($(date +%s) - $(date +%s -r "$MIRRORLIST")) / 86400))
if [ "$_FORCE_RANKMIRROR" != "" ] || [ "$mirrorlist_age" -gt "$MAX_MIRRORLIST_AGE" ]; then
echo "Pacman mirrorlist is ${mirrorlist_age} days old; ranking mirrors to create a new list..."
sudo mv "$MIRRORLIST" "${MIRRORLIST}.bak"
curl -s "https://archlinux.org/mirrorlist/?country=${MIRROR_COUNTRY}&protocol=https&use_mirror_status=on" | sed -e 's/^#Server/Server/' -e '/^#/d' | rankmirrors -n "$MIRROR_COUNT" - | sudo tee "$MIRRORLIST"
fi
fi
# Report cache stats (configurable)
if [ "$REPORT_CACHE_STATS" -ne 0 ]; then
echo $(sudo ls /var/cache/pacman/pkg/ | wc -l) packages in cache
echo $(du -sh /var/cache/pacman/pkg/) in storage space
fi
# Update Archlinux Keyring package before everything else (configurable)
if [ "$UPDATE_KEYRING" -ne 0 ]; then
sudo pacman -Sy --needed --noconfirm archlinux-keyring
fi
# Use powerpill if it's available (configurable)
if [ "$USE_POWERPILL" -ne 0 ]; then
PACMAN_EXEC=$(which powerpill || which pacman)
else
PACMAN_EXEC=$(which pacman)
fi
# Do the needful
exec yay --pacman="$PACMAN_EXEC" "$@"
@jpsutton
Copy link
Author

jpsutton commented Feb 1, 2023

Updated to add warning for updated kernel package, as some functionality doesn't function as desired when you're booted to a kernel that has upgraded (e.g., can't load modules for the booted kernel). I also changed the final call to yay to use exec so that the return code from yay is passed along to the caller of the wrapper script.

@jpsutton
Copy link
Author

jpsutton commented Sep 2, 2023

Updated to add powerpill support as well as adding config flags to enable/disable various functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment