Skip to content

Instantly share code, notes, and snippets.

@Strykar
Forked from michaelbeaumont/mkosi.build.chroot
Created June 28, 2025 20:08
Show Gist options
  • Select an option

  • Save Strykar/db26343797caaeb619abfe37f05bfbc0 to your computer and use it in GitHub Desktop.

Select an option

Save Strykar/db26343797caaeb619abfe37f05bfbc0 to your computer and use it in GitHub Desktop.

Revisions

  1. @michaelbeaumont michaelbeaumont revised this gist May 24, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mkosi.build.chroot
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ aur fetch ${aur_deps[@]}
    for pkg in ${aur_deps[@]}; do
    pushd ${pkg}
    sudo -u nobody BUILDDIR="/tmp/build" SRCDEST="/tmp/build" aur build --no-sync
    pacinstall --dbsync --no-confirm --resolve-conflicts=provided ${pkg}
    pacinstall --dbsync --no-confirm --yolo ${pkg}
    popd
    done

  2. @michaelbeaumont michaelbeaumont renamed this gist May 24, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @michaelbeaumont michaelbeaumont revised this gist May 24, 2024. No changes.
  4. @michaelbeaumont michaelbeaumont created this gist May 24, 2024.
    31 changes: 31 additions & 0 deletions mkosi.build.chroot
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/usr/bin/env bash
    set -ex

    PACKAGES=(
    neovim-git
    )

    # All this is basically to get around makepkg calling pacman with sudo
    # Otherwise we could just call `aur sync` and be done with it

    cd /tmp

    pacman_deps=$(aur depends ${PACKAGES[@]} --json --all | jq -r '.[] | select(.ID == null) | .Name')
    pacman -S --needed --noconfirm ${pacman_deps[@]}

    # We install needed deps in topological order using tsort
    aur_deps=$(aur depends ${PACKAGES[@]} -r | tsort)

    aur fetch ${aur_deps[@]}
    for pkg in ${aur_deps[@]}; do
    pushd ${pkg}
    sudo -u nobody BUILDDIR="/tmp/build" SRCDEST="/tmp/build" aur build --no-sync
    pacinstall --dbsync --no-confirm --resolve-conflicts=provided ${pkg}
    popd
    done

    # Move what we actually need into the image
    cp -r --no-preserve=ownership /aur ${DESTDIR}
    cp --parents /etc/pacman.d/aur.conf ${DESTDIR}

    reflector --latest 5 --sort rate --save ${DESTDIR}/etc/pacman.d/mirrorlist
    18 changes: 18 additions & 0 deletions mkosi.postinst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/usr/bin/env bash
    set -ex

    # Setup pacman to read modular configs
    mkdir -p /etc/pacman.d
    echo > /etc/pacman.d/dummy.conf "# This exists just so /etc/pacman.d is never empty and we can Include = /etc/pacman.d/*.conf"
    echo -e "\nInclude = /etc/pacman.d/*.conf" >> /etc/pacman.conf

    # Configure pacman
    pacman-key --init
    pacman-key --populate

    # Install the aur packages we just built
    pacman -Sy
    pacinstall --dbsync \
    --no-confirm \
    --yolo \
    $(pacman -Sql aur)
    45 changes: 45 additions & 0 deletions mkosi.prepare.chroot
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/usr/bin/env bash
    set -e

    if [[ "${1}" != "build" ]]; then
    exit 0
    fi

    # Setup pacman
    reflector --latest 5 --sort rate --save /etc/pacman.d/mirrorlist
    pacman-key --init
    pacman-key --populate

    cd /tmp

    # Manually build and install aurutils
    sudo -u nobody git clone https://aur.archlinux.org/aurutils.git
    pushd aurutils
    (
    source ./PKGBUILD
    pacman -S \
    --needed \
    --noconfirm \
    "${makedepends[@]}" "${depends[@]}" "${checkdepends[@]}"
    )
    sudo -u nobody PKGDEST="/tmp" PKGEXT=".pkg.tar" makepkg --clean --cleanbuild
    popd

    pacman -U --noconfirm /tmp/aurutils-*.pkg.tar

    # Add local repo reference
    echo > /etc/pacman.d/aur.conf "
    [aur]
    SigLevel = Optional TrustAll
    Server = file:///aur"
    echo -e "\nInclude = /etc/pacman.d/*.conf" >> /etc/pacman.conf

    # Create empty repository
    # All the arch tooling assumes we can use sudo when we can't here
    # so we own this repo as -u nobody and call pacman explicitly

    install -d /aur -o nobody
    sudo -u nobody tar -ca -f /aur/aur.db.tar.xz -T /dev/null
    sudo -u nobody ln -s /aur/aur.db.tar.xz /aur/aur.db

    pacman -Sy