Skip to content

Commit

Permalink
buildGoModule/buildGoPackage: Introduce ldflags argument
Browse files Browse the repository at this point in the history
Previously it was not possible to define multiple ldflags, since only
the last definition applies, and there's some quoting issues with
`buildFlagsArray`. With the new `ldflags` argument it's possible to do
this, e.g.

    ldflags = drv.ldflags or [] ++ [
      "-X main.Version=1.0"
    ]

can now properly append a flag without clearing all previous ldflags.
  • Loading branch information
infinisil committed Jun 4, 2021
1 parent 1508c22 commit 764d1c0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
19 changes: 11 additions & 8 deletions doc/languages-frameworks/go.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,24 @@ Both `buildGoModule` and `buildGoPackage` can be tweaked to behave slightly diff

### `buildFlagsArray` and `buildFlags`: {#ex-goBuildFlags-noarray}

These attributes set build flags supported by `go build`. We recommend using `buildFlagsArray`. The most common use case of these attributes is to make the resulting executable aware of its own version. For example:
These attributes set build flags supported by `go build`. We recommend using `buildFlagsArray`.

```nix
buildFlagsArray = [
# Note: single quotes are not needed.
"-ldflags=-X main.Version=${version} -X main.Commit=${version}"
"-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:

```nix
buildFlagsArray = ''
-ldflags=
-X main.Version=${version}
-X main.Commit=${version}
'';
ldflags = [
"-s" "-w"
"-X main.Version=${version}"
"-X main.Commit=${version}"
];
```

### `deleteVendor` {#var-go-deleteVendor}
Expand Down
5 changes: 4 additions & 1 deletion pkgs/development/go-modules/generic/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
, passthru ? {}
, patches ? []

# Go linker flags, passed to go via -ldflags
, ldflags ? []

# A function to override the go-modules derivation
, overrideModAttrs ? (_oldAttrs : {})

Expand Down Expand Up @@ -153,7 +156,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[@]}" -v -p $NIX_BUILD_CORES $d 2>&1)"; then
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${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
Expand Down
5 changes: 4 additions & 1 deletion pkgs/development/go-packages/generic/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
, preFixup ? ""
, shellHook ? ""

# Go linker flags, passed to go via -ldflags
, ldflags ? []

# We want parallel builds by default
, enableParallelBuilding ? true

Expand Down Expand Up @@ -148,7 +151,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[@]}" -v -p $NIX_BUILD_CORES $d 2>&1)"; then
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${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
Expand Down

0 comments on commit 764d1c0

Please sign in to comment.