Introduction

Why

Building in a clean chroot prevents missing dependencies in packages, whether due to unwanted linking or packages missing in the depends array in the PKGBUILD. It also allows users to build a package for the stable repositories (core, extra, community) while having packages from [testing] installed.

Convenience Way

To quickly build a package in a chroot without any further tinkering, one can use the helper scripts from the devtools package: $REPO-$ARCH-build

These helper scripts should be called in the same directory where the PKGBUILD is, just like with makepkg. For instance, extra-i686-build automatically sets up chroot in /var/tmp/archbuild, updates it, and builds a package for the extra repository. For multilib builds there is just multilib-build without an architecture.

Classic Way

Setting Up A Chroot

The devtools package provides tools for creating and building within clean chroots. Install it if not done already:

pacman -S devtools

To make a clean chroot, create a directory in which the chroot will reside. For example, $HOME/chroot.

Note: One can also define the CHROOT variable in $HOME/.bashrc using the export command if the location is to be repeatedly used.

Edit $CHROOT/root/etc/makepkg.conf to set the packager name and any makeflags. Also adjust the mirror list in $CHROOT/root/etc/pacman.d/mirrorlist and enable [testing] in $CHROOT/root/etc/pacman.conf if desired.

Alternatively, provide a custompacman.conf and makepkg.conf with the following:

It is recommended however users do not use custom pacman.conf and makepkg.conf during the initial creation of clean chroot to ensure no user-specific adjustments are made. Use with caution.

Building in the Chroot

Firstly, make sure the chroot is up to date with:

sudo mkarchroot -u $CHROOT/root

Then, to build a package in the chroot, run the following from the dir containing the PKGBUILD:

sudo makechrootpkg -c -r $CHROOT

A unionfs is used to maintain the clean chroot during building. All installed dependencies or makedepends and other changes made during building are done in $CHROOT/rw. Passing the -c flag to makechrootpkg ensures that this directory is cleaned before building starts.

Handling Major Rebuilds

The cleanest way to handle a major rebuild is to create a new chroot and build the first package (typically the package for which the rebuild is meant). Then create a local repo inside the new chroot. To do this:

sudo mkdir $CHROOT/root/repo
sudo chmod 0777 $CHROOT/root/repo

The chmod statement allows for the coping of package files and for the creation of the local repo as your user rather than root.