# Optimizing Nix Flakes & Binary Caches on Self-Hosted GitLab Runners

> Engineering Guide by Wake ([wakeci.dev](https://wakeci.dev/)) · Updated November 22, 2026

**Direct answer:** Most slow Nix GitLab pipelines suffer from two measurable leaks: ephemeral Docker runners rebuilding uncached derivations from scratch (low binary-cache hit rate), and unrelated monorepo edits triggering root `flake.nix` builds. Measuring runner trace phases alongside Nix build logs isolates cache misses from evaluation overhead, while deterministic flake graph pruning skips unaffected Nix jobs completely.

## Why Self-Hosted Nix CI Feels Slow (Post-Garnix)

After hosted Nix CI services like Garnix shut down in July 2026, robotics, defense, and deep-tech teams running Nix monorepos consolidated onto self-hosted GitLab Runners. By default, GitLab Docker executors destroy `/nix/store` after every job, turning a 5-second incremental build into multi-minute store fetches or source rebuilds on every push.

## Step 1: Separate Runner Prep Time from Nix Compilation

GitLab's UI displays one combined job duration. In our self-hosted GitLab CE benchmark, `build-nix-env` took **103 seconds**, of which **88 seconds was image pull and executor prep** and only 15 seconds was `step_script`.

`wake xray` parses both GitLab Runner trace sections (`prepare_executor`, `get_sources`, `restore_cache`, `step_script`) and Nix build output (`these N derivations will be built` vs. `these M paths will be fetched`) to compute the true binary-cache hit rate (`WAKE-010`):

```bash
docker run --rm -v "$PWD:/out" -e GITLAB_TOKEN \
  ghcr.io/strange-metal/wake:latest \
  wake xray --gitlab-url https://git.your-vpc.internal \
  --project robotics/monorepo --html /out/nix-xray.html
```

- **Binary-cache hit rate below 70%:** Point runners at a VPC-local binary cache (Attic, Harmonia, or S3-backed store) and push outputs via post-build hook on default branches.
- **High `prepare_executor` time:** Set runner `pull_policy = if-not-present` and mount a persistent Nix store or daemon socket where isolation rules allow.

## Step 2: Trace `flake.nix` and `callPackage` Deterministically

In polyglot monorepos combining Nix with Rust, Python, Go, and TypeScript, a frontend edit should never trigger `nix build .#firmware`. Wake parses:

- Root and sub-directory `flake.nix` files, including local `path:` flake inputs.
- `callPackage ./packages/...` references and `self.packages` outputs in `default.nix` and `flake.nix`.
- Cross-ecosystem edges between Nix devShells/packages and `Cargo.toml`, `pyproject.toml`, `go.mod`, and `package.json`.

Combined with GitLab [Dynamic Child Pipelines](https://wakeci.dev/guides/gitlab-dynamic-child-pipelines.md), Nix derivations build only when their source files, local flake inputs, or `flake.lock` actually change, while default branches always run the full Nix check matrix.

---

- HTML version: https://wakeci.dev/guides/nix-gitlab-ci-optimization
- Live X-Ray flamegraph: https://wakeci.dev/demo/xray-baseline.html
- Contact: chris@wakeci.dev
