From d7f86e118db45937718f4f6a899e7f2ccfeb3b43 Mon Sep 17 00:00:00 2001 From: Maru Newby Date: Fri, 21 Mar 2025 17:30:21 -0700 Subject: [PATCH] [nix] Add package for official golang binaries to the dev shell This ensures dev shell users always have the required golang version. --- flake.nix | 11 +++++++++++ go.mod | 1 + nix/go.nix | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 nix/go.nix diff --git a/flake.nix b/flake.nix index 9eaf6c78db49..8c26e2efe534 100644 --- a/flake.nix +++ b/flake.nix @@ -33,6 +33,9 @@ default = pkgs.mkShell { # The Nix packages provided in the environment packages = with pkgs; [ + # Local Go package + (import ./nix/go.nix { inherit pkgs; }) + # Monitoring tools promtail # Loki log shipper prometheus # Metrics collector @@ -54,6 +57,14 @@ # macOS-specific frameworks darwin.apple_sdk.frameworks.Security ]; + + shellHook = '' + # Ensure golang bin is in the path + GOBIN="$(go env GOPATH)/bin" + if [[ ":$PATH:" != *":$GOBIN:"* ]]; then + export PATH="$GOBIN:$PATH" + fi + ''; }; }); diff --git a/go.mod b/go.mod index d4b390d9dcc8..71250a09aab0 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/ava-labs/avalanchego // - Changes to the minimum golang version must also be replicated in: // - CONTRIBUTING.md // - README.md +// - nix/go.nix (update version and sha256 for supported arches) // - go.mod (here) // // - If updating between minor versions (e.g. 1.23.x -> 1.24.x): diff --git a/nix/go.nix b/nix/go.nix new file mode 100644 index 000000000000..7aca299160f6 --- /dev/null +++ b/nix/go.nix @@ -0,0 +1,50 @@ +{ pkgs }: +let + # Helper functions to derive Go arch from Nix arch + nixArchToGoArch = arch: { + "x86_64" = "amd64"; + "aarch64" = "arm64"; + }.${arch} or arch; + + # Split system into arch and os + parseSystem = system: + let + parts = builtins.split "-" system; + arch = builtins.elemAt parts 0; + os = builtins.elemAt parts 2; + in { + goarch = nixArchToGoArch arch; + goos = os; + goURLPath = "${os}-${nixArchToGoArch arch}"; + }; + + # Update the following to change the version: + goVersion = "1.23.6"; + goSHA256s = { + "linux-amd64" = "9379441ea310de000f33a4dc767bd966e72ab2826270e038e78b2c53c2e7802d"; + "linux-arm64" = "561c780e8f4a8955d32bf72e46af0b5ee5e0debe1e4633df9a03781878219202"; + "darwin-amd64" = "782da50ce8ec5e98fac2cd3cdc6a1d7130d093294fc310038f651444232a3fb0"; + "darwin-arm64" = "5cae2450a1708aeb0333237a155640d5562abaf195defebc4306054565536221"; + }; + + targetSystem = parseSystem pkgs.system; +in +pkgs.stdenv.mkDerivation { + name = "go-${goVersion}"; + version = goVersion; + + inherit (targetSystem) goos goarch; + GOOS = targetSystem.goos; + GOARCH = targetSystem.goarch; + + src = pkgs.fetchurl { + url = "https://go.dev/dl/go${goVersion}.${targetSystem.goURLPath}.tar.gz"; + sha256 = goSHA256s.${targetSystem.goURLPath} or (throw "Unsupported system: ${pkgs.system}"); + }; + + installPhase = '' + mkdir -p $out + cp -r ./* $out/ + chmod +x $out/bin/go + ''; +}