diff --git a/doc/languages-frameworks/go.section.md b/doc/languages-frameworks/go.section.md index 8bcbbd323e3..45d85f1f99e 100644 --- a/doc/languages-frameworks/go.section.md +++ b/doc/languages-frameworks/go.section.md @@ -13,6 +13,7 @@ In the following is an example expression using `buildGoModule`, the following a - `vendorSha256`: is the hash of the output of the intermediate fetcher derivation. `vendorSha256` can also take `null` as an input. When `null` is used as a value, rather than fetching the dependencies and vendoring them, we use the vendoring included within the source repo. If you'd like to not have to update this field on dependency changes, run `go mod vendor` in your source repo and set `vendorSha256 = null;` - `runVend`: runs the vend command to generate 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. +- `proxyVendor`: Fetches (go mod download) and proxies the vendor directory. This is useful if any dependency has case-insensitive conflicts which will produce platform dependant `vendorSha256` checksums. ```nix pet = buildGoModule rec { diff --git a/pkgs/development/go-modules/generic/default.nix b/pkgs/development/go-modules/generic/default.nix index 58747d11a59..5ab5818a6c1 100644 --- a/pkgs/development/go-modules/generic/default.nix +++ b/pkgs/development/go-modules/generic/default.nix @@ -29,6 +29,10 @@ # Whether to run the vend tool to regenerate the vendor directory. # This is useful if any dependency contain C files. , runVend ? false +# Whether to fetch (go mod download) and proxy the vendor directory. +# This is useful if any dependency has case-insensitive conflicts +# which will produce platform dependant `vendorSha256` checksums. +, proxyVendor ? false # We want parallel builds by default , enableParallelBuilding ? true @@ -46,6 +50,8 @@ with builtins; +assert (runVend == true && proxyVendor == true) -> throw "can't use `runVend` and `proxyVendor` together"; + assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`"; let @@ -97,6 +103,9 @@ let ${if runVend then '' echo "running 'vend' to rewrite vendor folder" ${vend}/bin/vend + '' else if proxyVendor then '' + mkdir -p "''${GOPATH}/pkg/mod/cache/download" + go mod download '' else '' go mod vendor ''} @@ -109,8 +118,12 @@ let installPhase = args.modInstallPhase or '' runHook preInstall - # remove cached lookup results and tiles + ${if 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 + ''} runHook postInstall ''; @@ -130,7 +143,7 @@ let inherit (go) GOOS GOARCH; GO111MODULE = "on"; - GOFLAGS = [ "-mod=vendor" ] ++ lib.optionals (!allowGoReference) [ "-trimpath" ]; + GOFLAGS = lib.optionals (!proxyVendor) [ "-mod=vendor" ] ++ lib.optionals (!allowGoReference) [ "-trimpath" ]; configurePhase = args.configurePhase or '' runHook preConfigure @@ -138,11 +151,15 @@ let export GOCACHE=$TMPDIR/go-cache export GOPATH="$TMPDIR/go" export GOSUMDB=off - export GOPROXY=off cd "$modRoot" '' + lib.optionalString (go-modules != "") '' - rm -rf vendor - cp -r --reflink=auto ${go-modules} vendor + ${if proxyVendor then '' + export GOPROXY=file://${go-modules} + '' else '' + export GOPROXY=off + rm -rf vendor + cp -r --reflink=auto ${go-modules} vendor + ''} '' + '' runHook postConfigure