Running Guix in Container Toolbox
Preamble #
For some time now, I’ve been running Fedora Kinoite on my main workstation. This in an atomic desktop with graphical applications installed via Flatpak and containerized development environments managed by Toolbx. Running Guix on such a distribution can be something of a challenge; Sebastian Dümcke describes his approach here: Running guix package manager on top of Fedora Silverblue. This should also work for Fedora Kinoite, but I did not like that it requires switching SELinux to non-enforcing mode.
Inspired by the instructions for Installing Guix on a Cluster, I wondered if I could do something similar on my workstation, but instead of the Guix daemon running on a server, it would run in a local podman container, and instead of using a network filesystem, I can just share podman volumes between containers. Sounds simple…
Updating toolbox #
The first hurdle is that toolbox does not support mounting additional volumes;
this needs to be done when the container is created, as you cannot add volumes
to an existing container. But it’s free software, so I can fix that!
I forked the project on GitHub and added support to the toolbox create command
for defining additional volumes and environment variables. The changes are
fairly minimal; you can find my fork on GitHub.
I created a Fedora 42 toolbox (matching my workstation OS version) to build the
modified version of toolbox:
# Create and enter the toolbox
toolbox create -d fedora -r 42 toolbx-build
toolbox enter toolbx-build
# Install build dependencies
sudo yum install meson gcc shadow-utils-subid-devel.x86_64 golang go-md2man
# Fetch the source code
cd $(mktemp -d)
git clone https://github.com/ray1729/toolbox.git
cd toolbox
git checkout feature/container-create-extras
# Build the binary
meson setup --reconfigure builddir
meson compile -C builddir/
# Install to local bin dir
mkdir -p ~/.local/bin
cp ./builddir/src/toolbox ~/.local/bin/toolbox
# Exit the toolbox
exit
I’m done with the build container, so cleaned it up:
podman stop toolbx-build
toolbox rm toolbx-build
Note: I have not installed the manual pages for this version of toolbox, so
running toolbox create --help will display the manual page for the system-installed
version which doesn’t include the new options.
Configuring the Guix daemon #
I run the Guix daemon in a rootless podman container, taking advantage of the user systemd service. The first step is to build a container for the daemon, which I do by installing
Guix on a debian:bookworm-slim base image.
Note: I had some problems running the Guix daemon from the package available in Debian in the container, so I installed it from the archive.
pushd $(mktemp -d)
cat > Containerfile <<EOF
FROM docker.io/library/debian:bookworm-slim
RUN apt-get -qq update && apt-get -qq upgrade && apt-get -qq install wget gpg xz-utils netbase locales
ADD --chmod=0755 https://git.savannah.gnu.org/cgit/guix.git/plain/etc/guix-install.sh /etc/guix-install.sh
RUN yes '' | /etc/guix-install.sh
RUN rm /etc/guix-install.sh
VOLUME /gnu/store
VOLUME /var/guix
EXPOSE 44146
ENTRYPOINT ["/root/.config/guix/current/bin/guix-daemon", "--build-users-group=guixbuild", "--listen=/var/guix/daemon-socket/socket", "--listen=127.0.0.1"]
EOF
podman build -t debian-guix:latest .
popd
Next, I created podman quadlets for the shared volumes and the daemon service:
QUADLETS="${HOME}/.config/containers/systemd/"
mkdir -p "${QUADLETS}"
cat > "${QUADLETS}/gnu-store.volume" <<EOF
[Unit]
Description = GNU Store data volume
[Service]
RemainAfterExit = yes
[Volume]
EOF
cat > "${QUADLETS}/guix-var.volume" <<EOF
[Unit]
Description = Guix var volume
[Service]
RemainAfterExit = yes
[Volume]
EOF
cat > "${QUADLETS}/guix-daemon.container" <<EOF
[Unit]
Description = Guix Daemon
[Container]
Image = localhost/debian-guix:latest
ContainerName = guix-daemon
Volume = guix-var.volume:/var/guix:z
Volume = gnu-store.volume:/gnu/store:z
PodmanArgs = --privileged
Network = host
[Install]
WantedBy = multi-user.target default.target
EOF
Note: The z option on the volumes tells podman to add SELinux labels that
allow them to be shared by multiple containers.
Next I reloaded systemd and started the service:
systemctl --user daemon-reload
systemctl --user start guix-daemon.service
If all went well, the Guix daemon will now be listening on localhost port 44146.
The steps so far only need to be done once: next time I login, systemd will take care of starting the Guix daemon. If I need it to be running all the time (even when not logged in), I can enable linger for my user:
sudo loginctl enable-linger ray
Creating the Guix toolbox #
Now that all the pieces are in place, I can create a toolbox to run Guix on a foreign distribution. I usually use Ubuntu for my development containers, so:
~/.local/bin/toolbox create -d ubuntu -r 24.10 \
-V "systemd-gnu-store:/gnu/store:z,ro" \
-V "systemd-guix-var:/var/guix:z,rw" \
-E GUIX_DAEMON_SOCKET="guix://127.0.0.1:44146" \
guix-ubuntu
The -V and -E options are the new options I added to define volumes and
environment variables when the container is created. The /gnu/store volume is
mounted read-only, while /var/guix is mounted read/write to allow creation of
user profiles etc. in the toolbox.
When I first entered the container, I had to use the guix binary in root’s
profile to install the first package; this created the user profile along with
the ~/.guix/profile symlink in my home directory:
toolbox enter guix-ubuntu
/var/guix/profiles/per-user/root/current-guix/bin/guix install glibc-locales
exit
For convenience, I added some setup to $HOME/.bashrc.d/guix.sh:
if [ -n "${GUIX_DAEMON_SOCKET}" ]; then
GUIX_PROFILE="${HOME}/.guix-profile"
export GUIX_LOCPATH="${GUIX_PROFILE}/lib/locale"
. "$GUIX_PROFILE/etc/profile"
fi
Now when I enter the toolbox, guix is on my path:
toolbox enter guix-ubuntu
> which guix
/home/ray/.guix-profile/bin/guix
Conclusion #
With this setup, I can easily make Guix available to multiple Toolbox
containers, simply by mounting the /gnu/store and /var/guix volumes when
creating the container. Any Guix profiles I create (and the packages installed
into those profiles) will be available across all containers.
It’s reasonably space-efficient, with only one copy of the GNU store (in the shared podman volume).
It’s also reasonably secure, with the daemon running in a rootless container under my system UID, and SELinux remaining in enforcing mode.