Skip to content

Commit 4c60cda

Browse files
authored
Merge pull request #2187 from o1-labs/brian/nix-cache
Push to the nix cache in ci
2 parents 4f567f0 + 24b2dc0 commit 4c60cda

File tree

8 files changed

+118
-23
lines changed

8 files changed

+118
-23
lines changed

.github/actions/build/action.yml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
name: Build o1js
22
description: 'All of the building steps for o1js'
33

4-
54
permissions:
65
contents: write
76

7+
inputs:
8+
cache_id:
9+
required: true
10+
nar_secret:
11+
required: true
12+
gcp_secret:
13+
required: true
14+
815
runs:
916
using: "composite"
1017
steps:
11-
- name: Set up Nix
18+
- name: Install zstd if needed
1219
shell: bash
13-
run: echo "PATH=$PATH:/nix/var/nix/profiles/default/bin" >> $GITHUB_ENV
20+
run: command -v unzstd || (sudo apt-get update && sudo apt-get install -y zstd)
21+
- uses: nixbuild/nix-quick-install-action@v30
22+
continue-on-error: true
23+
with:
24+
nix_conf: |
25+
keep-env-derivations = true
26+
keep-outputs = true
27+
- name: Fallback to persistant nix
28+
# only available on some runners
29+
shell: bash
30+
run: command -v nix || echo "PATH=$PATH:/nix/var/nix/profiles/default/bin" >> $GITHUB_ENV
31+
1432
- name: Disable smudging
1533
shell: bash
1634
run: echo "GIT_LFS_SKIP_SMUDGE=1" >> $GITHUB_ENV
@@ -30,11 +48,20 @@ runs:
3048

3149
- name: Build the o1js bindings
3250
if: steps.bindings-cache.outputs.cache-hit != 'true'
51+
env:
52+
AWS_ACCESS_KEY_ID: ${{ inputs.cache_id }}
53+
AWS_SECRET_ACCESS_KEY: ${{ inputs.gcp_secret }}
3354
shell: bash
3455
run: |
3556
set -Eeu
3657
./pin.sh
58+
nix build o1js#bindings --accept-flake-config
59+
temp_key=$(mktemp)
60+
echo ${{ inputs.nar_secret }} > "$temp_key"
61+
nix store sign --key-file "$temp_key" --recursive ./result
62+
nix copy --to "s3://mina-nix-cache?endpoint=https://storage.googleapis.com" $(nix path-info ./result)
3763
nix run o1js#generate-bindings --max-jobs auto
64+
rm "$temp_key"
3865
3966
- name: Cache dependencies and build
4067
uses: actions/cache@v4

.github/actions/upload/action.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ on:
33
workflow_dispatch:
44
workflow_call:
55

6+
inputs:
7+
cache_id:
8+
required: true
9+
nar_secret:
10+
required: true
11+
gcp_secret:
12+
required: true
13+
614
runs:
715
using: "composite"
816
steps:
917
- name: Build
1018
uses: ./.github/actions/build
19+
with:
20+
cache_id: ${{ inputs.cache_id }}
21+
nar_secret: ${{ inputs.nar_secret }}
22+
gcp_secret: ${{ inputs.gcp_secret }}
23+
1124
- name: generate tar
1225
shell: bash
1326
run: |

.github/workflows/checks.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ permissions:
1818

1919
jobs:
2020
Prepare:
21-
runs-on: [sdk-self-hosted-linux-amd64-build-system]
21+
runs-on: self-hosted
2222
steps:
2323
- name: Checkout repository with submodules
2424
uses: actions/checkout@v4
2525
with:
2626
submodules: recursive
2727
- name: Build
2828
uses: ./.github/actions/build
29+
with:
30+
cache_id: ${{ secrets.NIX_CACHE_GCP_ID }}
31+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
32+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
2933

3034
Lint-Format-and-TypoCheck:
3135
strategy:
@@ -68,7 +72,7 @@ jobs:
6872
- name: Run Oxlint
6973
if: steps.get_changed_files.outputs.files_changed == 'true'
7074
run: xargs npm run lint:strict < changed_files.txt
71-
75+
7276
- name: Run codespell
7377
if: steps.get_changed_files.outputs.files_changed == 'true' && github.event.pull_request.labels.*.name != 'no-typo-check'
7478
uses: codespell-project/actions-codespell@master
@@ -82,13 +86,17 @@ jobs:
8286
Upload-bindings:
8387
name: upload bindings artifact
8488
needs: [Prepare]
85-
runs-on: [sdk-self-hosted-linux-amd64-build-system]
89+
runs-on: self-hosted
8690
steps:
8791
- uses: actions/checkout@v4
8892
with:
8993
submodules: recursive
9094
- name: upload
9195
uses: ./.github/actions/upload
96+
with:
97+
cache_id: ${{ secrets.NIX_CACHE_ID }}
98+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
99+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
92100

