From fd5eeb97c350f0b04b2e05968544ed1eeb7aecff Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Mon, 2 Dec 2024 00:51:05 +0800 Subject: [PATCH 1/2] setup.sh: add function removeAllPrefixedFromVar --- pkgs/stdenv/generic/setup.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 0660c6b642db7..397b95f495ed8 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -385,6 +385,25 @@ appendToVar() { fi } +removeAllPrefixedFromVar() { + local varName="$1" + local -n nameref="$varName" + shift + if ! [[ -z "${nameref[*]-}" ]] || (("$#" == 0)); then + return + fi + if [[ "$(declare -p "$varName")" =~ "declare -a" ]]; then + for prefix in "$@"; do + nameref=("${nameref[@]/$prefix}") + done + else + local _arr=() + read -ra _arr <<<"$nameref" + removeAllPrefixedFromVar _arr "$@" + nameref="${_arr[*]}" + fi +} + # Accumulate flags from the named variables $2+ into the indexed array $1. # # Arrays are simply concatenated, strings are split on whitespace. From 8bd0d067dc6d3f432b65038c67587a5f75404d69 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Mon, 2 Dec 2024 01:43:13 +0800 Subject: [PATCH 2/2] buildGoModule: introduce goFlags to form GOFLAGS --- doc/languages-frameworks/go.section.md | 4 +++ .../manual/release-notes/rl-2505.section.md | 2 ++ pkgs/build-support/go/module.nix | 27 ++++++++++++++----- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/doc/languages-frameworks/go.section.md b/doc/languages-frameworks/go.section.md index 4529639f3d4ad..7a28841a44720 100644 --- a/doc/languages-frameworks/go.section.md +++ b/doc/languages-frameworks/go.section.md @@ -117,6 +117,10 @@ The root directory of the Go module that contains the `go.mod` file. Defaults to `./`, which is the root of `src`. +### `goFlags` {#var-go-goFlags} + +A string list of flags to form the environment variable `GOFLAGS` at the beginning of `buildPhase` and `checkPhase`, overridable via `overrideAttrs`. + ### `ldflags` {#var-go-ldflags} A string list of flags to pass to the Go linker tool via the `-ldflags` argument of `go build`. Possible values can be retrieved by running `go tool link --help`. diff --git a/nixos/doc/manual/release-notes/rl-2505.section.md b/nixos/doc/manual/release-notes/rl-2505.section.md index 6814caa1bad28..a1f1006228cbb 100644 --- a/nixos/doc/manual/release-notes/rl-2505.section.md +++ b/nixos/doc/manual/release-notes/rl-2505.section.md @@ -40,6 +40,8 @@ - `binwalk` was updated to 3.1.0, which has been rewritten in rust. The python module is no longer available. See the release notes of [3.1.0](https://github.com/ReFirmLabs/binwalk/releases/tag/v3.1.0) for more information. +- `buildGoModule` receives a new argument `goFlags` to form the environment variable `GOFLAGS` during build. `GOFLAGS` specified as a `buildGoModule` argument is assimilated into `goFlags` during evaluation for compatibility purposes, but such way of specification is deprecated and the compatibility behaviour would be removed in future releases. + - `buildGoPackage` has been removed. Use `buildGoModule` instead. See the [Go section in the nixpkgs manual](https://nixos.org/manual/nixpkgs/unstable/#sec-language-go) for details. - `timescaledb` requires manual upgrade steps. diff --git a/pkgs/build-support/go/module.nix b/pkgs/build-support/go/module.nix index 4ef0bcc9db21e..a32d6e40cf34c 100644 --- a/pkgs/build-support/go/module.nix +++ b/pkgs/build-support/go/module.nix @@ -167,12 +167,24 @@ in inherit (go) GOOS GOARCH; - GOFLAGS = GOFLAGS - ++ lib.warnIf (lib.any (lib.hasPrefix "-mod=") GOFLAGS) "use `proxyVendor` to control Go module/vendor behavior instead of setting `-mod=` in GOFLAGS" + inherit CGO_ENABLED enableParallelBuilding GO111MODULE GOTOOLCHAIN; + + goFlags = + let + # Compatibility layers for direct specification of the GOFLAGS environment variable + goFlags = + lib.optionals (args?GOFLAGS) ( + if lib.isString args.GOFLAGS then + lib.splitString args.GOFLAGS + else args.GOFLAGS + ) ++ args.goFlags or [ ]; + in + 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.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; + # If not set to an explicit value, set the buildid empty for reproducibility. ldflags = ldflags ++ lib.optional (!lib.any (lib.hasPrefix "-buildid=") ldflags) "-buildid="; @@ -197,7 +209,7 @@ in # currently pie is only enabled by default in pkgsMusl # this will respect the `hardening{Disable,Enable}` flags if set if [[ $NIX_HARDENING_ENABLE =~ "pie" ]]; then - export GOFLAGS="-buildmode=pie $GOFLAGS" + prependToVar goFlags -buildmode=pie fi runHook postConfigure @@ -209,6 +221,8 @@ in '' runHook preBuild + export GOFLAGS="''${goFlags[*]}" + exclude='\(/_\|examples\|Godeps\|testdata' if [[ -n "$excludedPackages" ]]; then IFS=' ' read -r -a excludedArr <<<$excludedPackages @@ -283,7 +297,8 @@ in checkPhase = args.checkPhase or '' runHook preCheck # We do not set trimpath for tests, in case they reference test assets - export GOFLAGS=''${GOFLAGS//-trimpath/} + removeAllPrefixedFromVar goFlags -trimpath + export GOFLAGS="''${goFlags[*]}" for pkg in $(getGoDirs test); do buildGoDir test "$pkg"