iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐡

Empirical Comparison of Three Nix Binary Cache Tools in GitHub Actions

に公開

Introduction

When using Nix for CI/CD in GitHub Actions, utilizing binary caches can shorten build times.

Several cache tools exist, each with varying popularity (star counts), making it difficult to decide which tool to choose based solely on popularity.
Furthermore, there are few articles that quantitatively compare the performance of each tool, making it challenging to select the optimal one for your environment.

This article compares three tools based on execution speed (job time, build time) in GitHub Actions:

The comparison was conducted in two types of environments: one with only packages available on the official binary cache (cache.nixos.org), and one including packages not on the official binary cache.

If you'd like to directly check the code used for verification, please see the repository below:

https://github.com/ryuryu333/nix_cache_ci_experiments

Target Audience

  • People using Nix
  • People building CI/CD with GitHub Actions × Nix and considering reducing build times
  • People who want to know the differences between various cache tools and get criteria for choosing one

Conclusion

Basically, using cache-nix-action and optionally comparing it with cachix-action seems like a good approach.

Item cachix-action cache-nix-action magic-nix-cache-action
Storage Destination Cachix GitHub Actions Cache GitHub Actions Cache
Cache Unit Per store path nix/store/<hash><name> One per job Per store path nix/store/<hash><name>
Cache Target Store paths not on cache.nixos.org Almost entire nix/ Store paths not on cache.nixos.org

※ When using default settings

Summary by Environment

Environment with only packages on the official binary cache (cache.nixos.org)

The cache effect was limited. If using one, cache-nix-action is useful (approximately 10% reduction).

marp job time list use cache only

Environment including packages not on the official binary cache (e.g., reproducing Node.js packages with importNpmLock)

The top choice is cache-nix-action, followed by cachix-action (approximately 50% reduction).

zenn job time list use cache only

Article Flow

  • Background
  • Introduction of each tool
  • Verification method
  • Verification results

The article proceeds in this order.

1. Background

1.1 Nix Build and Caching

Nix builds are optimized by caching.
To help understand how cache tools work, here's a brief overview of the build flow.

When you run the nix build command, a build recipe (derivation) is generated based on configurations in flake.nix, etc.
Then, the hash of the build artifact (e.g., treefmt) is calculated.

flake.nix
packages.default = buildEnv {
    name = "example";
    paths = [ pkgs.treefmt ];
};

/nix/store/pnl4n8xandadzdp0jlnbkq4smkldprzs-treefmt-2.3.1

Before building the treefmt binary, Nix checks for the cache of the build artifact in the following order:

  1. Whether the same path exists in the local Nix store
  2. If not, whether it exists in the configured binary cache
  3. If neither, it builds from scratch

The default binary cache is cache.nixos.org.
Most packages from nixpkgs have their cache here, so the build is skipped from the first run, making it fast.

Log example during build
these 464 paths will be fetched (265.13 MiB download, 1391.91 MiB unpacked):
  # ...
  /nix/store/pnl4n8xandadzdp0jlnbkq4smkldprzs-treefmt-2.3.1
  # ...
copying path '/nix/store/pnl4n8xandadzdp0jlnbkq4smkldprzs-treefmt-2.3.1' from 'https://cache.nixos.org'...
# ...

On the other hand, custom-defined packages do not exist in the Nix store or cache.nixos.org, so the cache cannot be utilized on the first run.

Log example during build
these 464 paths will be fetched (265.13 MiB download, 1391.91 MiB unpacked):
# ...
# If on the official binary cache, use cache.nixos.org
copying path '/nix/store/pnl4n8xandadzdp0jlnbkq4smkldprzs-treefmt-2.3.1' from 'https://cache.nixos.org'...
# ...
# If not on the official binary cache, build
building '/nix/store/d314dc7v1hakn4g9824yyrff2gxckg0h-zenn-cli-0.2.3.tgz.drv'...
# ...

1.2 Differences Between Local and GitHub Actions Builds

In a local environment, even for custom-defined packages, subsequent builds are sped up by caching in the local Nix store.

However, in GitHub Actions, a clean environment starts up for each run, so reuse via the local Nix store isn't possible.

In other words, environments that include packages not on the official binary cache require rebuilding every time, leading to longer CI/CD execution times.

1.3 Binary Cache Tools

Nix provides mechanisms to store and reuse build artifacts beyond cache.nixos.org.

This article focuses on Nix builds in GitHub Actions and compares the following cache tools:

  • cachix-action
  • cache-nix-action
  • magic-nix-cache-action

We targeted tools found when searching for "nix cache" that had recognizable names.

1.4 Which Tool Is Best?

Multiple cache tools are available on GitHub, with varying star counts.
We'll present data in the next section, but if judging by star count alone, magic-nix-cache-action is the most popular.

