From ce6138c4c541a22a0b8acb328af9a38f6397a72d Mon Sep 17 00:00:00 2001 From: Grayson Tinker Date: Mon, 21 Sep 2026 12:45:56 -0600 Subject: [PATCH 1/5] go_1_{26,27}: stop misusing build script environment variables --- pkgs/development/compilers/go/1.26.nix | 16 +++++++++------- pkgs/development/compilers/go/1.27.nix | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pkgs/development/compilers/go/1.26.nix b/pkgs/development/compilers/go/1.26.nix index 74efd4d9e954..2f98863206d8 100644 --- a/pkgs/development/compilers/go/1.26.nix +++ b/pkgs/development/compilers/go/1.26.nix @@ -66,7 +66,9 @@ stdenv.mkDerivation (finalAttrs: { ]; env = { - inherit (stdenv.targetPlatform.go) GOOS GOARCH GOARM; + # These control the architectures we're building the toolchain itself for in make.bash. + # Go's "target architecture" is our host. + inherit (stdenv.hostPlatform.go) GOOS GOARCH GOARM; # GOHOSTOS/GOHOSTARCH must match the building system, not the host system. # Go will nevertheless build a for host system that we will copy over in # the install phase. @@ -92,10 +94,10 @@ stdenv.mkDerivation (finalAttrs: { GOROOT_BOOTSTRAP = "${goBootstrap}/share/go"; } // lib.optionalAttrs isCross { - # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those - # to be different from CC/CXX - CC_FOR_TARGET = "${targetCC}/bin/${targetCC.targetPrefix}cc"; - CXX_FOR_TARGET = "${targetCC}/bin/${targetCC.targetPrefix}c++"; + # Go's "target architecture" is our host, so these variables are the compilers for + # building the toolchain for hostPlatform (passed as GOOS/GOARCH above) + CC_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; + CXX_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++"; }; buildPhase = '' @@ -108,8 +110,8 @@ stdenv.mkDerivation (finalAttrs: { export PATH=$(pwd)/bin:$PATH ${lib.optionalString isCross '' - # Independent from host/target, CC should produce code for the building system. - # We only set it when cross-compiling. + # "Command line to run to compile C code for GOHOSTARCH." + # Go's "host" is our buildPlatform. export CC=${buildPackages.stdenv.cc}/bin/cc # Prefer external linker for cross when CGO is supported, since # we haven't taught go's internal linker to pick the correct ELF diff --git a/pkgs/development/compilers/go/1.27.nix b/pkgs/development/compilers/go/1.27.nix index 05d99837fecd..2ff13d4aa7e9 100644 --- a/pkgs/development/compilers/go/1.27.nix +++ b/pkgs/development/compilers/go/1.27.nix @@ -66,7 +66,9 @@ stdenv.mkDerivation (finalAttrs: { ]; env = { - inherit (stdenv.targetPlatform.go) GOOS GOARCH GOARM; + # These control the architectures we're building the toolchain itself for in make.bash. + # Go's "target architecture" is our host. + inherit (stdenv.hostPlatform.go) GOOS GOARCH GOARM; # GOHOSTOS/GOHOSTARCH must match the building system, not the host system. # Go will nevertheless build a for host system that we will copy over in # the install phase. @@ -92,10 +94,10 @@ stdenv.mkDerivation (finalAttrs: { GOROOT_BOOTSTRAP = "${goBootstrap}/share/go"; } // lib.optionalAttrs isCross { - # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those - # to be different from CC/CXX - CC_FOR_TARGET = "${targetCC}/bin/${targetCC.targetPrefix}cc"; - CXX_FOR_TARGET = "${targetCC}/bin/${targetCC.targetPrefix}c++"; + # Go's "target architecture" is our host, so these variables are the compilers for + # building the toolchain for hostPlatform (passed as GOOS/GOARCH above) + CC_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; + CXX_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++"; }; buildPhase = '' @@ -108,8 +110,8 @@ stdenv.mkDerivation (finalAttrs: { export PATH=$(pwd)/bin:$PATH ${lib.optionalString isCross '' - # Independent from host/target, CC should produce code for the building system. - # We only set it when cross-compiling. + # "Command line to run to compile C code for GOHOSTARCH." + # Go's "host" is our buildPlatform. export CC=${buildPackages.stdenv.cc}/bin/cc # Prefer external linker for cross when CGO is supported, since # we haven't taught go's internal linker to pick the correct ELF From 374e2bd28630c9bf9827f563487138c1aa147cff Mon Sep 17 00:00:00 2001 From: Grayson Tinker Date: Mon, 21 Sep 2026 14:43:04 -0600 Subject: [PATCH 2/5] buildGoModule: inherit GOOS and GOARCH from hostSystem instead of go go.GOOS is OS of the host platform of Go, which has no bearing on the platform which we are compiling for. Inherit from the hostSystem instead, which properly adjusts when cross-compiling. --- pkgs/build-support/go/module.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/go/module.nix b/pkgs/build-support/go/module.nix index bbe918684a0f..e394cde32738 100644 --- a/pkgs/build-support/go/module.nix +++ b/pkgs/build-support/go/module.nix @@ -217,7 +217,7 @@ lib.extendMkDerivation { nativeBuildInputs = [ go ] ++ nativeBuildInputs; env = args.env or { } // { - inherit (go) GOOS GOARCH; + inherit (stdenv.hostPlatform.go) GOOS GOARCH; GOTOOLCHAIN = "local"; From ccc0a0ca445284d4af38ae7a45ccd5f875f75b87 Mon Sep 17 00:00:00 2001 From: Grayson Tinker Date: Mon, 21 Sep 2026 18:56:17 -0600 Subject: [PATCH 3/5] go_1_{26,27}: fix logic of if we're cross-compiling --- pkgs/development/compilers/go/1.26.nix | 3 ++- pkgs/development/compilers/go/1.27.nix | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pkgs/development/compilers/go/1.26.nix b/pkgs/development/compilers/go/1.26.nix index 2f98863206d8..c85c19782f74 100644 --- a/pkgs/development/compilers/go/1.26.nix +++ b/pkgs/development/compilers/go/1.26.nix @@ -21,7 +21,7 @@ let # to handle the cross-building case where build != host == target targetCC = pkgsBuildTarget.targetPackages.stdenv.cc; - isCross = stdenv.buildPlatform != stdenv.targetPlatform; + isCross = !(lib.systems.equals stdenv.buildPlatform stdenv.hostPlatform); in stdenv.mkDerivation (finalAttrs: { pname = "go"; @@ -134,6 +134,7 @@ stdenv.mkDerivation (finalAttrs: { rm src/regexp/syntax/make_perl_groups.pl '' + ( + # Not equivalent to isCross; e.g. x86_64-linux vs. musl64 have the same system string if (stdenv.buildPlatform.system != stdenv.hostPlatform.system) then '' mv bin/*_*/* bin diff --git a/pkgs/development/compilers/go/1.27.nix b/pkgs/development/compilers/go/1.27.nix index 2ff13d4aa7e9..624d532cc9f2 100644 --- a/pkgs/development/compilers/go/1.27.nix +++ b/pkgs/development/compilers/go/1.27.nix @@ -21,7 +21,7 @@ let # to handle the cross-building case where build != host == target targetCC = pkgsBuildTarget.targetPackages.stdenv.cc; - isCross = stdenv.buildPlatform != stdenv.targetPlatform; + isCross = !(lib.systems.equals stdenv.buildPlatform stdenv.hostPlatform); in stdenv.mkDerivation (finalAttrs: { pname = "go"; @@ -134,6 +134,7 @@ stdenv.mkDerivation (finalAttrs: { rm src/regexp/syntax/make_perl_groups.pl '' + ( + # Not equivalent to isCross; e.g. x86_64-linux vs. musl64 have the same system string if (stdenv.buildPlatform.system != stdenv.hostPlatform.system) then '' mv bin/*_*/* bin From 2afd837088347aac28f67d02721be01a588f03b3 Mon Sep 17 00:00:00 2001 From: Grayson Tinker Date: Mon, 21 Sep 2026 18:56:17 -0600 Subject: [PATCH 4/5] go_1_{26,27}: drop useless dependency --- pkgs/development/compilers/go/1.26.nix | 7 ------- pkgs/development/compilers/go/1.27.nix | 7 ------- 2 files changed, 14 deletions(-) diff --git a/pkgs/development/compilers/go/1.26.nix b/pkgs/development/compilers/go/1.26.nix index c85c19782f74..7fece112cbfb 100644 --- a/pkgs/development/compilers/go/1.26.nix +++ b/pkgs/development/compilers/go/1.26.nix @@ -7,7 +7,6 @@ iana-etc, mailcap, buildPackages, - pkgsBuildTarget, targetPackages, # for testing buildGo126Module, @@ -17,10 +16,6 @@ let goBootstrap = buildPackages.callPackage ./bootstrap124.nix { }; - # We need a target compiler which is still runnable at build time, - # to handle the cross-building case where build != host == target - targetCC = pkgsBuildTarget.targetPackages.stdenv.cc; - isCross = !(lib.systems.equals stdenv.buildPlatform stdenv.hostPlatform); in stdenv.mkDerivation (finalAttrs: { @@ -38,8 +33,6 @@ stdenv.mkDerivation (finalAttrs: { ++ lib.optionals stdenv.hostPlatform.isLinux [ stdenv.cc.libc.out ] ++ lib.optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ]; - depsBuildTarget = lib.optional isCross targetCC; - depsTargetTarget = lib.optional stdenv.targetPlatform.isMinGW targetPackages.threads; postPatch = '' diff --git a/pkgs/development/compilers/go/1.27.nix b/pkgs/development/compilers/go/1.27.nix index 624d532cc9f2..0560cb881775 100644 --- a/pkgs/development/compilers/go/1.27.nix +++ b/pkgs/development/compilers/go/1.27.nix @@ -7,7 +7,6 @@ iana-etc, mailcap, buildPackages, - pkgsBuildTarget, targetPackages, # for testing buildGo127Module, @@ -17,10 +16,6 @@ let goBootstrap = buildPackages.callPackage ./bootstrap124.nix { }; - # We need a target compiler which is still runnable at build time, - # to handle the cross-building case where build != host == target - targetCC = pkgsBuildTarget.targetPackages.stdenv.cc; - isCross = !(lib.systems.equals stdenv.buildPlatform stdenv.hostPlatform); in stdenv.mkDerivation (finalAttrs: { @@ -38,8 +33,6 @@ stdenv.mkDerivation (finalAttrs: { ++ lib.optionals stdenv.hostPlatform.isLinux [ stdenv.cc.libc.out ] ++ lib.optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ]; - depsBuildTarget = lib.optional isCross targetCC; - depsTargetTarget = lib.optional stdenv.targetPlatform.isMinGW targetPackages.threads; postPatch = '' From fee211b582c36f8634e20f68d82565c56e71885b Mon Sep 17 00:00:00 2001 From: Grayson Tinker Date: Tue, 3 Nov 2026 14:51:02 -0700 Subject: [PATCH 5/5] go: restore GO_EXTLINK_ENABLED env variable to be gated on build != target --- pkgs/development/compilers/go/1.26.nix | 20 ++++++++++++++------ pkgs/development/compilers/go/1.27.nix | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/pkgs/development/compilers/go/1.26.nix b/pkgs/development/compilers/go/1.26.nix index 7fece112cbfb..3efd0ed9a2ba 100644 --- a/pkgs/development/compilers/go/1.26.nix +++ b/pkgs/development/compilers/go/1.26.nix @@ -91,6 +91,20 @@ stdenv.mkDerivation (finalAttrs: { # building the toolchain for hostPlatform (passed as GOOS/GOARCH above) CC_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; CXX_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++"; + } + // lib.optionalAttrs (!lib.systems.equals stdenv.buildPlatform stdenv.targetPlatform) { + # Prefer external linker for cross when CGO is supported, since + # we haven't taught go's internal linker to pick the correct ELF + # interpreter for cross + # When CGO is not supported we rely on static binaries being built + # since they don't need an ELF interpreter + + # Without an equivalent, pkgsStatic builds fail. See for some extra context + # https://github.com/NixOS/nixpkgs/issues/582185 + + # TODO: Clean this up. Out-of-tree users should almost never be hitting this case, + # except through buildGoModule, so we have relative freedom in doing so. + GO_EXTLINK_ENABLED = finalAttrs.env.CGO_ENABLED; }; buildPhase = '' @@ -106,12 +120,6 @@ stdenv.mkDerivation (finalAttrs: { # "Command line to run to compile C code for GOHOSTARCH." # Go's "host" is our buildPlatform. export CC=${buildPackages.stdenv.cc}/bin/cc - # Prefer external linker for cross when CGO is supported, since - # we haven't taught go's internal linker to pick the correct ELF - # interpreter for cross - # When CGO is not supported we rely on static binaries being built - # since they don't need an ELF interpreter - export GO_EXTLINK_ENABLED=${toString finalAttrs.env.CGO_ENABLED} ''} ulimit -a diff --git a/pkgs/development/compilers/go/1.27.nix b/pkgs/development/compilers/go/1.27.nix index 0560cb881775..04d0346b8fe7 100644 --- a/pkgs/development/compilers/go/1.27.nix +++ b/pkgs/development/compilers/go/1.27.nix @@ -91,6 +91,20 @@ stdenv.mkDerivation (finalAttrs: { # building the toolchain for hostPlatform (passed as GOOS/GOARCH above) CC_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; CXX_FOR_TARGET = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++"; + } + // lib.optionalAttrs (!lib.systems.equals stdenv.buildPlatform stdenv.targetPlatform) { + # Prefer external linker for cross when CGO is supported, since + # we haven't taught go's internal linker to pick the correct ELF + # interpreter for cross + # When CGO is not supported we rely on static binaries being built + # since they don't need an ELF interpreter + + # Without an equivalent, pkgsStatic builds fail. See for some extra context + # https://github.com/NixOS/nixpkgs/issues/582185 + + # TODO: Clean this up. Out-of-tree users should almost never be hitting this case, + # except through buildGoModule, so we have relative freedom in doing so. + GO_EXTLINK_ENABLED = finalAttrs.env.CGO_ENABLED; }; buildPhase = '' @@ -106,12 +120,6 @@ stdenv.mkDerivation (finalAttrs: { # "Command line to run to compile C code for GOHOSTARCH." # Go's "host" is our buildPlatform. export CC=${buildPackages.stdenv.cc}/bin/cc - # Prefer external linker for cross when CGO is supported, since - # we haven't taught go's internal linker to pick the correct ELF - # interpreter for cross - # When CGO is not supported we rely on static binaries being built - # since they don't need an ELF interpreter - export GO_EXTLINK_ENABLED=${toString finalAttrs.env.CGO_ENABLED} ''} ulimit -a