go-modules: Augment builds w/ vendor src

This is done in response to complaints that the module format is not
human readable. The vendor source blob is flat files and should be
extremely readable.
This commit is contained in:
Colin L Rice 2020-04-29 23:08:21 -04:00 committed by Jörg Thalheim
parent ce9ed9baed
commit 9761128d2d
No known key found for this signature in database
GPG Key ID: 003F2096411B5F92

View File

@ -13,12 +13,15 @@
# path to go.mod and go.sum directory # path to go.mod and go.sum directory
, modRoot ? "./" , modRoot ? "./"
# modSha256 is the sha256 of the vendored dependencies # vendorSha256 is the sha256 of the vendored dependencies
# #
# CAUTION: if `null` is used as a value, the derivation won't be a # if vendorSha256 is null, then we won't fetch any dependencies and
# fixed-output derivation but disable the build sandbox instead. Don't use # rely on the vendor folder within the source.
# this in nixpkgs as Hydra won't build those packages. , vendorSha256 ? null
, modSha256 # Whether to delete the vendor folder supplied with the source.
, deleteVendor ? false
, modSha256 ? null
# We want parallel builds by default # We want parallel builds by default
, enableParallelBuilding ? true , enableParallelBuilding ? true
@ -37,21 +40,26 @@
with builtins; with builtins;
let let
args = removeAttrs args' [ "overrideModAttrs" "modSha256" "disabled" ]; args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" "disabled" ];
removeReferences = [ ] ++ lib.optional (!allowGoReference) go; removeReferences = [ ] ++ lib.optional (!allowGoReference) go;
removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}''; removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}'';
go-modules = go.stdenv.mkDerivation (let modArgs = { deleteFlag = if deleteVendor then "true" else "false";
go-modules = if vendorSha256 != null then go.stdenv.mkDerivation (let modArgs = {
name = "${name}-go-modules"; name = "${name}-go-modules";
nativeBuildInputs = [ go git cacert ]; nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ go git cacert ];
inherit (args) src; inherit (args) src;
inherit (go) GOOS GOARCH; inherit (go) GOOS GOARCH;
patches = args.patches or []; patches = args.patches or [];
preBuild = args.preBuild or "";
sourceRoot = args.sourceRoot or "";
GO111MODULE = "on"; GO111MODULE = "on";
@ -64,7 +72,6 @@ let
export GOCACHE=$TMPDIR/go-cache export GOCACHE=$TMPDIR/go-cache
export GOPATH="$TMPDIR/go" export GOPATH="$TMPDIR/go"
mkdir -p "''${GOPATH}/pkg/mod/cache/download"
cd "${modRoot}" cd "${modRoot}"
runHook postConfigure runHook postConfigure
''; '';
@ -72,7 +79,16 @@ let
buildPhase = args.modBuildPhase or '' buildPhase = args.modBuildPhase or ''
runHook preBuild runHook preBuild
go mod download if [ ${deleteFlag} == "true" ]; then
rm -rf vendor
fi
if [ -e vendor ]; then
echo "vendor folder exists, please set 'vendorSha256=null;' or 'deleteVendor=true;' in your expression"
exit 10
fi
go mod vendor
mkdir -p vendor
runHook postBuild runHook postBuild
''; '';
@ -81,23 +97,19 @@ let
runHook preInstall runHook preInstall
# remove cached lookup results and tiles # remove cached lookup results and tiles
rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb" cp -r --reflink=auto vendor $out
cp -r "''${GOPATH}/pkg/mod/cache/download" $out
runHook postInstall runHook postInstall
''; '';
dontFixup = true; dontFixup = true;
}; in modArgs // ( }; in modArgs // (
if modSha256 == null then
{ __noChroot = true; }
else
{ {
outputHashMode = "recursive"; outputHashMode = "recursive";
outputHashAlgo = "sha256"; outputHashAlgo = "sha256";
outputHash = modSha256; outputHash = vendorSha256;
} }
) // overrideModAttrs modArgs); ) // overrideModAttrs modArgs) else "";
package = go.stdenv.mkDerivation (args // { package = go.stdenv.mkDerivation (args // {
nativeBuildInputs = [ removeReferencesTo go ] ++ nativeBuildInputs; nativeBuildInputs = [ removeReferencesTo go ] ++ nativeBuildInputs;
@ -105,6 +117,7 @@ let
inherit (go) GOOS GOARCH; inherit (go) GOOS GOARCH;
GO111MODULE = "on"; GO111MODULE = "on";
GOFLAGS = "-mod=vendor";
configurePhase = args.configurePhase or '' configurePhase = args.configurePhase or ''
runHook preConfigure runHook preConfigure
@ -112,9 +125,12 @@ 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=file://${go-modules} export GOPROXY=off
cd "$modRoot" cd "$modRoot"
if [ -n "${go-modules}" ]; then
rm -rf vendor
ln -s ${go-modules} vendor
fi
runHook postConfigure runHook postConfigure
''; '';
@ -212,7 +228,7 @@ let
disallowedReferences = lib.optional (!allowGoReference) go; disallowedReferences = lib.optional (!allowGoReference) go;
passthru = passthru // { inherit go go-modules modSha256; }; passthru = passthru // { inherit go go-modules vendorSha256 ; };
meta = { meta = {
# Add default meta information # Add default meta information
@ -225,5 +241,7 @@ let
}); });
in if disabled then in if disabled then
throw "${package.name} not supported for go ${go.meta.branch}" throw "${package.name} not supported for go ${go.meta.branch}"
else if modSha256 != null then
throw "${package.name} should use vendorSha256 not modSha256"
else else
package package