Testing our package build in the Docker world
Tags: archlinux, bash, linux, docker
Published: 2020-09-09 11:01:20

We write and build our `PKGBUILD` files in our local machine, but we need to make sure our build script is portable and up to date with the required dependencies.

If you've built a CI before, you already know that Docker is a de facto solution for this. You should create a container, install the dependencies to build Arch Linux package, and then build your PKGBUILD to check the success result.

## Creating the container

First, we should install the docker package and start its service:

```bash
sudo pacman -S docker
sudo systemctl start docker.service
```

Then, start an interactive container using the Arch Linux image as base:

```bash
sudo docker run -it archlinux bash
```

## Preparing the container

We should add the `base-devel` package group to our container - these packages are required to build packages ourselves. The Arch Linux team is working in a new docker image `archlinux:devel` with this [package group pre-installed](https://gitlab.archlinux.org/archlinux/archlinux-docker/-/issues/35).

And, Git if we want to test a `PKGBUILD` hosted in the AUR repository.

```bash
pacman -Sy --noconfirm base-devel git
```

💡 Tip: Maybe you need to check the `/etc/pacman.d/mirrorlist` file to get a good bandwidth based on your geolocation.

## Add a non-root user

For security reasons, `makepkg` can't run as `root`, but we need to add a user with sudo to build the package.

```bash
useradd builduser -m # Create the builduser
passwd -d builduser # Delete the buildusers password
printf 'builduser ALL=(ALL) ALL\n' | tee -a /etc/sudoers # Allow the builduser passwordless sudo
```

Now, change the user:

```bash
su builduser && cd
```

## Cloning the AUR package

We can clone the AUR package without using an AUR helper, like `yay`. We'll use the .Net Core app we visited in [the previous post](https://dev.to/cloudx/how-to-distribute-a-net-core-app-in-arch-linux-13nj).

```bash
git clone https://aur.archlinux.org/taskcore.git
```

## Build the package

And now we can build and install our package

```bash
makepkg -si --noconfirm
```

Then, clean the orphan packages that we don't need on runtime:

```bash
pacman -Rns $(pacman -Qtdq)
```

## Automate the build process

We can create a `Dockerfile` to run the build steps without an interactive terminal.

```Dockerfile
FROM archlinux

RUN pacman -Sy --noconfirm base-devel git

ARG AUR_PACKAGE=taskcore

RUN useradd builduser -m \
  && passwd -d builduser \
  && cd /home/builduser \
  && git clone "https://aur.archlinux.org/$AUR_PACKAGE.git" target \
  && chown builduser -R target \
  && (printf 'builduser ALL=(ALL) ALL\n' | tee -a /etc/sudoers) \
  && sudo -u builduser bash -c 'cd ~/target && makepkg -si --noconfirm' \
  && pacman -Rns $(pacman -Qtdq)

WORKDIR /home/builduser/target

CMD ["bash"]
```

We can build other packages using the `--build-arg` argument:

```bash
sudo docker build --build-arg AUR_PACKAGE=git-delta -t git-delta .
```

## Run the package!

Don't forget to run the package to check the runtime dependencies.

```bash
docker run -it taskcore
taskcore ls
```

## New base-devel image! (Update 2020-11-06)

The ticket was [closed](https://gitlab.archlinux.org/archlinux/archlinux-docker/-/issues/35) and the new `archlinux:base-devel` image is available, with a current size of 700MB vs 467MB for `archlinux:latest` (a.k.a. `archlinux:base`).