Skip to content

Instantly share code, notes, and snippets.

@skorotkiewicz
Created September 4, 2025 06:33
Show Gist options
  • Save skorotkiewicz/8eead068db8784abb05f9e5f02b49f33 to your computer and use it in GitHub Desktop.
Save skorotkiewicz/8eead068db8784abb05f9e5f02b49f33 to your computer and use it in GitHub Desktop.
Build AUR packages in container (systemd-nspawn) and then install on host (Arch Linux)
#!/usr/bin/env bash
set -euo pipefail
# chmod +x /usr/local/bin/yayc
# Config
CONTAINER_NAME="aur-builder"
CONTAINER_DIR="/var/lib/machines/$CONTAINER_NAME"
PKG_OUT="/var/cache/aur-builder" # here are .zst packages
HOST_USER="$(id -un)"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
BOLD="\e[1m"
RESET="\e[0m"
usage() {
echo "Usage: $0 [--rebuild] [packages...]"
echo " --rebuild Rebuild the container from scratch"
exit 1
}
# Create/use container
build_container() {
echo -e "${GREEN}${BOLD}[*] Creating container${RESET} ${BLUE}$CONTAINER_NAME...${RESET}"
sudo rm -rf "$CONTAINER_DIR"
sudo mkdir $CONTAINER_DIR
sudo pacstrap -K -c "$CONTAINER_DIR" base base-devel git --noconfirm
sudo systemd-nspawn -D "$CONTAINER_DIR" --machine="$CONTAINER_NAME" \
bash -c "useradd -m build && echo 'build ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/build"
echo -e "${GREEN}${BOLD}[*] Container created.${RESET}"
}
# Check args
REBUILD=0
PKGS=()
for arg in "$@"; do
case "$arg" in
--rebuild) REBUILD=1 ;;
-h|--help) usage ;;
*) PKGS+=("$arg") ;;
esac
done
if [[ $REBUILD -eq 1 ]] || ! sudo test -d "$CONTAINER_DIR"; then
build_container
sudo sed -i "s/^ParallelDownloads = [0-9]\+$/ParallelDownloads = 1/" "$CONTAINER_DIR/etc/pacman.conf"
fi
if [[ ${#PKGS[@]} -eq 0 ]]; then
echo "No packages to build."
exit 0
fi
# Create folder for package
sudo mkdir -p "$PKG_OUT"
echo -e "${GREEN}${BOLD}[*] Building packages:${RESET} ${BLUE}${PKGS[*]} ${RESET}"
# Run yay in container
sudo systemd-nspawn -D "$CONTAINER_DIR" --machine="$CONTAINER_NAME" \
--bind "$PKG_OUT:/output" \
bash -c "
set -e
if ! id build >/dev/null 2>&1; then useradd -m build; fi
su - build -c '
if ! command -v yay >/dev/null; then
git clone https://aur.archlinux.org/yay.git
cd yay && makepkg -si --noconfirm
fi
mkdir /home/build/tmp
yay -S --noconfirm --builddir /home/build/tmp --mflags --skipinteg ${PKGS[*]}
sudo mv /home/build/tmp/**/*.zst /output/
rm -rf /home/build/tmp
'
"
# Install on host
latest_pkg=$(ls -t "$PKG_OUT"/*.zst | head -n1)
echo -e "${GREEN}${BOLD}[*] Installing packages on host...${RESET} ${BLUE} $latest_pkg ${RESET}"
sudo pacman -U --noconfirm "$latest_pkg"
sudo ls $latest_pkg
# sudo rm $latest_pkg
# echo -e "${GREEN}${BOLD}[*] Building packages...${RESET}"
# echo -e "${YELLOW}Warning:${RESET} something happened"
# echo -e "${RED}${BOLD}Error:${RESET} failed to build"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment