Skip to content

Commit 68b2de3

Browse files
authored
[nix] Add package for official golang binaries (#3824)
1 parent e207817 commit 68b2de3

File tree

10 files changed

+109
-17
lines changed

10 files changed

+109
-17
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/flake.nix @joshua-kim @maru-ava
2323
/network/p2p/ @joshua-kim
2424
/network/p2p/*.md @joshua-kim @meaghanfitzgerald
25+
/nix/* @joshua-kim @maru-ava
2526
/scripts/ @joshua-kim @maru-ava
2627
/scripts/*.md @joshua-kim @maru-ava @meaghanfitzgerald
2728
/scripts/benchmark_cchain_range.sh @aaronbuchwald

.github/actions/c-chain-reexecution-benchmark/action.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ inputs:
6464
runs:
6565
using: composite
6666
steps:
67-
- uses: ./.github/actions/setup-go-for-project
6867
- name: Set task env
6968
shell: bash
7069
run: |
@@ -113,8 +112,6 @@ runs:
113112
github-token: ${{ inputs.github-token }}
114113
auto-push: ${{ inputs.push-github-action-benchmark }}
115114

116-
- uses: ./.github/actions/install-nix
117-
if: ${{ inputs.push-post-state != '' }}
118115
- name: Push Post-State to S3 (if not exists)
119116
if: ${{ inputs.push-post-state != '' }}
120117
shell: nix develop --command bash -x {0}

.github/actions/install-nix/action.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,23 @@ runs:
1515
github_access_token: ${{ inputs.github_token }}
1616
- run: nix develop --command echo "dependencies installed"
1717
shell: bash
18+
# Cache Go modules (architecture-independent)
19+
- uses: actions/cache@v4
20+
id: go-mod-cache
21+
with:
22+
path: ~/go/pkg/mod
23+
key: ${{ runner.os }}-go-mod-${{ hashFiles('go.sum', 'tools/go.sum') }}
24+
restore-keys: ${{ runner.os }}-go-mod-
25+
# Cache Go build cache (architecture-specific)
26+
- uses: actions/cache@v4
27+
with:
28+
path: ~/.cache/go-build
29+
key: ${{ runner.os }}-${{ runner.arch }}-go-build-${{ hashFiles('go.sum', 'tools/go.sum') }}
30+
restore-keys: ${{ runner.os }}-${{ runner.arch }}-go-build-
31+
# Download modules only on cache miss
32+
- run: nix develop --command go mod download
33+
if: steps.go-mod-cache.outputs.cache-hit != 'true'
34+
shell: bash
35+
- run: nix develop --command go mod download -modfile=tools/go.mod
36+
if: steps.go-mod-cache.outputs.cache-hit != 'true'
37+
shell: bash

.github/actions/run-monitored-tmpnet-cmd/action.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,33 @@ runs:
5959
# - Avoid using the install-nix custom action since a relative
6060
# path wouldn't be resolveable from other repos and an absolute
6161
# path would require setting a version.
62+
# - TODO(marun) Switch to a relative reference to the install-nix
63+
# custom action if/when no external repos use this action.
6264
- uses: cachix/install-nix-action@02a151ada4993995686f9ed4f1be7cfbb229e56f #v31
6365
with:
6466
github_access_token: ${{ inputs.github_token }}
6567
- run: $GITHUB_ACTION_PATH/nix-develop.sh --command echo "dependencies installed"
6668
shell: bash
69+
# Cache Go modules (architecture-independent)
70+
- uses: actions/cache@v4
71+
id: go-mod-cache
72+
with:
73+
path: ~/go/pkg/mod
74+
key: ${{ runner.os }}-go-mod-${{ hashFiles('go.sum', 'tools/go.sum') }}
75+
restore-keys: ${{ runner.os }}-go-mod-
76+
# Cache Go build cache (architecture-specific)
77+
- uses: actions/cache@v4
78+
with:
79+
path: ~/.cache/go-build
80+
key: ${{ runner.os }}-${{ runner.arch }}-go-build-${{ hashFiles('go.sum', 'tools/go.sum') }}
81+
restore-keys: ${{ runner.os }}-${{ runner.arch }}-go-build-
82+
# Download modules only on cache miss
83+
- run: $GITHUB_ACTION_PATH/nix-develop.sh --command go mod download
84+
if: steps.go-mod-cache.outputs.cache-hit != 'true'
85+
shell: bash
86+
- run: $GITHUB_ACTION_PATH/nix-develop.sh --command go mod download -modfile=tools/go.mod
87+
if: steps.go-mod-cache.outputs.cache-hit != 'true'
88+
shell: bash
6789
- name: Notify of metrics availability
6890
if: (inputs.prometheus_username != '')
6991
shell: bash

.github/workflows/ci.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ jobs:
4646
runs-on: ubuntu-latest
4747
steps:
4848
- uses: actions/checkout@v4
49-
- uses: ./.github/actions/setup-go-for-project
5049
- name: Run e2e tests
5150
uses: ./.github/actions/run-monitored-tmpnet-cmd
5251
with:
@@ -65,7 +64,6 @@ jobs:
6564
runs-on: ubuntu-latest
6665
steps:
6766
- uses: actions/checkout@v4
68-
- uses: ./.github/actions/setup-go-for-project
6967
- name: Run e2e tests
7068
uses: ./.github/actions/run-monitored-tmpnet-cmd
7169
with:
@@ -84,7 +82,6 @@ jobs:
8482
runs-on: ubuntu-latest
8583
steps:
8684
- uses: actions/checkout@v4
87-
- uses: ./.github/actions/setup-go-for-project
8885
- uses: ./.github/actions/run-monitored-tmpnet-cmd
8986
with:
9087
run: ./scripts/run_task.sh test-e2e-kube-ci
@@ -103,7 +100,6 @@ jobs:
103100
runs-on: ubuntu-latest
104101
steps:
105102
- uses: actions/checkout@v4
106-
- uses: ./.github/actions/setup-go-for-project
107103
- name: Run e2e tests with existing network
108104
uses: ./.github/actions/run-monitored-tmpnet-cmd
109105
with:
@@ -121,7 +117,6 @@ jobs:
121117
runs-on: ubuntu-latest
122118
steps:
123119
- uses: actions/checkout@v4
124-
- uses: ./.github/actions/setup-go-for-project
125120
- name: Run e2e tests
126121
uses: ./.github/actions/run-monitored-tmpnet-cmd
127122
with:
@@ -139,7 +134,6 @@ jobs:
139134
runs-on: ubuntu-latest
140135
steps:
141136
- uses: actions/checkout@v4
142-
- uses: ./.github/actions/setup-go-for-project
143137
- uses: ./.github/actions/install-nix
144138
- name: Runs all lint checks
145139
shell: nix develop --command bash -x {0}
@@ -157,7 +151,6 @@ jobs:
157151
runs-on: ubuntu-latest
158152
steps:
159153
- uses: actions/checkout@v4
160-
- uses: ./.github/actions/setup-go-for-project
161154
# Use the dev shell instead of bufbuild/buf-action to ensure the dev shell provides the expected versions
162155
- uses: ./.github/actions/install-nix
163156
- shell: nix develop --command bash -x {0}
@@ -183,7 +176,6 @@ jobs:
183176
runs-on: ubuntu-latest
184177
steps:
185178
- uses: actions/checkout@v4
186-
- uses: ./.github/actions/setup-go-for-project
187179
- uses: ./.github/actions/install-nix
188180
- shell: nix develop --command bash -x {0}
189181
run: task check-generate-load-contract-bindings
@@ -230,7 +222,6 @@ jobs:
230222
runs-on: ubuntu-latest
231223
steps:
232224
- uses: actions/checkout@v4
233-
- uses: ./.github/actions/setup-go-for-project
234225
- uses: ./.github/actions/install-nix
235226
- name: Run e2e tests
236227
shell: bash
@@ -240,7 +231,6 @@ jobs:
240231
runs-on: ubuntu-latest
241232
steps:
242233
- uses: actions/checkout@v4
243-
- uses: ./.github/actions/setup-go-for-project
244234
- uses: ./.github/actions/run-monitored-tmpnet-cmd
245235
with:
246236
run: ./scripts/run_task.sh test-load -- --load-timeout=30s
@@ -258,7 +248,6 @@ jobs:
258248
runs-on: ubuntu-latest
259249
steps:
260250
- uses: actions/checkout@v4
261-
- uses: ./.github/actions/setup-go-for-project
262251
- uses: ./.github/actions/run-monitored-tmpnet-cmd
263252
with:
264253
run: ./scripts/run_task.sh test-load-kube-kind -- --load-timeout=30s
@@ -276,7 +265,6 @@ jobs:
276265
runs-on: ubuntu-latest
277266
steps:
278267
- uses: actions/checkout@v4
279-
- uses: ./.github/actions/setup-go-for-project
280268
- uses: ./.github/actions/install-nix
281269
# TODO(marun) Extend testing of robustness beyond deploying a suitable test environment
282270
- name: Deploy kind with chaos mesh

.github/workflows/firewood-load-test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ jobs:
2020
sudo apt-get install -y xz-utils
2121
fi
2222
- uses: actions/checkout@v4
23-
- uses: ./.github/actions/setup-go-for-project
2423
- uses: ./.github/actions/run-monitored-tmpnet-cmd
2524
with:
2625
run: ./scripts/run_task.sh test-load -- --load-timeout=30m --firewood

.github/workflows/self-hosted-load-tests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ jobs:
3434
sudo apt-get install -y xz-utils
3535
fi
3636
- uses: actions/checkout@v4
37-
- uses: ./.github/actions/setup-go-for-project
3837
- name: Run load test
3938
uses: ./.github/actions/run-monitored-tmpnet-cmd
4039
with:

flake.nix

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
# Task runner
4040
go-task
4141

42+
# Local Go package
43+
(import ./nix/go.nix { inherit pkgs; })
44+
4245
# Monitoring tools
4346
promtail # Loki log shipper
4447
prometheus # Metrics collector
@@ -71,6 +74,12 @@
7174
# Add scripts/ directory to PATH so kind-with-registry.sh is accessible
7275
shellHook = ''
7376
export PATH="$PWD/scripts:$PATH"
77+
78+
# Ensure golang bin is in the path
79+
GOBIN="$(go env GOPATH)/bin"
80+
if [[ ":$PATH:" != *":$GOBIN:"* ]]; then
81+
export PATH="$GOBIN:$PATH"
82+
fi
7483
'';
7584
};
7685
});

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module github.com/ava-labs/avalanchego
88
// - CONTRIBUTING.md
99
// - README.md
1010
// - go.mod (here)
11+
// - nix/go.nix (update version and sha256 for supported arches)
1112
// - tools/go.mod
1213
//
1314
// - If updating between minor versions (e.g. 1.24.x -> 1.25.x):

nix/go.nix

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{ pkgs }:
2+
let
3+
# Helper functions to derive Go arch from Nix arch
4+
nixArchToGoArch = arch: {
5+
"x86_64" = "amd64";
6+
"aarch64" = "arm64";
7+
}.${arch} or arch;
8+
9+
# Split system into arch and os
10+
parseSystem = system:
11+
let
12+
parts = builtins.split "-" system;
13+
arch = builtins.elemAt parts 0;
14+
os = builtins.elemAt parts 2;
15+
in {
16+
goarch = nixArchToGoArch arch;
17+
goos = os;
18+
goURLPath = "${os}-${nixArchToGoArch arch}";
19+
};
20+
21+
# Update the following to change the version:
22+
goVersion = "1.24.7";
23+
goSHA256s = {
24+
"linux-amd64" = "da18191ddb7db8a9339816f3e2b54bdded8047cdc2a5d67059478f8d1595c43f";
25+
"linux-arm64" = "fd2bccce882e29369f56c86487663bb78ba7ea9e02188a5b0269303a0c3d33ab";
26+
"darwin-amd64" = "138b6be2138e83d2c90c23d3a2cc94fcb11864d8db0706bb1d1e0dde744dc46a";
27+
"darwin-arm64" = "d06bad763f8820d3e29ee11f2c0c71438903c007e772a159c5760a300298302e";
28+
};
29+
30+
targetSystem = parseSystem pkgs.system;
31+
in
32+
pkgs.stdenv.mkDerivation {
33+
name = "go-${goVersion}";
34+
version = goVersion;
35+
36+
inherit (targetSystem) goos goarch;
37+
GOOS = targetSystem.goos;
38+
GOARCH = targetSystem.goarch;
39+
40+
src = pkgs.fetchurl {
41+
url = "https://go.dev/dl/go${goVersion}.${targetSystem.goURLPath}.tar.gz";
42+
sha256 = goSHA256s.${targetSystem.goURLPath} or (throw "Unsupported system: ${pkgs.system}");
43+
};
44+
45+
# Skip unpacking since we need special handling for the tarball
46+
dontUnpack = true;
47+
48+
installPhase = ''
49+
mkdir -p $out
50+
# Extract directly to output, stripping the 'go' directory prefix
51+
# and ignoring permission/ownership issues in containers
52+
tar xzf $src -C $out --strip-components=1 --no-same-owner --no-same-permissions
53+
# Ensure go binary is executable
54+
chmod +x $out/bin/go
55+
'';
56+
}

0 commit comments

Comments
 (0)