93101
Build-And-Test-Server:
94102
needs: [Prepare]
@@ -116,6 +124,10 @@ jobs:
116124

117125
- name: build
118126
uses: ./.github/actions/build
127+
with:
128+
cache_id: ${{ secrets.NIX_CACHE_ID }}
129+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
130+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
119131
- name: Prepare for tests
120132
run: touch profiling.md
121133
- name: Execute tests
@@ -144,6 +156,10 @@ jobs:
144156
submodules: recursive
145157
- name: build
146158
uses: ./.github/actions/build
159+
with:
160+
cache_id: ${{ secrets.NIX_CACHE_ID }}
161+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
162+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
147163
- name: Count tests
148164
id: count_tests
149165
run: |
@@ -277,6 +293,10 @@ jobs:
277293
uses: actions/checkout@v4
278294
- name: build
279295
uses: ./.github/actions/build
296+
with:
297+
cache_id: ${{ secrets.NIX_CACHE_ID }}
298+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
299+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
280300
- name: Use shared steps for live testing jobs
281301
uses: ./.github/actions/live-tests-shared
282302
with:
@@ -305,6 +325,10 @@ jobs:
305325
uses: actions/checkout@v4
306326
- name: build
307327
uses: ./.github/actions/build
328+
with:
329+
cache_id: ${{ secrets.NIX_CACHE_ID }}
330+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
331+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
308332
- name: Use shared steps for live testing jobs
309333
uses: ./.github/actions/live-tests-shared
310334
with:

.github/workflows/push_main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ jobs:
8585
node-version: ${{ matrix.node }}
8686
- name: build
8787
uses: ./.github/actions/build
88+
with:
89+
cache_id: ${{ secrets.NIX_CACHE_ID }}
90+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
91+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
8892
- name: Build o1js and mina-signer
8993
run: |
9094
npm run prepublishOnly
@@ -116,6 +120,10 @@ jobs:
116120

117121
- name: build
118122
uses: ./.github/actions/build
123+
with:
124+
cache_id: ${{ secrets.NIX_CACHE_ID }}
125+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
126+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
119127
- name: add build to gc-root if on main
120128
run: |
121129
nix build o1js#o1js-bindings --out-link /home/app/actions-runner/nix-cache/main-bindings-gcroot

.github/workflows/remote_bindings.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ jobs:
1313
submodules: recursive
1414
- name: upload
1515
uses: ./.github/actions/upload
16+
with:
17+
cache_id: ${{ secrets.NIX_CACHE_ID }}
18+
nar_secret: ${{ secrets.NIX_CACHE_NAR_SECRET }}
19+
gcp_secret: ${{ secrets.NIX_CACHE_GCP_SECRET }}
1620

