Allows (advanced) configuration of GitLab CI using Nix. https://nix-gitlab-ci.projects.tf
Find a file
2025-05-10 19:48:20 +02:00
docs docs: write docs & improve tooling 2025-05-02 17:30:53 +02:00
lib fix(flakeModule): use bash for stdenvMinimal 2025-05-10 19:45:10 +02:00
scripts fix(script): quote NEW_PATHS to preserve newlines 2025-04-04 22:22:35 +02:00
snapshots tests: try testing with nixtest 2025-05-10 19:48:20 +02:00
templates chore(scripts): improve cache strategy handling, see #21 2025-03-22 21:50:33 +01:00
.envrc fix(image): switch from busybox to coreutils 2025-04-04 20:30:56 +02:00
.gitignore chore: initial commit 2024-01-14 16:45:07 +01:00
.gitlab-ci.yml refactor(template): get rid of rules and work around limitations in a new way 2025-03-04 16:12:51 +01:00
flake.lock tests: try testing with nixtest 2025-05-10 19:48:20 +02:00
flake.nix tests: try testing with nixtest 2025-05-10 19:48:20 +02:00
LICENSE.md chore(LICENSE): use markdown for better readability 2024-10-30 16:11:19 +00:00
README.md docs: write docs & improve tooling 2025-05-02 17:30:53 +02:00

Nix GitLab CI

built with nix pipeline status License: MIT Latest Release Support me Docs

Flake module which allows generating a .gitlab-ci.yml from Nix.

This allows easily using any Nix package in CI.

Also makes it possible to split CI parts in a separate module which can be imported in multiple projects.

Usage

# flake.nix
{
  ...
  inputs.nix-gitlab-ci.url = "gitlab:TECHNOFAB/nix-gitlab-ci/<version>?dir=lib";  # recommendation: pin to the latest release/version
  
  outputs = {...}: flake-parts.lib.mkFlake {...} {
    imports = [
      inputs.nix-gitlab-ci.flakeModule
    ];
    ...
    
    perSystem = {pkgs, ...}: {
      # ci is a shortcut and creates a "default" pipeline
      ci = {
        stages = ["test"];
        jobs = {
          "test" = {
            stage = "test";
            nix.deps = [pkgs.unixtools.ping];
            script = [
              "ping -c 5 8.8.8.8"
            ];
          };
        };
      };
      # runs on a merge request for example
      pipelines."merge_request_event" = {
        stages = ["some_stage"];
        jobs = { ... };
      };
      ...
    }
  }
}
# .gitlab-ci.yml
include:
  - component: gitlab.com/TECHNOFAB/nix-gitlab-ci/nix-gitlab-ci@<version>  # recommendation: pin to the latest release/version (don't use "main" etc.)
    inputs:
      version: <version>  # docker image tag, use the same version as a above

Utilities

Disable Caching temporarily

To disable any of the provided caches for a pipeline one can set NIX_CI_DISABLE_CACHE to anything non-empty (eg. "yes") when triggering the pipeline.

The build:nix-ci job has a different special environment variable NIX_CI_FORCE_BUILD (useful if the generated pipeline in the cache is outdated, this will build it again).

Run Jobs locally

You can run any job's script (+ before and after) locally with Nix for easier testing:

#                                /  pipeline name, like "default"
nix run .#gitlab-ci:pipeline:<pipeline name>:job:<name>

There is also .#gitlab-ci:pipeline:<pipeline name>:job-deps:<name> which generates and exports the required environment variables for each job:

  • PATH (with all deps)
  • any custom env variables which contain store paths to not break stuff when switching archs

Thanks to

Some parts of this implementation are adapted/inspired from https://gitlab.com/Cynerd/gitlab-ci-nix