Jump to content

Continuous Integration (CI)

From NixOS Wiki

Nix nicely integrates into your CI.

TODO: some general tips & tricks

Avoid downloading already built results

nix-build will always ensure the built store path is put in the local store, be it by building or by downloading from a substituter. On CI, we often only want to check whether we can build the derivation, without using or running the output.

This can be achieved by using --dry-run to check whether the result would be fetched, and only building it it has to be built.

nix-build-if-changed.py implements this in a relatively straightforward (but naïve) Python script.

nix-build-uncached implements it in a slightly more elaborate manner, and is available on nixpkgs (as the nix-build-uncached package).

Caching built results

After building your project you might want to cache the results. The cache server could be a Cachix cache, a self-hosted Attic cache or even your own nix machine.

nix-fast-build uses nix-eval-jobs in parallel to speed-up the evaluation and building process. It's useful for building flakes that have multiple outputs. It also supports uploading to Cachix and Attic.

You can also use nix-copy-closure to directly upload to a remote /nix/store through a SSH connection. As it's already built in Nix, It's the simplest way to cache the results. Albeit, in my experience it's slower.[citation needed]


Instructions for specific CI Providers

Travis CI

See Nix_on_Travis article

Github actions

See install-nix-action to install nix in Linux/macOS actions. There is also one action to setup cachix, a hosted binary cache.

Self-hosted runners

NixOS has a few unofficial modules for running self-hosted GitHub runners. see srvos, juspay/github-nix-ci

Build kite

See the Buildkite article

Drone

There is no official NixOS module however both drone and drone-cli are packaged in nixpkgs.

Mic92 has the following custom module in his repository. An example public project can be found in cntr and this project for an example using flakes Mic92's dotfiles.

Jenkins

Jenkins

Gitlab

NixOS has a module for Gitlab runner

Garnix

Garnix is a nix-specific CI provider that also provides a cache. it currently only works with flake-enabled repos.

Sourcehut

Sourcehut provides an official NixOS image