README-nix.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ with all the dependencies required executing `nix develop o1js#default`.
8888
nix develop o1js#default
8989
```
9090
91-
The first time you run this command, you can expect it to take hours (or even a full day) to complete. Then, you will observe that the current devshell becomes a Nix shell with the right
91+
On macos the first time you run this command, you can expect it to take hours (or even a full day) to complete, due to the lack of cached builds.
92+
Then, you will observe that the current devshell becomes a Nix shell with the right
9293
configuration for `o1js` and `mina`.
9394
95+
9496
From within the shell, you can build o1js and update the bindings.
9597
9698
```console
@@ -114,28 +116,31 @@ nix develop mina
114116
115117
Using Nix can take up a lot of disk space if not optimized. Every time you run `nix develop {SOMETHING}`, Nix will create new generations taking gigabytes of data instead of replacing the old ones. This can soon become a problem in your hard disk if you don't handle it carefully. Here are a few indications that can help with this.
116118
117-
Nix has a garbage collector that **is not used by default** after every run. Instead, artifacts get accumulated in your disk unless configured otherwise. But if the full gargabe collector is executed (`nix-store --gc`), it will get the dependencies removed completely, and you can expect that the next time executing the Nix build will take hours to complete.
118-
119-
Instead, you can try to run `nix-env --delete-generations old` or any other time bound like `7d`. This will not have any effect on MacOS though. Alternatively, the [direnv](https://github.com/direnv/direnv) / [nix-direnv](https://github.com/nix-community/nix-direnv) tool can create garbage collector roots that won't be collected for removal. It just keeps one gc-root to the latest build of the dev shell so that `nix-store --gc` only removes older generations.
119+
Nix has a garbage collector that **is not used by default** after every run. Instead, artifacts get accumulated in your disk unless configured otherwise.
120+
This is why we recomend `auto-optimise-store = true` (you will be prompted to accept this). You can also run `nix-store --optimize` retroactively.
120121
121-
On top of that, adding `auto-optimise-store = true` to `/etc/nix/nix.conf` and running `nix-store --optimize` should help with disk usage, as it replaces duplicated files with symlinks.
122+
If you still need to free up space you can run `nix-store --gc`, unfortunately this can slow down futurue nix builds by forcing you to rebuild dependencies.
123+
This can be mitigated with [direnv](https://github.com/direnv/direnv) and [nix-direnv](https://github.com/nix-community/nix-direnv) which can create garbage collector roots,
124+
keeping one gc-root to the latest build of the dev shell so that `nix-store --gc` won't remove it.
125+
You can also create a gc root any time you run `nix build` (until you remove `./result`) so running `nix build o1js#bindings` before `nix-store --gc` may also help.
122126
123127
### Runtime optimization
124128
125-
Other configurations are worth adding into your `/etc/nix/nix.conf`:
126-
127-
```bash
128-
keep-outputs = true
129-
max-jobs = 20
130-
extra-substituters = https://storage.googleapis.com/mina-nix-cache
131-
extra-trusted-public-keys = nix-cache.minaprotocol.org:fdcuDzmnM0Kbf7yU4yywBuUEJWClySc1WIF6t6Mm8h4= nix-cache.minaprotocol.org:D3B1W+V7ND1Fmfii8EhbAbF1JXoe2Ct4N34OKChwk2c= mina-nix-cache-1:djtioLfv2oxuK2lqPUgmZbf8bY8sK/BnYZCU2iU5Q10=
132-
```
129+
We suggest a few settings in `flake.nix`.
130+
You will be prompted to accept or reject these the first time you use nix in this repo.
131+
You can also use `--accept-flake-config` to accept all of them.
133132
134-
The first of those flags tells the garbage collector to keep build time dependencies of current gc-roots, which should help reduce the amount of data that gets removed and rebuilt.
133+
`max-jobs = auto`
134+
For some reason the default is `1`.
135135
136-
The second flag increases the default number of jobs being 1, so that rebuilding from scratch will take shorter time.
136+
`auto-optimize-store = true;`
137+
When building slightly different versions of the same repo your nix store can fill up with coppies of the same files.
138+
This saves space by replacing them with symlinks.
137139
138-
The last two lines tell Nix to use the Mina Foundation's cache whenever possible, which should as well speed things up when building code that has been build in Mina's CI before.
140+
`substituters = ...`
141+
`trusted-public-keys = ...`
142+
These make sure you are using the mina-nix-cache which will save time by downloading any derivations already available.
143+
Anything built in CI is added to this nix-cache, so it should make a big difference in build times.
139144
140145
## Common Issues
141146

flake.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
dune-nix.inputs.flake-utils.follows = "flake-utils";
1515
flake-utils.url = "github:numtide/flake-utils";
1616
};
17+
nixConfig = {
18+
auto-optimize-store = true;
19+
max-jobs = "auto";
20+
#coppied from flake.nix in mina
21+
allow-import-from-derivation = "true";
22+
substituters =
23+
[ "https://storage.googleapis.com/mina-nix-cache"
24+
];
25+
trusted-public-keys =
26+
[ "mina-nix-cache-1:djtioLfv2oxuK2lqPUgmZbf8bY8sK/BnYZCU2iU5Q10="
27+
"nix-cache.minaprotocol.org:fdcuDzmnM0Kbf7yU4yywBuUEJWClySc1WIF6t6Mm8h4="
28+
"nix-cache.minaprotocol.org:D3B1W+V7ND1Fmfii8EhbAbF1JXoe2Ct4N34OKChwk2c="
29+
];
30+
};
1731
outputs = { self, nixpkgs-mina, nixpkgs-newer, flake-utils, ... }@inputs:
1832
flake-utils.lib.eachDefaultSystem (system:
1933
let

0 commit comments

Comments
 (0)