Should we decide based solely on popularity?

I wanted to know which tool is best for my environment (i.e., which can speed up CI/CD). That was the impetus for this investigation.

Using job time and build time as evaluation metrics, I measured the time for the first build and the second build, and quantitatively compared the cache tools.

References

  • General Nix Reference

https://nixos.org/manual/nixpkgs/stable/

  • Cache explanation page (easy to understand)

https://zero-to-nix.com/concepts/caching/

  • attic (self-hostable Nix Binary Cache server)

https://github.com/zhaofengli/attic

  • cachix (binary cache server)

https://docs.cachix.org/getting-started

  • Binary cache explanation articles

https://zenn.dev/asa1984/books/nix-introduction/viewer/07-binary-cache

https://zenn.dev/asa1984/articles/make-a-binary-cache

2. Introduction of Each Tool

Item cachix-action cache-nix-action magic-nix-cache-action
Storage destination Cachix GitHub Actions Cache GitHub Actions Cache
Cache unit Store path unit nix/store/<hash><name> One per job Store path unit nix/store/<hash><name>
Cache target Store paths not on cache.nixos.org Almost entire nix/ Store paths not on cache.nixos.org

※When using default settings

2.1 cachix-action

  • Uses an external binary cache service (Cachix)
  • Cache can be shared locally, across GitHub Actions, between repositories, and between jobs
  • Free tier available
    • However, the cache is public
    • Private is paid
  • Only caches items not on cache.nixos.org

https://app.cachix.org/

https://github.com/cachix/cachix-action

https://zenn.dev/trifolium/articles/fc89526ce652fa

2.2 cache-nix-action

  • Uses GitHub Actions cache
  • Default targets are /nix, ~/.cache/nix, ~root/.cache/nix
  • Stored as a single large file in the GitHub Actions Cache
    • e.g., cache name: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
  • Caches and restores the entire Nix environment (my impression)

https://github.com/nix-community/cache-nix-action

2.3 magic-nix-cache-action

  • Uses GitHub Actions cache
  • Easy to use with no detailed configuration (as per the official repo)
    • Use Magic Nix Cache, a totally free and zero-configuration binary cache for Nix on GitHub Actions.
  • Cache behavior resembles cachix (my impression)

https://github.com/DeterminateSystems/magic-nix-cache-action

2.4 Star counts of each tool

Comparison of GitHub Star counts

https://www.star-history.com/#cachix/cachix-action&nix-community/cache-nix-action&DeterminateSystems/magic-nix-cache-action&type=date&legend=top-left

Looking at the star count trends, magic-nix-cache-action has garnered the most support.

On the other hand, cache-nix-action has seen notable growth since 2025.

This increase is likely due to magic-nix-cache-action being temporarily deprecated around mid-2025, leading many projects to migrate to cache-nix-action.

:details Specific Example: Hyprland
In the Hyprland repository, a PR migrating to cache-nix-action was confirmed in June 2025.

2025/6/20
Describe your PR, what does it fix/add?
After https://github.com/DeterminateSystems/magic-nix-cache-action has been deprecated, we've been running Nix Actions without a cache for /nix/store. This meant that all dependencies had to be downloaded on each action run.

https://github.com/hyprwm/Hyprland/pull/10794

https://github.com/hyprwm/Hyprland/blob/main/.github/workflows/nix.yml

:::

3. Verification Method

3.1 Build Environments

Two types of Nix environments were prepared for verification.

  • marp
    • An environment with only packages that exist in the official binary cache (cache.nixos.org)
    • Uses pkgs.marp, etc.
  • zenn
    • An environment that includes packages not in the official binary cache
    • Uses importNpmLock to reproduce node_modules with Nix

3.1.1 marp - Environment with only packages in the official binary cache

This environment is used to statically create slides from Markdown using Marp CLI.

It uses nixpkgs packages such as pkgs.marp-cli. Therefore, the build can leverage the official binary cache (cache.nixos.org).

flake.nix
{
  description = "Marp environment";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs =
    { nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      rec {
        packages = {
          marp_build = pkgs.buildEnv {
            name = "marp_build_tools";
            paths = with pkgs; [
              marp-cli
              chromium
              noto-fonts-cjk-sans
            ];
          };
          default = packages.marp_build;
        };

        devShells = {
          default = pkgs.mkShell {
            nativeBuildInputs = with pkgs; [
              packages.marp_build
              gh
            ];
          };
        };

        apps = {
          marp = {
            type = "app";
            program = "${pkgs.marp-cli}/bin/marp";
          };
          default = apps.marp;
        };
      }
  );
}

3.1.2 zenn - Environment that includes packages not in the official binary cache

This environment is used to create articles locally using Zenn CLI.

To use Node.js tools, importNpmLock is employed to build node_modules with Nix.

