buildGo{Module,Package}: add tags argument

Simpler method of setting tags rather than using some combination of buildFlags, buildFlagsArray, preBuild, etc

Using `lib.concatStringsSep ","` as space separated tags are deprecated in go.
This commit is contained in:
zowoq 2021-07-31 14:57:54 +10:00
parent ed6852a778
commit b60dde0c1e
3 changed files with 23 additions and 12 deletions

View file

@ -112,16 +112,6 @@ done
Both `buildGoModule` and `buildGoPackage` can be tweaked to behave slightly differently, if the following attributes are used:
### `buildFlagsArray` and `buildFlags`: {#ex-goBuildFlags-noarray}
These attributes set build flags supported by `go build`. We recommend using `buildFlagsArray`.
```nix
buildFlagsArray = [
"-tags=release"
];
```
### `ldflags` {#var-go-ldflags}
Arguments to pass to the Go linker tool via the `-ldflags` argument of `go build`. The most common use case for this argument is to make the resulting executable aware of its own version. For example:
@ -134,6 +124,21 @@ Arguments to pass to the Go linker tool via the `-ldflags` argument of `go build
];
```
### `tags` {#var-go-tags}
Arguments to pass to the Go via the `-tags` argument of `go build`. For example:
```nix
tags = [
"production"
"sqlite"
];
```
```nix
tags = [ "production" ] ++ lib.optionals withSqlite [ "sqlite" ];
```
### `deleteVendor` {#var-go-deleteVendor}
Removes the pre-existing vendor directory. This should only be used if the dependencies included in the vendor folder are broken or incomplete.

View file

@ -10,6 +10,9 @@
# Go linker flags, passed to go via -ldflags
, ldflags ? []
# Go tags, passed to go via -tag
, tags ? []
# A function to override the go-modules derivation
, overrideModAttrs ? (_oldAttrs : {})
@ -156,7 +159,7 @@ let
echo "$d" | grep -q "\(/_\|examples\|Godeps\|testdata\)" && return 0
[ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0
local OUT
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${ldflags:+-ldflags="$ldflags"} -v -p $NIX_BUILD_CORES $d 2>&1)"; then
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${tags:+-tags=${lib.concatStringsSep "," tags}} ''${ldflags:+-ldflags="$ldflags"} -v -p $NIX_BUILD_CORES $d 2>&1)"; then
if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
echo "$OUT" >&2
return 1

View file

@ -10,6 +10,9 @@
# Go linker flags, passed to go via -ldflags
, ldflags ? []
# Go tags, passed to go via -tag
, tags ? []
# We want parallel builds by default
, enableParallelBuilding ? true
@ -151,7 +154,7 @@ let
echo "$d" | grep -q "\(/_\|examples\|Godeps\)" && return 0
[ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0
local OUT
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${ldflags:+-ldflags="$ldflags"} -v -p $NIX_BUILD_CORES $d 2>&1)"; then
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${tags:+-tags=${lib.concatStringsSep "," tags}} ''${ldflags:+-ldflags="$ldflags"} -v -p $NIX_BUILD_CORES $d 2>&1)"; then
if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
echo "$OUT" >&2
return 1