68. Building containers with Nix and Gitlab CI

submited by
Style Pass
2023-03-24 12:30:07

Let’s build a container around a Rust webserver and some static files using Nix and Gitlab CI. The process is what you’d expect, but there are a few details that are annoying to puzzle out.

First up is the Nix flake which has the build definitions. The packages.​site output is the Rust webserver built with naersk. Then there’s the packages.​container output created with buildLayeredImage from nixpkgs. The container includes both the site binary and the ./dist directory of static files. The latter doesn’t have a dedicated package and is just included as-is.

Next up is the Gitlab CI config file which tells the build host how to actually build our project. Essentially, we want to create the container with nix build .#container and then upload it to our registry with skopeo. Practically, there’s lots of pomp and ceremony around this:

With the overview done, let’s zoom in on the Nix flake. It starts with the usual boilerplate for Rust flakes (see A NixOS flake for Rust, egui, and OpenGL if you need a refresher).

Leave a Comment