magic-nix-cache-action/README.md

126 lines
8.1 KiB
Markdown
Raw Normal View History

2023-06-26 21:23:57 +02:00
# Magic Nix Cache
2023-06-23 18:49:10 +02:00
2023-06-26 21:23:57 +02: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 21:23:57 +02:00
Add our [GitHub Action][action] after installing Nix, in your workflow, like this:
```yaml
- uses: DeterminateSystems/magic-nix-cache-action@main
```
See [Usage](#usage) for a detailed example.
## Why use the Magic Nix Cache?
Magic Nix Cache uses the GitHub Actions [built-in cache][ghacache] to share builds between Workflow runs, and has many advantages over alternatives.
1. Totally free: backed by GitHub Actions' cache, there is no additional service to pay for.
1. Zero configuration: add our action to your workflow.
That's it.
Everything built in your workflow will be cached.
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.
> **Note:** the Magic Nix Cache doesn't offer a publicly available cache.
> This means the cache is only usable in CI.
> [Zero to Nix][z2n] has an article on binary caching if you want to [share Nix builds][z2ncache] with users outside of CI.
## Usage
Add it to your Linux and macOS GitHub Actions workflows, like this:
```yaml
2023-06-26 21:23:57 +02:00
name: CI
on:
push:
2023-06-26 21:23:57 +02:00
pull_request:
jobs:
2023-06-26 21:23:57 +02:00
check:
runs-on: ubuntu-22.04
2024-03-13 14:28:02 +01:00
permissions:
id-token: "write"
contents: "read"
steps:
2024-04-18 19:12:29 +02:00
- uses: actions/checkout@v4
2023-06-26 21:23:57 +02:00
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
2023-08-31 16:05:41 +02:00
- uses: DeterminateSystems/flake-checker-action@main
- name: Run `nix build`
run: nix build .
2023-06-26 21:23:57 +02: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 21:23:57 +02:00
The caching daemon and Nix both handle this gracefully, and won't cause your CI to fail.
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-06-28 23:08:52 +02:00
## Concepts
2023-06-26 21:23:57 +02:00
2023-06-28 23:08:52 +02:00
### Upstream cache
2024-03-13 14:28:02 +01:00
When you configure an upstream cache for the Magic Nix Cache, any store paths fetched from that source are _not_ cached because they are known to be fetchable on future workflow runs.
2023-06-28 23:08:52 +02:00
The default is `https://cache.nixos.org` but you can set a different upstream:
```yaml
- uses: DeterminateSystems/magic-nix-cache-action@main
with:
2023-06-28 23:11:59 +02:00
upstream-cache: https://my-binary-cache.com
2023-06-28 23:08:52 +02:00
```
## Action Options
2024-03-13 14:28:02 +01:00
2023-06-26 21:23:57 +02:00
<!--
cat action.yml| nix run nixpkgs#yq-go -- '[[ "Parameter", "Description", "Required", "Default" ], ["-", "-", "-", "-"]] + [.inputs | to_entries | sort_by(.key) | .[] | ["`" + .key + "`", .value.description, .value.required // "", .value.default // ""]] | map(join(" | ")) | .[] | "| " + . + " |"' -r
-->
2024-03-13 14:28:02 +01:00
| Parameter | Description | Required | Default |
| --------------------------- | --------------------------------------------------------------------------------------------------------------- | -------- | -------------------------------------------------------- |
| `diagnostic-endpoint` | Diagnostic endpoint url where diagnostics and performance data is sent. To disable set this to an empty string. | | https://install.determinate.systems/magic-nix-cache/perf |
2024-05-22 18:39:53 +02:00
| `diff-store` | Whether or not to diff the store before and after Magic Nix Cache runs. | | `false` |
2024-03-13 14:28:02 +01:00
| `flakehub-api-server` | The FlakeHub API server. | | https://api.flakehub.com |
| `flakehub-cache-server` | The FlakeHub binary cache server. | | https://cache.flakehub.com |
| `flakehub-flake-name` | The name of your flake on FlakeHub. The empty string will autodetect your FlakeHub flake. | | `""` |
2024-03-13 14:28:02 +01:00
| `listen` | The host and port to listen on. | | 127.0.0.1:37515 |
| `source-binary` | Run a version of the cache binary from somewhere already on disk. Conflicts with all other `source-*` options. | | |
| `source-branch` | The branch of `magic-nix-cache` to use. Conflicts with all other `source-*` options. | | main |
| `source-pr` | The PR of `magic-nix-cache` to use. Conflicts with all other `source-*` options. | | |
| `source-revision` | The revision of `nix-magic-nix-cache` to use. Conflicts with all other `source-*` options. | | |
| `source-tag` | The tag of `magic-nix-cache` to use. Conflicts with all other `source-*` options. | | |
| `source-url` | A URL pointing to a `magic-nix-cache` binary. Overrides all other `source-*` options. | | |
| `startup-notification-port` | The port magic-nix-cache uses for daemon startup notification. | | 41239 |
| `upstream-cache` | Your preferred upstream cache. Store paths in this store will not be cached in GitHub Actions' cache. | | https://cache.nixos.org |
| `use-flakehub` | Whether to upload build results to FlakeHub Cache (private beta). | | true |
| `use-gha-cache` | Whether to upload build results to the GitHub Actions cache. | | true |
2023-06-26 21:47:39 +02:00
2023-06-28 23:08:52 +02:00
[gha-cache]: https://docs.github.com/en/rest/actions/cache
2023-06-26 21:47:39 +02:00
[detsys]: https://determinate.systems/
[action]: https://github.com/DeterminateSystems/magic-nix-cache-action/
[installer]: https://github.com/DeterminateSystems/nix-installer/
[ghacache]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows
2024-02-25 21:26:17 +01:00
[privacy]: https://determinate.systems/policies/privacy
2023-06-26 21:47:39 +02:00
[telemetry]: https://github.com/DeterminateSystems/magic-nix-cache/blob/main/magic-nix-cache/src/telemetry.rs
[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
[zhaofeng]: https://github.com/zhaofengli/
[attic]: https://github.com/zhaofengli/attic
[colmena]: https://github.com/zhaofengli/colmena
[z2n]: https://zero-to-nix.com