1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
{
go,
cacert,
gitMinimal,
lib,
stdenv,
}:
lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;
excludeDrvArgNames = [
"overrideModAttrs"
];
extendDrvArgs =
finalAttrs:
{
nativeBuildInputs ? [ ], # Native build inputs used for the derivation.
passthru ? { },
# A function to override the `goModules` derivation.
overrideModAttrs ? (finalAttrs: previousAttrs: { }),
# Directory to the `go.mod` and `go.sum` relative to the `src`.
modRoot ? "./",
# The SRI hash of the vendored dependencies.
# If `null`, it means the project either has no external dependencies
# or the vendored dependencies are already present in the source tree.
vendorHash ? throw (
if args ? vendorSha256 then
"buildGoModule: Expect vendorHash instead of vendorSha256"
else
"buildGoModule: vendorHash is missing"
),
# The go.sum file to track which can cause rebuilds.
goSum ? null,
# Whether to delete the vendor folder supplied with the source.
deleteVendor ? false,
# Whether to fetch (go mod download) and proxy the vendor directory.
# This is useful if your code depends on c code and go mod tidy does not
# include the needed sources to build or if any dependency has case-insensitive
# conflicts which will produce platform dependant `vendorHash` checksums.
proxyVendor ? false,
# We want parallel builds by default.
enableParallelBuilding ? true,
# Do not enable this without good reason
# IE: programs coupled with the compiler.
allowGoReference ? false,
# Meta data for the final derivation.
meta ? { },
# Go linker flags.
ldflags ? [ ],
# Go build flags.
GOFLAGS ? [ ],
# Instead of building binary targets with 'go install', build test binaries with 'go test'.
# The binaries found in $out/bin can be executed as go tests outside of the sandbox.
# This is mostly useful outside of nixpkgs, for example to build integration/e2e tests
# that won't run within the sandbox.
buildTestBinaries ? false,
...
}@args:
{
inherit
modRoot
vendorHash
deleteVendor
proxyVendor
goSum
;
goModules =
if (finalAttrs.vendorHash == null) then
""
else
(stdenv.mkDerivation {
name =
let
prefix = "${finalAttrs.name or "${finalAttrs.pname}-${finalAttrs.version}"}-";
# If "goSum" is supplied then it can cause "goModules" to rebuild.
# Attach the hash name of the "go.sum" file so we can rebuild when it changes.
suffix = lib.optionalString (
finalAttrs.goSum != null
) "-${(lib.removeSuffix "-go.sum" (lib.removePrefix "${builtins.storeDir}/" finalAttrs.goSum))}";
in
"${prefix}go-modules${suffix}";
nativeBuildInputs = (finalAttrs.nativeBuildInputs or [ ]) ++ [
go
gitMinimal
cacert
];
inherit (finalAttrs) src modRoot goSum;
# 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
# out in the wild. In anycase, it's documented in:
# doc/languages-frameworks/go.section.md.
prePatch = finalAttrs.prePatch or "";
patches = finalAttrs.patches or [ ];
patchFlags = finalAttrs.patchFlags or [ ];
postPatch = finalAttrs.postPatch or "";
preBuild = finalAttrs.preBuild or "";
postBuild = finalAttrs.modPostBuild or "";
sourceRoot = finalAttrs.sourceRoot or "";
setSourceRoot = finalAttrs.setSourceRoot or "";
env = finalAttrs.env or { };
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND"
"SOCKS_SERVER"
"GOPROXY"
];
configurePhase =
args.modConfigurePhase or ''
runHook preConfigure
export GOCACHE=$TMPDIR/go-cache
export GOPATH="$TMPDIR/go"
cd "$modRoot"
runHook postConfigure
'';
buildPhase =
args.modBuildPhase or (
''
runHook preBuild
''
+ lib.optionalString finalAttrs.deleteVendor ''
if [ ! -d vendor ]; then
echo "vendor folder does not exist, 'deleteVendor' is not needed"
exit 10
else
rm -rf vendor
fi
''
+ ''
if [ -d vendor ]; then
echo "vendor folder exists, please set 'vendorHash = null;' in your expression"
exit 10
fi
export GIT_SSL_CAINFO=$NIX_SSL_CERT_FILE
${
if finalAttrs.proxyVendor then
''
mkdir -p "$GOPATH/pkg/mod/cache/download"
go mod download
''
else
''
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
goModVendorFlags+=(-v)
fi
go mod vendor "''${goModVendorFlags[@]}"
''
}
mkdir -p vendor
runHook postBuild
''
);
installPhase =
args.modInstallPhase or ''
runHook preInstall
${
if finalAttrs.proxyVendor then
''
rm -rf "$GOPATH/pkg/mod/cache/download/sumdb"
cp -r --reflink=auto "$GOPATH/pkg/mod/cache/download" $out
''
else
''
cp -r --reflink=auto vendor $out
''
}
if ! [ "$(ls -A $out)" ]; then
echo "vendor folder is empty, please set 'vendorHash = null;' in your expression"
exit 10
fi
runHook postInstall
'';
dontFixup = true;
outputHashMode = "recursive";
outputHash = finalAttrs.vendorHash;
# Handle empty `vendorHash`; avoid error:
# empty hash requires explicit hash algorithm.
outputHashAlgo = if finalAttrs.vendorHash == "" then "sha256" else null;
# in case an overlay clears passthru by accident, don't fail evaluation
}).overrideAttrs
(
let
pos = builtins.unsafeGetAttrPos "passthru" finalAttrs;
posString =
if pos == null then "unknown" else "${pos.file}:${toString pos.line}:${toString pos.column}";
in
finalAttrs.passthru.overrideModAttrs
or (lib.warn "buildGoModule: ${finalAttrs.name or finalAttrs.pname}: passthru.overrideModAttrs missing after overrideAttrs. Last overridden at ${posString}." overrideModAttrs)
);
nativeBuildInputs = [ go ] ++ nativeBuildInputs;
env = args.env or { } // {
inherit (go) GOOS GOARCH;
GO111MODULE = "on";
GOTOOLCHAIN = "local";
CGO_ENABLED = args.env.CGO_ENABLED or go.CGO_ENABLED;
GOFLAGS = toString (
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 (!finalAttrs.allowGoReference) "-trimpath")
);
};
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=";
configurePhase =
args.configurePhase or (
''
runHook preConfigure
export GOCACHE=$TMPDIR/go-cache
export GOPATH="$TMPDIR/go"
export GOPROXY=off
export GOSUMDB=off
if [ -f "$NIX_CC_FOR_TARGET/nix-support/dynamic-linker" ]; then
export GO_LDSO=$(cat $NIX_CC_FOR_TARGET/nix-support/dynamic-linker)
fi
cd "$modRoot"
''
+ lib.optionalString (finalAttrs.vendorHash != null) ''
${
if finalAttrs.proxyVendor then
''
export GOPROXY="file://$goModules"
''
else
''
rm -rf vendor
cp -r --reflink=auto "$goModules" vendor
''
}
''
+ ''
runHook postConfigure
''
);
buildPhase =
args.buildPhase or (
lib.warnIf (builtins.elem "-buildid=" ldflags)
"`-buildid=` is set by default as ldflag by buildGoModule"
''
runHook preBuild
exclude='\(/_\|examples\|Godeps\|testdata'
if [[ -n "$excludedPackages" ]]; then
IFS=' ' read -r -a excludedArr <<<"''${excludedPackages[@]}"
printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}"
excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf
exclude+='\|'"$excludedAlternates"
fi
exclude+='\)'
buildGoDir() {
local cmd="$1" dir="$2"
declare -a flags
flags+=(''${tags:+-tags=$(concatStringsSep "," tags)})
flags+=(''${ldflags:+-ldflags="''${ldflags[*]}"})
flags+=("-p" "$NIX_BUILD_CORES")
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
flags+=(-x)
fi
if [ "$cmd" = "test" ]; then
flags+=(-vet=off)
flags+=($checkFlags)
fi
local OUT
if ! OUT="$(go $cmd "''${flags[@]}" $dir 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
fi
fi
if [ -n "$OUT" ]; then
echo "$OUT" >&2
fi
return 0
}
getGoDirs() {
local -r type="$1"
# Support structuredAttrs, they are not space separated
local -a subPackagesArray
concatTo subPackagesArray subPackages
# Outputs each element prefixed with './' if the array is not empty
if [[ ''${#subPackagesArray[@]} -gt 0 ]]; then
echo "''${subPackagesArray[*]/#/./}"
else
find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique | grep -v "$exclude"
fi
}
if [ -z "$enableParallelBuilding" ]; then
export NIX_BUILD_CORES=1
fi
for pkg in $(getGoDirs ""); do
${
if buildTestBinaries then
''
echo "Building test binary for $pkg"
buildGoDir "test -c -o $GOPATH/bin/" "$pkg"
''
else
''
echo "Building subPackage $pkg"
buildGoDir install "$pkg"
''
}
done
''
+ lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
# normalize cross-compiled builds w.r.t. native builds
(
dir=$GOPATH/bin/''${GOOS}_''${GOARCH}
if [[ -n "$(shopt -s nullglob; echo $dir/*)" ]]; then
mv $dir/* $dir/..
fi
if [[ -d $dir ]]; then
rmdir $dir
fi
)
''
+ ''
runHook postBuild
''
);
doCheck = args.doCheck or (!buildTestBinaries);
checkPhase =
args.checkPhase or ''
runHook preCheck
# We do not set trimpath for tests, in case they reference test assets
export GOFLAGS=''${GOFLAGS//-trimpath/}
for pkg in $(getGoDirs test); do
buildGoDir test "$pkg"
done
runHook postCheck
'';
installPhase =
args.installPhase or ''
runHook preInstall
mkdir -p $out
dir="$GOPATH/bin"
[ -e "$dir" ] && cp -r $dir $out
runHook postInstall
'';
strictDeps = true;
inherit allowGoReference;
disallowedReferences = lib.optional (!finalAttrs.allowGoReference) go;
passthru = {
inherit go;
# Canonicallize `overrideModAttrs` as an attribute overlay.
# `passthru.overrideModAttrs` will be overridden
# when users want to override `goModules`.
overrideModAttrs = lib.toExtension overrideModAttrs;
}
// passthru;
meta = {
# Add default meta information.
platforms = go.meta.platforms or lib.platforms.all;
}
// meta;
};
}
|