Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/languages-frameworks/go.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 2 additions & 0 deletions nixos/doc/manual/release-notes/rl-2505.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 21 additions & 6 deletions pkgs/build-support/go/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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=";
Expand All @@ -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
Expand All @@ -209,6 +221,8 @@ in
''
runHook preBuild

export GOFLAGS="''${goFlags[*]}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we'd want to do it, but we could have goFlags be appended to GOFLAGS in case it's already set, for some reason.

I'm trying to look at this from a standpoint where the build script eventually becomes a hook, which wouldn't be able to cleanly enforce using goFlags over GOFLAGS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to look at this from a standpoint where the build script eventually becomes a hook, which wouldn't be able to cleanly enforce using goFlags over GOFLAGS.

If the "hook" hear means a "setup hook", it can as long as it provides the buildPhase and the checkPhase.


exclude='\(/_\|examples\|Godeps\|testdata'
if [[ -n "$excludedPackages" ]]; then
IFS=' ' read -r -a excludedArr <<<$excludedPackages
Expand Down Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions pkgs/stdenv/generic/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,25 @@ appendToVar() {
fi
}

removeAllPrefixedFromVar() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is something that would be useful globally, but should probably stay in module.nix until the testing is done.

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.
Expand Down