Allows a gitlab-runner to share the /nix
store between builds and thus improve compilation
times and leverages the full potential of nix. Only works for docker-runners.
- You don't want to rebuild large dependencies on every run (I am looking at you, CUDA)
- You want to build large, multi staged derivations which depend each other
- You want to share build results across multiple repositories
- You already have a bunch of gitlab runner and can't be bothered to host a real hydra
- You cannot host your gitlab runner on a nixos machine
The gitlab runners still require the nixos/nix base image. To share the nix-store you'll have to copy its initial contents to the runner- host and mount the resulting dir in all docker runners.
To do so, you'll have to follow these steps
- "Pin" the
nixos/nix
-Image hash: Check the docker repo and copy the sha-hash. This enables you to reference the image f.e. bynixos/nix@sha256:2da921898aa6c89e2e60b1fb72d74525b8464b47412482c7f1cf77b8e707a099
- Execute
copy-nix-store.sh
from this repository on your host machine. This script takes the image name as its first and only parameter (thenixos/nix@hash
combination) from which it should copy the nix store. The result is stored in$CWD/nix-backup
. - Locate your gitlab-runner
config.toml
(see docs) and find the correct runner.docker section (take care: there may be multiple, one for each of your registered repositories). - Update your
volumes
entry to contain two new mount points:
volumes=["/path/to/nix-backup/nix/store:/nix/store:rw","/path/to/nix-backup/nix/var/nix/db:/nix/var/nix/db:rw"]
- Restart the runner ( by using
gitlab-runner restart
) or wait for it to pickup the changes. - Update your
.gitlab-ci.yml
to only use the image you have pinned above.
To upgrade the nixos/nix base image, simply delete your nix-backup
dir and
execute copy-nix-store.sh
again with your new base image. You may need
to stop your runner before you delete the shared folders and restart it afterwards.
- The
/nix
dir will be available in every docker-image you use in your pipeline, even non-nixos images if you use any.
- Original idea by Arian van Putten over at https://gitlab.com/arianvp/nixos-gitlab-runner