https://github.com/NixOS/nixpkgs/tree/master/pkgs/build-support/node/import-npm-lock

importNpmLock によるビルドは公式バイナリキャッシュ(cache.nixos.org)が効きません。
この点が、marp 環境との明確な違いです。

flake.nix
{
  description = "Zenn CLI environment";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs =
    { nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        npmRoot = ./node-pkgs;
        nodejs = pkgs.nodejs_24;
        inherit (pkgs) importNpmLock;
        npmDeps = importNpmLock.buildNodeModules {
          inherit nodejs npmRoot;
        };
      in
      rec {
        packages = {
          zenn_tools = pkgs.buildEnv {
            name = "zenn_tools";
            paths = with pkgs; [
              treefmt
              lychee
              npmDeps
            ];
          };
          default = packages.zenn_tools;
        };

        devShells = {
          zenn = pkgs.mkShell {
            nativeBuildInputs = [
              packages.zenn_tools
              pkgs.go-task
              importNpmLock.hooks.linkNodeModulesHook
            ];
            inherit npmDeps;
          };
          node = pkgs.mkShell {
            packages = [
              nodejs
              pkgs.npm-check-updates
            ];
          };
          default = devShells.zenn;
        };
      }
    );
}
  • 参考資料

https://zenn.dev/trifolium/articles/5b01a68b80808b

https://zenn.dev/trifolium/articles/6678b0c0fb0d27

3.2 Workflow

3.2.1 Flow Details

Jobs

Using a matrix strategy, 2 build environments x (3 cache tools + no tool) = 8 job patterns were defined.

Trigger

The execution trigger is manual (workflow_dispatch).

Variables specified at trigger time

To compare job times when generating a cache versus when using a cache, a variable generate-cache or use-cache can be specified at runtime.

There is no difference in the processing content; it is used as a prefix for the job name.

Steps

For each job's steps, the sample configurations described in each cache tool's official README.md were followed.

The Nix installation method differs per tool, but we intentionally did not unify them, adopting a policy of following each tool's recommended approach.

Code

build.yml
name: "build"

on:
  workflow_dispatch:
    inputs:
      phase:
        description: "Choose cache phase. Only used for naming the job."
        required: true
        type: choice
        options:
          - generate-cache
          - use-cache

jobs:
  build:
    strategy:
      matrix:
        cache_tool: [none, cachix-action, cache-nix-action, magic-nix-cache-action]
        target_nix_env: [marp, zenn]
    name: build_${{ matrix.target_nix_env }}_cachetool_${{ matrix.cache_tool }}_phase_${{ github.event.inputs.phase }}
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ matrix.target_nix_env }}_${{ matrix.cache_tool }}_${{ github.event.inputs.phase }}_${{ github.ref }}
      cancel-in-progress: true
    defaults:
      run:
        working-directory: ${{ matrix.target_nix_env }}

    steps:
      - uses: actions/checkout@v5

      # === Install nix & setup cache tool ===
      # none
      - name: Install nix
        uses: cachix/install-nix-action@v31
        if: ${{ matrix.cache_tool}} == 'none'

      # cachix Action
      - name: Install nix
        uses: cachix/install-nix-action@v31
        if: ${{ matrix.cache_tool}} == 'cachix-action'

      - name: Setup cache tool
        uses: cachix/cachix-action@v16
        with:
          name: ryuryu333
          authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
        if: ${{ matrix.cache_tool }} == 'cachix-action'

      # cache-nix-action
      - name: Install nix
        uses: nixbuild/nix-quick-install-action@v30
        with:
          nix_conf: |
            keep-env-derivations = true
            keep-outputs = true
        if: ${{ matrix.cache_tool}} == 'cache-nix-action'

      - name: Setup cache tool
        uses: nix-community/cache-nix-action@v6
        with:
          primary-key: nix-${{ matrix.target_nix_env }}-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
          restore-prefixes-first-match: nix-${{ matrix.target_nix_env }}-${{ runner.os }}-
          gc-max-store-size-linux: 1G
          purge: true
          purge-prefixes: nix-${{ runner.os }}-
          purge-created: 0
          purge-last-accessed: 0
          purge-primary-key: never
        if: ${{ matrix.cache_tool }} == 'cache-nix-action'

      # magic-nix-cache-action
      - name: Install nix
        uses: DeterminateSystems/nix-installer-action@v20
        if: ${{ matrix.cache_tool}} == 'magic-nix-cache-action'

      - name: Setup cache tool
        uses: DeterminateSystems/magic-nix-cache-action@v13
        if: ${{ matrix.cache_tool }} == 'magic-nix-cache-action'

      # === Build ===
      - run: nix build

      # === Run test command ===
      - run: nix develop -c zenn --version
        if: ${{ matrix.target_nix_env }} == 'zenn'

      - run: nix run .#marp -- --version
        if: ${{ matrix.target_nix_env }} == 'marp'

