You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+94-31Lines changed: 94 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,36 +6,53 @@ This action is based on [actions/cache](https://github.com/actions/cache).
6
6
7
7
## Approach
8
8
9
-
The [nix-quick-install-action](https://github.com/nixbuild/nix-quick-install-action) action makes `/nix/store` owned by an unpriviliged user.
10
-
That's why, the `cache-nix-action`action can restore and save`/nix`.
9
+
1.The [nix-quick-install-action](https://github.com/nixbuild/nix-quick-install-action) action makes `/nix/store` owned by an unpriviliged user.
10
+
1.`cache-nix-action`restores`/nix`.
11
11
12
-
When there is a cache hit, restoring `/nix/store` from a cache is faster than downloading multiple paths from binary caches (see [ci.yaml](.github/workflows/ci.yaml) and related [Actions](https://github.com/nix-community/cache-nix-action/actions/workflows/ci.yaml)).
12
+
* When there's a cache hit, restoring `/nix/store` from a cache is faster than downloading multiple paths from binary caches.
13
+
* You can compare run times of jobs with and without store caching in [Actions](https://github.com/nix-community/cache-nix-action/actions/workflows/ci.yaml).
14
+
* Open a run and click on the time under `Total duration`.
13
15
14
-
The [Approaches](#approaches) section compares this approach with other caching approaches.
16
+
1. Optionally, `cache-nix-action` purges old caches.
17
+
* As Nix (flake) inputs may change, it's necessary to use fresher caches.
18
+
* Caches can be purged by `created` or `last accessed` time (see [Configuration](#configuration)).
19
+
20
+
1. Optionally, `cache-nix-action` collects garbage in the Nix store (see [Garbage Collection](#garbage-collection)).
21
+
* The store may contain useless paths from previous runs.
22
+
* This action allows to limit nix store size (see [Configuration](#configuration)).
23
+
24
+
1.`cache-nix-action` saves a new cache.
15
25
16
26
## Limitations
17
27
18
-
*Restores and saves a full`/nix` directory.
19
-
*Requires`nix-quick-install-action` (see [Approach](#approach)).
20
-
* Store size is limited by a runner storage size ([lnk](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources)).
28
+
*`cache-nix-action` restores and saves the whole`/nix` directory.
29
+
*`cache-nix-action` requires`nix-quick-install-action` (see [Approach](#approach)).
30
+
* Store size is limited by a runner storage size ([link](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources)).
21
31
* Caches are isolated between branches ([link](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache)).
22
-
* When restoring, this action writes cached Nix store paths into a read-only `/nix/store` of a runner. Some of these paths may already be present, so this action will show `File exists` errors and a warning that it failed to restore. It's OK.
23
-
* May be necessary to purge old caches (see [Example workflow](#example-workflow)).
32
+
* When restoring, `cache-nix-action` writes cached Nix store paths into a read-only `/nix/store` of a runner.
33
+
Some of these paths may already be present, so the action will show `File exists` errors and a warning that it failed to restore.
34
+
It's OK.
35
+
* It may be necessary to purge old caches (see [Example workflow](#example-workflow)) and [New inputs](#new-inputs).
24
36
25
37
## Configuration
26
38
27
39
See [action.yaml](action.yaml).
28
40
29
41
This action inherits some [inputs](#inputs) and [outputs](#outputs) of `actions/cache`.
42
+
Other inputs are new or override the inherited inputs.
On `macOS` runners, when `macos-gc-enabled` is `true`, when a cache size is greater than `macos-max-cache-size`, this action will run `nix store gc --max R` before saving a cache.
48
-
Here, `R` is `max(0, S - macos-max-store-size)`, where `S` is the current store size.
64
+
On `macOS` runners, when `gc-enabled-macos` is `true`, when a cache size is greater than `gc-max-cache-size-macos`, this action will run `nix store gc --max R` before saving a cache.
65
+
Here, `R` is `max(0, S - gc-max-store-size-macos)`, where `S` is the current store size.
49
66
Respective conditions hold for `Linux` runners.
50
67
51
-
Tip: if you need to save a path from caching, `nix profile install` it (see [Example workflow](#example-workflow))
52
-
53
68
There are alternative approaches to garbage collection (see [Garbage collection](#garbage-collection)).
54
69
55
70
## Usage
56
71
57
72
* This action **must** be used with [nix-quick-install-action](https://github.com/nixbuild/nix-quick-install-action).
58
-
* Maximum Nix store size on `Linux` runners will be `512MB` due to `linux-max-store-size: 536870912`.
73
+
* Maximum Nix store size on `Linux` runners will be `512MB` due to `gc-max-store-size-linux: 536870912`.
59
74
* If the store has a larger size, it will be garbage collected to reach this limit (See [Garbage collection parameters](#garbage-collection-parameters)).
60
75
* Maximum Nix store size on `macOS` runners will be limited by the runner storage size.
61
-
* Stores won't be garbage collected since `macos-gc-enabled: true` isn't set.
76
+
* Stores won't be garbage collected since `gc-enabled-macos: true` isn't set.
62
77
63
78
```yaml
64
79
- uses: nixbuild/nix-quick-install-action@v25
@@ -69,13 +84,19 @@ There are alternative approaches to garbage collection (see [Garbage collection]
Discussed in more details [here](https://github.com/DeterminateSystems/magic-nix-cache-action/issues/16) and [here](https://github.com/nixbuild/nix-quick-install-action/issues/33).
@@ -109,7 +126,7 @@ Pros:
109
126
* Free.
110
127
* Uses `GitHub Actions Cache` and works fast.
111
128
* Easy to set up.
112
-
* Allows to save a store of at most a given size (see [Garbage collection](#garbage-collection-parameters)).
129
+
* Allows to save a store of at most a given size (see [Garbage collection parameters](#garbage-collection-parameters)).
113
130
* Allows to save output from garbage collection (see [Garbage collection](#garbage-collection)).
114
131
115
132
Cons: see [Limitations](#limitations)
@@ -167,6 +184,52 @@ Cons:
167
184
* Need good bandwidth for receiving and pushing paths over the Internet.
168
185
* Can be down.
169
186
187
+
## Garbage collection
188
+
189
+
When restoring a Nix store from a cache, the store may contain old unnecessary paths.
190
+
These paths should be removed sometimes to limit cache size and ensure the fastest restore/save steps.
191
+
192
+
### GC approach 1
193
+
194
+
Produce a cache once, use it multiple times. Don't collect garbage.
195
+
196
+
Advantages:
197
+
198
+
* Unnecessary paths are saved to a cache only during a new save.
199
+
200
+
Disadvantages:
201
+
202
+
* Unnecessary paths can accumulate between new saves.
203
+
* A job at the firs run produces a path `A` and saves a cache.
204
+
* The job at the second run restores the cache, produces a path `B`, and saves a cache. The cache has both `A` and `B`.
205
+
* etc.
206
+
207
+
### GC approach 2
208
+
209
+
Collect garbage before saving a cache.
210
+
211
+
Advantages:
212
+
213
+
* Automatically keep cache at a minimal/limited size
214
+
215
+
Disadvantages:
216
+
217
+
* No standard way to gc only old paths.
218
+
219
+
### Save a path from GC
220
+
221
+
* Use `nix profile install` to save installables from garbage collection.
222
+
* Get store paths of `inputs` via `nix flake archive` (see [comment](https://github.com/NixOS/nix/issues/4250#issuecomment-1146878407)).
223
+
* Get outputs via `nix flake show --json | jq '.packages."x86_64-linux"|keys[]'| xargs -I {}` on `x86_64-linux` (see this [issue](https://github.com/NixOS/nix/issues/7165)).
224
+
* Keep inputs (see this [issue](https://github.com/NixOS/nix/issues/4250) and this [issue](https://github.com/NixOS/nix/issues/6895)).
225
+
* Start [direnv](https://github.com/nix-community/nix-direnv) in background.
226
+
227
+
### Garbage collection approaches
228
+
229
+
* Use [nix-heuristic-gc](https://github.com/risicle/nix-heuristic-gc) for cache eviction via `atime`
230
+
* gc via gc roots [nix-cache-cut](https://github.com/astro/nix-cache-cut)
231
+
* gc based on time [cache-gc](https://github.com/lheckemann/cache-gc)
232
+
170
233
# Cache action
171
234
172
235
## !!! This documentation was inherited from actions/cache and may be partially irrelevant to cache-nix-action
0 commit comments