1 { go, cacert, git, lib, stdenv }:
3 { name ? "${args'.pname}-${args'.version}"
4 # The source used to build the derivation.
6 # Native build inputs used for the derivation.
7 , nativeBuildInputs ? [ ]
11 # A function to override the `goModules` derivation.
12 , overrideModAttrs ? (finalAttrs: previousAttrs: { })
14 # Directory to the `go.mod` and `go.sum` relative to the `src`.
17 # The SRI hash of the vendored dependencies.
18 # If `vendorHash` is `null`, no dependencies are fetched and
19 # the build relies on the vendor folder within the source.
20 , vendorHash ? throw (
21 if args'?vendorSha256 then
22 "buildGoModule: Expect vendorHash instead of vendorSha256"
24 "buildGoModule: vendorHash is missing"
27 # Whether to delete the vendor folder supplied with the source.
28 , deleteVendor ? false
30 # Whether to fetch (go mod download) and proxy the vendor directory.
31 # This is useful if your code depends on c code and go mod tidy does not
32 # include the needed sources to build or if any dependency has case-insensitive
33 # conflicts which will produce platform dependant `vendorHash` checksums.
36 # We want parallel builds by default.
37 , enableParallelBuilding ? true
39 # Do not enable this without good reason
40 # IE: programs coupled with the compiler.
41 , allowGoReference ? false
43 # Go env. variable to enable CGO.
44 , CGO_ENABLED ? go.CGO_ENABLED
46 # Meta data for the final derivation.
49 # Not needed with `buildGoModule`.
57 # Needed for buildFlags{,Array} warning
58 , buildFlags ? "" # deprecated
59 , buildFlagsArray ? "" # deprecated
64 assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`";
67 args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" ];
70 GOTOOLCHAIN = "local";
73 (stdenv.mkDerivation (finalAttrs:
77 inherit modRoot vendorHash deleteVendor proxyVendor;
78 goModules = if (finalAttrs.vendorHash == null) then "" else
79 (stdenv.mkDerivation {
80 name = "${finalAttrs.name or "${finalAttrs.pname}-${finalAttrs.version}"}-go-modules";
82 nativeBuildInputs = (finalAttrs.nativeBuildInputs or [ ]) ++ [ go git cacert ];
84 inherit (finalAttrs) src modRoot;
85 inherit (go) GOOS GOARCH;
86 inherit GO111MODULE GOTOOLCHAIN;
88 # The following inheritance behavior is not trivial to expect, and some may
89 # argue it's not ideal. Changing it may break vendor hashes in Nixpkgs and
90 # out in the wild. In anycase, it's documented in:
91 # doc/languages-frameworks/go.section.md.
92 prePatch = finalAttrs.prePatch or "";
93 patches = finalAttrs.patches or [ ];
94 patchFlags = finalAttrs.patchFlags or [ ];
95 postPatch = finalAttrs.postPatch or "";
96 preBuild = finalAttrs.preBuild or "";
97 postBuild = finalAttrs.modPostBuild or "";
98 sourceRoot = finalAttrs.sourceRoot or "";
99 setSourceRoot = finalAttrs.setSourceRoot or "";
100 env = finalAttrs.env or { };
102 impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
108 configurePhase = args.modConfigurePhase or ''
110 export GOCACHE=$TMPDIR/go-cache
111 export GOPATH="$TMPDIR/go"
113 runHook postConfigure
116 buildPhase = args.modBuildPhase or (''
118 '' + lib.optionalString finalAttrs.deleteVendor ''
119 if [ ! -d vendor ]; then
120 echo "vendor folder does not exist, 'deleteVendor' is not needed"
126 if [ -d vendor ]; then
127 echo "vendor folder exists, please set 'vendorHash = null;' in your expression"
131 export GIT_SSL_CAINFO=$NIX_SSL_CERT_FILE
132 ${if finalAttrs.proxyVendor then ''
133 mkdir -p "''${GOPATH}/pkg/mod/cache/download"
136 if (( "''${NIX_DEBUG:-0}" >= 1 )); then
137 goModVendorFlags+=(-v)
139 go mod vendor "''${goModVendorFlags[@]}"
147 installPhase = args.modInstallPhase or ''
150 ${if finalAttrs.proxyVendor then ''
151 rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb"
152 cp -r --reflink=auto "''${GOPATH}/pkg/mod/cache/download" $out
154 cp -r --reflink=auto vendor $out
157 if ! [ "$(ls -A $out)" ]; then
158 echo "vendor folder is empty, please set 'vendorHash = null;' in your expression"
167 outputHashMode = "recursive";
168 outputHash = finalAttrs.vendorHash;
169 # Handle empty `vendorHash`; avoid error:
170 # empty hash requires explicit hash algorithm.
171 outputHashAlgo = if finalAttrs.vendorHash == "" then "sha256" else null;
172 # in case an overlay clears passthru by accident, don't fail evaluation
173 }).overrideAttrs (finalAttrs.passthru.overrideModAttrs or overrideModAttrs);
175 nativeBuildInputs = [ go ] ++ nativeBuildInputs;
177 inherit (go) GOOS GOARCH;
180 ++ lib.warnIf (lib.any (lib.hasPrefix "-mod=") GOFLAGS) "use `proxyVendor` to control Go module/vendor behavior instead of setting `-mod=` in GOFLAGS"
181 (lib.optional (!finalAttrs.proxyVendor) "-mod=vendor")
182 ++ lib.warnIf (builtins.elem "-trimpath" GOFLAGS) "`-trimpath` is added by default to GOFLAGS by buildGoModule when allowGoReference isn't set to true"
183 (lib.optional (!allowGoReference) "-trimpath");
184 inherit CGO_ENABLED enableParallelBuilding GO111MODULE GOTOOLCHAIN;
186 # If not set to an explicit value, set the buildid empty for reproducibility.
187 ldflags = ldflags ++ lib.optional (!lib.any (lib.hasPrefix "-buildid=") ldflags) "-buildid=";
189 configurePhase = args.configurePhase or (''
192 export GOCACHE=$TMPDIR/go-cache
193 export GOPATH="$TMPDIR/go"
197 '' + lib.optionalString (finalAttrs.vendorHash != null) ''
198 ${if finalAttrs.proxyVendor then ''
199 export GOPROXY="file://$goModules"
202 cp -r --reflink=auto "$goModules" vendor
206 # currently pie is only enabled by default in pkgsMusl
207 # this will respect the `hardening{Disable,Enable}` flags if set
208 if [[ $NIX_HARDENING_ENABLE =~ "pie" ]]; then
209 export GOFLAGS="-buildmode=pie $GOFLAGS"
212 runHook postConfigure
215 buildPhase = args.buildPhase or (
216 lib.warnIf (buildFlags != "" || buildFlagsArray != "")
217 "`buildFlags`/`buildFlagsArray` are deprecated and will be removed in the 24.11 release. Use the `ldflags` and/or `tags` attributes instead of `buildFlags`/`buildFlagsArray`"
218 lib.warnIf (builtins.elem "-buildid=" ldflags)
219 "`-buildid=` is set by default as ldflag by buildGoModule"
223 exclude='\(/_\|examples\|Godeps\|testdata'
224 if [[ -n "$excludedPackages" ]]; then
225 IFS=' ' read -r -a excludedArr <<<$excludedPackages
226 printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}"
227 excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf
228 exclude+='\|'"$excludedAlternates"
233 local cmd="$1" dir="$2"
235 declare -ga buildFlagsArray
237 flags+=($buildFlags "''${buildFlagsArray[@]}")
238 flags+=(''${tags:+-tags=''${tags// /,}})
239 flags+=(''${ldflags:+-ldflags="$ldflags"})
240 flags+=("-p" "$NIX_BUILD_CORES")
242 if [ "$cmd" = "test" ]; then
248 if ! OUT="$(go $cmd "''${flags[@]}" $dir 2>&1)"; then
249 if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
254 if [ -n "$OUT" ]; then
263 if [ -n "$subPackages" ]; then
264 echo "$subPackages" | sed "s,\(^\| \),\1./,g"
266 find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique | grep -v "$exclude"
270 if (( "''${NIX_DEBUG:-0}" >= 1 )); then
271 buildFlagsArray+=(-x)
274 if [ -z "$enableParallelBuilding" ]; then
275 export NIX_BUILD_CORES=1
277 for pkg in $(getGoDirs ""); do
278 echo "Building subPackage $pkg"
279 buildGoDir install "$pkg"
281 '' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
282 # normalize cross-compiled builds w.r.t. native builds
284 dir=$GOPATH/bin/${go.GOOS}_${go.GOARCH}
285 if [[ -n "$(shopt -s nullglob; echo $dir/*)" ]]; then
288 if [[ -d $dir ]]; then
296 doCheck = args.doCheck or true;
297 checkPhase = args.checkPhase or ''
299 # We do not set trimpath for tests, in case they reference test assets
300 export GOFLAGS=''${GOFLAGS//-trimpath/}
302 for pkg in $(getGoDirs test); do
303 buildGoDir test "$pkg"
309 installPhase = args.installPhase or ''
314 [ -e "$dir" ] && cp -r $dir $out
321 disallowedReferences = lib.optional (!allowGoReference) go;
325 # Canonicallize `overrideModAttrs` as an attribute overlay.
326 # `passthru.overrideModAttrs` will be overridden
327 # when users want to override `goModules`.
328 overrideModAttrs = lib.toExtension overrideModAttrs;
332 # Add default meta information.
333 platforms = go.meta.platforms or lib.platforms.all;