buildGoModule: add proxyVendor

This commit is contained in:
zowoq 2021-07-25 19:33:20 +10:00
parent b60dde0c1e
commit a4461b97c6
2 changed files with 23 additions and 5 deletions

View file

@ -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;` - `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. - `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 ```nix
pet = buildGoModule rec { pet = buildGoModule rec {

View file

@ -29,6 +29,10 @@
# Whether to run the vend tool to regenerate the vendor directory. # Whether to run the vend tool to regenerate the vendor directory.
# This is useful if any dependency contain C files. # This is useful if any dependency contain C files.
, runVend ? false , 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 # We want parallel builds by default
, enableParallelBuilding ? true , enableParallelBuilding ? true
@ -46,6 +50,8 @@
with builtins; with builtins;
assert (runVend == true && proxyVendor == true) -> throw "can't use `runVend` and `proxyVendor` together";
assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`"; assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`";
let let
@ -97,6 +103,9 @@ let
${if runVend then '' ${if runVend then ''
echo "running 'vend' to rewrite vendor folder" echo "running 'vend' to rewrite vendor folder"
${vend}/bin/vend ${vend}/bin/vend
'' else if proxyVendor then ''
mkdir -p "''${GOPATH}/pkg/mod/cache/download"
go mod download
'' else '' '' else ''
go mod vendor go mod vendor
''} ''}
@ -109,8 +118,12 @@ let
installPhase = args.modInstallPhase or '' installPhase = args.modInstallPhase or ''
runHook preInstall 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 cp -r --reflink=auto vendor $out
''}
runHook postInstall runHook postInstall
''; '';
@ -130,7 +143,7 @@ let
inherit (go) GOOS GOARCH; inherit (go) GOOS GOARCH;
GO111MODULE = "on"; GO111MODULE = "on";
GOFLAGS = [ "-mod=vendor" ] ++ lib.optionals (!allowGoReference) [ "-trimpath" ]; GOFLAGS = lib.optionals (!proxyVendor) [ "-mod=vendor" ] ++ lib.optionals (!allowGoReference) [ "-trimpath" ];
configurePhase = args.configurePhase or '' configurePhase = args.configurePhase or ''
runHook preConfigure runHook preConfigure
@ -138,11 +151,15 @@ let
export GOCACHE=$TMPDIR/go-cache export GOCACHE=$TMPDIR/go-cache
export GOPATH="$TMPDIR/go" export GOPATH="$TMPDIR/go"
export GOSUMDB=off export GOSUMDB=off
export GOPROXY=off
cd "$modRoot" cd "$modRoot"
'' + lib.optionalString (go-modules != "") '' '' + lib.optionalString (go-modules != "") ''
rm -rf vendor ${if proxyVendor then ''
cp -r --reflink=auto ${go-modules} vendor export GOPROXY=file://${go-modules}
'' else ''
export GOPROXY=off
rm -rf vendor
cp -r --reflink=auto ${go-modules} vendor
''}
'' + '' '' + ''
runHook postConfigure runHook postConfigure