-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
buildGoModule: Fix overriding with overlay-style stdenv #225051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| , patches ? [ ] | ||
|
|
||
| # A function to override the goModules derivation | ||
| , overrideModAttrs ? (_oldAttrs: { }) | ||
| , overrideModAttrs ? (finalAttrs: previousAttrs: { }) | ||
|
|
||
| # path to go.mod and go.sum directory | ||
| , modRoot ? "./" | ||
|
|
@@ -58,34 +58,54 @@ | |
| assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`"; | ||
ShamrockLee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let | ||
| args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" "vendorHash" ]; | ||
doronbehar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" ]; | ||
|
|
||
| GO111MODULE = "on"; | ||
| GOTOOLCHAIN = "local"; | ||
|
|
||
| goModules = if (vendorHash == null) then "" else | ||
| toExtension = | ||
| overlay0: | ||
| if lib.isFunction overlay0 then | ||
| final: prev: | ||
| if lib.isFunction (overlay0 prev) then | ||
| # `overlay0` is `final: prev: { ... }` | ||
| overlay0 final prev | ||
| else | ||
| # `overlay0` is `prev: { ... }` | ||
| overlay0 prev | ||
| else | ||
| # `overlay0` is `{ ... }` | ||
| final: prev: overlay0; | ||
|
|
||
| in | ||
| (stdenv.mkDerivation (finalAttrs: | ||
| args | ||
| // { | ||
|
|
||
| inherit modRoot vendorHash deleteVendor proxyVendor; | ||
| goModules = if (finalAttrs.vendorHash == null) then "" else | ||
| (stdenv.mkDerivation { | ||
| name = "${name}-go-modules"; | ||
| name = "${finalAttrs.name or "${finalAttrs.pname}-${finalAttrs.version}"}-go-modules"; | ||
|
|
||
| nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ go git cacert ]; | ||
| nativeBuildInputs = (finalAttrs.nativeBuildInputs or [ ]) ++ [ go git cacert ]; | ||
|
|
||
| inherit (args) src; | ||
| inherit (finalAttrs) src modRoot; | ||
| inherit (go) GOOS GOARCH; | ||
| inherit GO111MODULE GOTOOLCHAIN; | ||
|
|
||
| # The following inheritence 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 = args.prePatch or ""; | ||
| patches = args.patches or [ ]; | ||
| patchFlags = args.patchFlags or [ ]; | ||
| postPatch = args.postPatch or ""; | ||
| preBuild = args.preBuild or ""; | ||
| postBuild = args.modPostBuild or ""; | ||
| sourceRoot = args.sourceRoot or ""; | ||
| setSourceRoot = args.setSourceRoot or ""; | ||
| env = args.env or { }; | ||
| 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" | ||
|
|
@@ -97,13 +117,13 @@ let | |
| runHook preConfigure | ||
| export GOCACHE=$TMPDIR/go-cache | ||
| export GOPATH="$TMPDIR/go" | ||
| cd "${modRoot}" | ||
| cd "$modRoot" | ||
| runHook postConfigure | ||
| ''; | ||
|
|
||
| buildPhase = args.modBuildPhase or ('' | ||
| runHook preBuild | ||
| '' + lib.optionalString deleteVendor '' | ||
| '' + lib.optionalString finalAttrs.deleteVendor '' | ||
| if [ ! -d vendor ]; then | ||
| echo "vendor folder does not exist, 'deleteVendor' is not needed" | ||
| exit 10 | ||
|
|
@@ -116,7 +136,7 @@ let | |
| exit 10 | ||
| fi | ||
|
|
||
| ${if proxyVendor then '' | ||
| ${if finalAttrs.proxyVendor then '' | ||
| mkdir -p "''${GOPATH}/pkg/mod/cache/download" | ||
| go mod download | ||
| '' else '' | ||
|
|
@@ -134,7 +154,7 @@ let | |
| installPhase = args.modInstallPhase or '' | ||
| runHook preInstall | ||
|
|
||
| ${if proxyVendor then '' | ||
| ${if finalAttrs.proxyVendor then '' | ||
| rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb" | ||
| cp -r --reflink=auto "''${GOPATH}/pkg/mod/cache/download" $out | ||
| '' else '' | ||
|
|
@@ -152,20 +172,19 @@ let | |
| dontFixup = true; | ||
|
|
||
| outputHashMode = "recursive"; | ||
| outputHash = vendorHash; | ||
| outputHash = finalAttrs.vendorHash; | ||
| # Handle empty vendorHash; avoid | ||
| # error: empty hash requires explicit hash algorithm | ||
| outputHashAlgo = if vendorHash == "" then "sha256" else null; | ||
| }).overrideAttrs overrideModAttrs; | ||
| outputHashAlgo = if finalAttrs.vendorHash == "" then "sha256" else null; | ||
| }).overrideAttrs finalAttrs.passthru.overrideModAttrs; | ||
|
|
||
| package = stdenv.mkDerivation (args // { | ||
| nativeBuildInputs = [ go ] ++ nativeBuildInputs; | ||
|
|
||
| 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" | ||
| (lib.optional (!proxyVendor) "-mod=vendor") | ||
| (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 (!allowGoReference) "-trimpath"); | ||
| inherit CGO_ENABLED enableParallelBuilding GO111MODULE GOTOOLCHAIN; | ||
|
|
@@ -181,12 +200,12 @@ let | |
| export GOPROXY=off | ||
| export GOSUMDB=off | ||
| cd "$modRoot" | ||
| '' + lib.optionalString (vendorHash != null) '' | ||
| ${if proxyVendor then '' | ||
| export GOPROXY=file://${goModules} | ||
| '' + lib.optionalString (finalAttrs.vendorHash != null) '' | ||
| ${if finalAttrs.proxyVendor then '' | ||
| export GOPROXY="file://$goModules" | ||
| '' else '' | ||
| rm -rf vendor | ||
| cp -r --reflink=auto ${goModules} vendor | ||
| cp -r --reflink=auto "$goModules" vendor | ||
| ''} | ||
| '' + '' | ||
|
|
||
|
|
@@ -307,12 +326,17 @@ let | |
|
|
||
| disallowedReferences = lib.optional (!allowGoReference) go; | ||
|
|
||
| passthru = passthru // { inherit go goModules vendorHash; }; | ||
| passthru = { | ||
| inherit go; | ||
| # Canonicallize `overrideModAttrs` as an attribute overlay. | ||
| # `passthru.overrideModAttrs` will be overridden | ||
| # when users want to override `goModules`. | ||
| overrideModAttrs = toExtension overrideModAttrs; | ||
|
||
| } // passthru; | ||
|
|
||
| meta = { | ||
| # Add default meta information | ||
| platforms = go.meta.platforms or lib.platforms.all; | ||
| } // meta; | ||
| }); | ||
| in | ||
| package | ||
| } | ||
| )) | ||
Uh oh!
There was an error while loading. Please reload this page.