3.2.2 Workflow Execution Procedure

The workflow was executed according to the following steps:

  1. Delete all GitHub Actions Cache
    Run gh cache delete --all.

  2. Delete all Cachix cache
    From the Cachix screen, click Caches > ryuryu333 > Settings > Danger Zone > Clear.

  3. Manually run the build workflow
    Select generate-cache when executing.

  4. Wait for all jobs to complete

  5. Manually run the build workflow again
    Select use-cache when executing.

  6. Wait for all jobs to complete

  7. Return to step 1

This sequence was repeated 4 times to verify the difference between cache generation and cache usage.

3.3 Aggregating Job Times

Job times were measured using the GitHub API.
Logs for each job and step were retrieved, and aggregated and graphed using Python.

The code is stored in nix_cache_ci_experiments/reports/main.py.

https://github.com/ryuryu333/nix_cache_ci_experiments/tree/main/reports

The following run_id values were targeted:

run_id run_number note
18753771242 15 1st generate-cache
18754054171 16 1st use-cache
18777714953 21 2nd generate-cache
18777913273 22 2nd use-cache
18778308420 23 3rd generate-cache
18778547830 24 3rd use-cache
18779716921 27 4th generate-cache
18779934804 28 4th use-cache

Please note that the build list page below may also include jobs executed during the trial-and-error process.

https://github.com/ryuryu333/nix_cache_ci_experiments/actions/workflows/build.yml

4. Verification Results

4.1 marp - Environment with only packages available in the official binary cache

4.1.1 Job Time

Cache Tool Time (seconds) vs none
none 36.50 -
cachix-action 42.00 115%
cache-nix-action 31.75 87%
magic-nix-cache-action 54.00 148%

marp job time list use cache only

4.1.2 Build Time

Cache Tool Time (seconds) vs none
none 24.00 -
cachix-action 25.25 105%
cache-nix-action 9.25 39%
magic-nix-cache-action 30.75 128%

All results are from the second build (with cache utilized).

marp build time list

4.1.3 Job Time (Cache Generation vs Cache Usage)

Cache Tool Cache Generation
Job Time (sec)
vs none Cache Usage
Job Time (sec)
vs none
none 36.50 - 36.50 -
cachix-action 42.75 117% 42.00 115%
cache-nix-action 45.50 125% 31.75 87%
magic-nix-cache-action 428.50 1174% 54.00 148%

marp job time list

4.2 zenn - Environment including packages without official binary cache

4.2.1 Job Time

Cache Tool Time (seconds) vs none
none 66.25 -
cachix-action 40.50 61%
cache-nix-action 29.75 45%
magic-nix-cache-action 66.50 100%

zenn job time list use cache only

4.2.2 Build Time

Cache Tool Time (seconds) vs none
none 51.50 -
cachix-action 21.25 41%
cache-nix-action 9.00 17%
magic-nix-cache-action 38.50 75%

All results are from the second build (with cache utilized).

zenn build time list

4.2.3 Job Time (Cache Generation vs Cache Usage)

Cache Tool Cache Generation
Job Time (sec)
vs none Cache Usage
Job Time (sec)
vs none
none 66.25 - 66.25 -
cachix-action 99.00 149% 40.50 61%
cache-nix-action 108.25 163% 29.75 45%
magic-nix-cache-action 377.00 569% 66.50 100%

zenn job time list

4.3 Cache Size

As a supplementary point, we compared the cache sizes generated by each tool.
The target is the cache generated after completing builds for both the marp and zenn environments.

Cache Tool Storage Location Number of Files Size (MB)
cachix-action cachix 287 69
cache-nix-action GitHub Actions Cache 2 1130
magic-nix-cache-action GitHub Actions Cache 2202 1177
For those who want to see detailed data

After clearing all caches, we ran build #33 and checked the usage without deleting.

# After running build #33, with caches not deleted
$ gh api repos/ryuryu333/nix_cache_ci_experiments/actions/cache/usage
{
  "full_name": "ryuryu333/nix_cache_ci_experiments",
  "active_caches_size_in_bytes": 2307766854,
  "active_caches_count": 2202
}

zenn GitHub Actions Cache size

zenn cachix Cache size

zenn cachix Cache size individual

Conclusion

In this verification, we quantitatively compared three cache tools and evaluated how the caching effect changes depending on the environment configuration.

As a result, we found that each tool has different behaviors and strengths, and the optimal solution varies depending on the project's configuration.

Going forward, we plan to primarily use cache-nix-action in operation and also document insights gained from actual projects.

Nix is a field with few Japanese resources, but we hope to gradually increase practical examples.

Discussion