2023-06-26 03:04:46 +00:00
# Magic Nix Cache
2023-05-08 09:48:11 +00:00
2025-01-29 17:44:28 +00:00
> [!WARNING]
2025-02-18 20:05:25 +00:00
> The [Magic Nix Cache will will stop working](https://determinate.systems/posts/magic-nix-cache-free-tier-eol) on **February 1st, 2025** unless you're on [GitHub Enterprise Server](https://github.com/enterprise).
2025-01-29 17:44:28 +00:00
>
> You can upgrade to [FlakeHub Cache](https://flakehub.com/cache) and get **one month free** using the coupon code **`FHC`**.
>
> For more information, read [this blog post](https://determinate.systems/posts/magic-nix-cache-free-tier-eol/).
2023-06-26 03:04:46 +00:00
Save 30-50%+ of CI time without any effort or cost.
Use Magic Nix Cache, a totally free and zero-configuration binary cache for Nix on GitHub Actions.
2023-06-26 17:15:32 +00:00
Add our [GitHub Action][action] after installing Nix, in your workflow, like this:
```yaml
2024-03-09 19:30:48 +00:00
permissions:
contents: read
id-token: write
steps:
2024-11-05 14:48:35 +00:00
- uses: actions/checkout@v4
2024-03-13 13:29:55 +00:00
- uses: DeterminateSystems/nix-installer-action@main
2024-03-09 19:30:48 +00:00
- uses: DeterminateSystems/magic-nix-cache-action@main
- run: nix flake check
2023-06-26 17:15:32 +00:00
```
See [Usage ](#usage ) for a detailed example.
## Why use the Magic Nix Cache?
2023-06-26 19:16:52 +00:00
2023-06-26 16:00:38 +00:00
Magic Nix Cache uses the GitHub Actions [built-in cache][ghacache] to share builds between Workflow runs, and has many advantages over alternatives.
2023-06-26 03:04:46 +00:00
1. Totally free: backed by GitHub Actions' cache, there is no additional service to pay for.
2023-06-26 17:47:11 +00:00
1. Zero configuration: add our action to your workflow.
That's it.
Everything built in your workflow will be cached.
2023-06-26 03:04:46 +00:00
1. No secrets: Forks and pull requests benefit from the cache, too.
1. Secure: Magic Nix Cache follows the [same semantics as the GitHub Actions cache][semantics], and malicious pull requests cannot pollute your project.
1. Private: The cache is stored in the GitHub Actions cache, not with an additional third party.
2023-06-26 19:16:52 +00:00
> **Note:** the Magic Nix Cache doesn't offer a publicly available cache.
2023-06-26 14:56:25 +00:00
> This means the cache is only usable in CI.
2023-06-26 19:16:52 +00:00
> [Zero to Nix][z2n] has an article on binary caching if you want to [share Nix builds][z2ncache] with users outside of CI.
2023-05-08 09:48:11 +00:00
2023-06-26 15:00:45 +00:00
## Usage
Add it to your Linux and macOS GitHub Actions workflows, like this:
```yaml
name: CI
on:
push:
pull_request:
jobs:
check:
runs-on: ubuntu-22.04
2024-03-08 14:41:54 +00:00
permissions:
contents: read
id-token: write
2023-06-26 15:00:45 +00:00
steps:
2024-11-05 14:48:35 +00:00
- uses: actions/checkout@v4
2024-03-13 13:29:55 +00:00
- uses: DeterminateSystems/nix-installer-action@main
2023-06-26 15:00:45 +00:00
- uses: DeterminateSystems/magic-nix-cache-action@main
2023-06-26 15:00:58 +00:00
- run: nix flake check
2023-06-26 15:00:45 +00:00
```
2023-06-26 17:47:11 +00:00
That's it.
Everything built in your workflow will be cached.
## Usage Notes
The GitHub Actions Cache has a rate limit on reads and writes.
Occasionally, large projects or large rebuilds may exceed those rate-limits, and you'll see evidence of that in your logs.
The error looks like this:
```
error: unable to download 'http://127.0.0.1:37515/< ... > ': HTTP error 418
response body:
GitHub API error: API error (429 Too Many Requests): StructuredApiError { message: "Request was blocked due to exceeding usage of resource 'Count' in namespace ''." }
```
2023-06-26 18:49:58 +00:00
The caching daemon and Nix both handle this gracefully, and won't cause your CI to fail.
2023-06-26 17:47:11 +00:00
When the rate limit is exceeded while pulling dependencies, your workflow may perform more builds than usual.
When the rate limit is exceeded while uploading to the cache, the remainder of those store paths will be uploaded on the next run of the workflow.
2023-05-08 09:48:11 +00:00
## Development
2023-06-26 03:04:46 +00:00
This project depends on the GitHub Actions Cache API.
For local development, see `gha-cache/README.md` for more details on how to obtain the required tokens.
2023-05-08 09:48:11 +00:00
2023-06-26 19:16:52 +00:00
```shell
2023-05-08 18:59:57 +00:00
cargo run -- -c creds.json --upstream https://cache.nixos.org
2025-02-19 21:59:56 +00:00
cargo build --release --target x86_64-unknown-linux-gnu
cargo build --release --target aarch64-unknown-linux-gnu
2023-05-08 09:48:11 +00:00
nix copy --to 'http://127.0.0.1:3000' $(which bash)
nix-store --store $PWD/test-root --extra-substituters 'http://localhost:3000' --option require-sigs false -r $(which bash)
```
2023-06-26 03:04:46 +00:00
2023-06-26 17:15:32 +00:00
## Acknowledgement
2023-06-26 19:16:52 +00:00
2023-06-26 17:15:32 +00:00
Magic Nix Cache is a collaboration with [Zhaofeng Li][zhaofeng].
2023-06-26 17:19:33 +00:00
Zhaofeng is a major contributor to the Nix community, having authored [Attic][attic] and [Colmena][colmena].
We'd like to express our deep gratitude to Zhaofeng for his tremendous work on this project.
2023-06-26 17:15:32 +00:00
2023-06-26 03:04:46 +00:00
## Telemetry
The goal of Magic Nix Cache is to help teams save time in CI.
The cache daemon collects a little bit of telemetry information to help us make that true.
Here is a table of the [telemetry data we collect][telemetry]:
2023-06-26 14:56:25 +00:00
| Field | Use |
| -------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| `distinct_id` | An opaque string that represents your project, anonymized by sha256 hashing repository and organization details. |
| `version` | The version of Magic Nix Cache. |
| `is_ci` | Whether the Magic Nix Cache is being used in CI (i.e.: GitHub Actions). |
| `elapsed_seconds` | How long the cache daemon was running. |
| `narinfos_served` | Number of narinfos served from the cache daemon. |
| `narinfos_sent_upstream` | Number of narinfo requests forwarded to the upstream cache. |
| `narinfos_negative_cache_hits` | Effectiveness of an internal data structure which minimizes cache requests. |
| `narinfos_negative_cache_misses` | Effectiveness of an internal data structure which minimizes cache requests. |
| `narinfos_uploaded` | Number of new narinfo files cached during this run. |
| `nars_served` | Number of nars served from the cache daemon. |
| `nars_sent_upstream` | Number of nar requests forwarded to the upstream cache. |
| `nars_uploaded` | Number of nars uploaded during this run. |
| `num_original_paths` | Number of store paths that existed on startup. |
| `num_final_paths` | Number of store paths that existed on shutdown. |
2023-06-26 16:00:38 +00:00
| `num_new_paths` | The difference between `num_original_paths` and `num_final_paths` . |
2023-06-26 03:04:46 +00:00
To disable diagnostic reporting, set the diagnostics URL to an empty string by passing `--diagnostic-endpoint=""` .
2023-06-26 14:58:01 +00:00
You can read the full privacy policy for [Determinate Systems][detsys], the creators of this tool and the [Determinate Nix Installer][installer], [here][privacy].
2023-06-26 03:04:46 +00:00
2023-06-26 17:15:32 +00:00
[detsys]: https://determinate.systems/
[action]: https://github.com/DeterminateSystems/magic-nix-cache-action/
2023-06-26 14:58:01 +00:00
[installer]: https://github.com/DeterminateSystems/nix-installer/
2023-06-26 03:04:46 +00:00
[ghacache]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows
2024-02-25 20:25:09 +00:00
[privacy]: https://determinate.systems/policies/privacy
2023-06-26 17:15:32 +00:00
[telemetry]: https://github.com/DeterminateSystems/magic-nix-cache/blob/main/magic-nix-cache/src/telemetry.rs
2023-06-26 03:04:46 +00:00
[semantics]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
[z2ncache]: https://zero-to-nix.com/concepts/caching#binary-caches
2023-06-26 17:15:32 +00:00
[zhaofeng]: https://github.com/zhaofengli/
[attic]: https://github.com/zhaofengli/attic
[colmena]: https://github.com/zhaofengli/colmena
2023-06-26 19:16:52 +00:00
[z2n]: https://zero-to-nix.com