Skip to content

Instantly share code, notes, and snippets.

@Tremeschin
Last active July 18, 2024 19:49
Show Gist options
  • Save Tremeschin/2569cfed9be6fda555aa529bd49acbf3 to your computer and use it in GitHub Desktop.
Save Tremeschin/2569cfed9be6fda555aa529bd49acbf3 to your computer and use it in GitHub Desktop.
Synchronize a whitelist of packages on an Arch Linux system
#!/bin/bash
#
# (c) 2024, Tremeschin, MIT License
#
# pacsync - Synchronize a whitelist of packages on an Arch Linux system
#
# This scripts remove all non-related and non-dependencies of a package list, acting like a lock
# mechanism to match the system with a list of allowed packages, 'pacman.lock' vibes
#
# Usage: Add this file to your PATH or as a bash function, then run:
# pacsync package1 package2 package3 ...
# pacsync /path/to/packages-list
#
# Dangers:
# - Be sure to list essential packages like 'systemd, linux, base-devel, pacman, sudo', etc
# - I'm not sure if there's side effects of marking all packages "asdeps", but running the
# "remove orphans" command becomes unsafe if you don't list all packages you want to keep
#
function pacsync() {
yay=$(command -v yay || command -v paru)
if [[ -z $yay ]]; then
echo "Error: yay or paru not found in PATH"
return 1
fi
# If first argument is a file, read packages from it
if [[ -f $1 ]]; then
set -- $(<"$1")
fi
# Set all packages install reason to 'dependency'
$yay -D --asdeps $($yay -Qq) > /dev/null
# Set all packages passed as arguments to 'explicit';
# The package might be a group on the failure of the first
for package in "$@"; do
$yay -D --asexplicit $package > /dev/null 2>&1 || \
$yay -D --asexplicit $($yay -Qgq $package) > /dev/null 2>&1
done
# Remove orphans, that is, all unwanted :)
$yay -Qdtq | $yay -Rns -
# Install all wanted packages
$yay -Syu --needed "$@"
}
pacsync "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment