buildGoModule: pass environment variables with the env attribute

This help Go modules support __structuredAttrs = true.

This commit doesn't touch GOFLAGS, which will be handled in subequent
changes.
This commit is contained in:
Yueh-Shun Li
2024-12-18 23:16:41 +08:00
parent 9e13482d9f
commit 44876c6004
+26 -8
View File
@@ -59,11 +59,12 @@
}@args':
let
args = removeAttrs args' [ "overrideModAttrs" ];
GO111MODULE = "on";
GOTOOLCHAIN = "local";
args = removeAttrs args' [
"overrideModAttrs"
# Compatibility layer to the directly-specified CGO_ENABLED.
# TODO(@ShamrockLee): Remove after Nixpkgs 25.05 branch-off
"CGO_ENABLED"
];
in
(stdenv.mkDerivation (finalAttrs:
args
@@ -77,8 +78,6 @@ in
nativeBuildInputs = (finalAttrs.nativeBuildInputs or [ ]) ++ [ go git cacert ];
inherit (finalAttrs) src modRoot;
inherit (go) GOOS GOARCH;
inherit GO111MODULE GOTOOLCHAIN;
# The following inheritance behavior is not trivial to expect, and some may
# argue it's not ideal. Changing it may break vendor hashes in Nixpkgs and
@@ -169,14 +168,33 @@ in
nativeBuildInputs = [ go ] ++ nativeBuildInputs;
env = args.env or { } // {
inherit (go) GOOS GOARCH;
GO111MODULE = "on";
GOTOOLCHAIN = "local";
CGO_ENABLED = args.env.CGO_ENABLED or (
if args'?CGO_ENABLED then
# Compatibility layer to the CGO_ENABLED attribute not specified as env.CGO_ENABLED
# TODO(@ShamrockLee): Remove and convert to
# CGO_ENABLED = args.env.CGO_ENABLED or go.CGO_ENABLED
# after the Nixpkgs 25.05 branch-off.
lib.warn
"${finalAttrs.finalPackage.meta.position}: buildGoModule: specify CGO_ENABLED with env.CGO_ENABLED instead."
args'.CGO_ENABLED
else
go.CGO_ENABLED
);
};
GOFLAGS = GOFLAGS
++ lib.warnIf (lib.any (lib.hasPrefix "-mod=") GOFLAGS) "use `proxyVendor` to control Go module/vendor behavior instead of setting `-mod=` in GOFLAGS"
(lib.optional (!finalAttrs.proxyVendor) "-mod=vendor")
++ lib.warnIf (builtins.elem "-trimpath" GOFLAGS) "`-trimpath` is added by default to GOFLAGS by buildGoModule when allowGoReference isn't set to true"
(lib.optional (!allowGoReference) "-trimpath");
inherit CGO_ENABLED enableParallelBuilding GO111MODULE GOTOOLCHAIN;
inherit enableParallelBuilding;
# If not set to an explicit value, set the buildid empty for reproducibility.
ldflags = ldflags ++ lib.optional (!lib.any (lib.hasPrefix "-buildid=") ldflags) "-buildid=";