buildGoModule: format

Co-authored-by: Yueh-Shun Li <shamrocklee@posteo.net>
This commit is contained in:
zowoq 2023-05-28 07:46:45 +10:00
parent 2435bbb9a5
commit 304940d98e

View file

@ -2,51 +2,52 @@
{ name ? "${args'.pname}-${args'.version}" { name ? "${args'.pname}-${args'.version}"
, src , src
, nativeBuildInputs ? [] , nativeBuildInputs ? [ ]
, passthru ? {} , passthru ? { }
, patches ? [] , patches ? [ ]
# Go tags, passed to go via -tag # Go tags, passed to go via -tag
, tags ? [] , tags ? [ ]
# A function to override the go-modules derivation # A function to override the go-modules derivation
, overrideModAttrs ? (_oldAttrs : {}) , overrideModAttrs ? (_oldAttrs: { })
# path to go.mod and go.sum directory # path to go.mod and go.sum directory
, modRoot ? "./" , modRoot ? "./"
# vendorHash is the SRI hash of the vendored dependencies # vendorHash is the SRI hash of the vendored dependencies
# #
# if vendorHash is null, then we won't fetch any dependencies and # if vendorHash is null, then we won't fetch any dependencies and
# rely on the vendor folder within the source. # rely on the vendor folder within the source.
, vendorHash ? args'.vendorSha256 or (throw "buildGoModule: vendorHash is missing") , vendorHash ? args'.vendorSha256 or (throw "buildGoModule: vendorHash is missing")
# Whether to delete the vendor folder supplied with the source. # Whether to delete the vendor folder supplied with the source.
, deleteVendor ? false , deleteVendor ? false
# Whether to fetch (go mod download) and proxy the vendor directory. # Whether to fetch (go mod download) and proxy the vendor directory.
# This is useful if your code depends on c code and go mod tidy does not # This is useful if your code depends on c code and go mod tidy does not
# include the needed sources to build or if any dependency has case-insensitive # include the needed sources to build or if any dependency has case-insensitive
# conflicts which will produce platform dependant `vendorHash` checksums. # conflicts which will produce platform dependant `vendorHash` checksums.
, proxyVendor ? false , proxyVendor ? false
# We want parallel builds by default # We want parallel builds by default
, enableParallelBuilding ? true , enableParallelBuilding ? true
# Do not enable this without good reason # Do not enable this without good reason
# IE: programs coupled with the compiler # IE: programs coupled with the compiler
, allowGoReference ? false , allowGoReference ? false
, CGO_ENABLED ? go.CGO_ENABLED , CGO_ENABLED ? go.CGO_ENABLED
, meta ? {} , meta ? { }
# Not needed with buildGoModule # Not needed with buildGoModule
, goPackagePath ? "" , goPackagePath ? ""
# needed for buildFlags{,Array} warning # needed for buildFlags{,Array} warning
, buildFlags ? "" , buildFlags ? ""
, buildFlagsArray ? "" , buildFlagsArray ? ""
, ... }@args': , ...
}@args':
assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`"; assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`";
assert (args' ? vendorHash && args' ? vendorSha256) -> throw "both `vendorHash` and `vendorSha256` set. only one can be set."; assert (args' ? vendorHash && args' ? vendorSha256) -> throw "both `vendorHash` and `vendorSha256` set. only one can be set.";
@ -55,11 +56,10 @@ let
args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" "vendorHash" ]; args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" "vendorHash" ];
go-modules = if (vendorHash == null) then "" else go-modules = if (vendorHash == null) then "" else
(stdenv.mkDerivation { (stdenv.mkDerivation {
name = "${name}-go-modules"; name = "${name}-go-modules";
nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ go git cacert ]; nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ go git cacert ];
inherit (args) src; inherit (args) src;
inherit (go) GOOS GOARCH; inherit (go) GOOS GOARCH;
@ -69,8 +69,8 @@ let
# out in the wild. In anycase, it's documented in: # out in the wild. In anycase, it's documented in:
# doc/languages-frameworks/go.section.md # doc/languages-frameworks/go.section.md
prePatch = args.prePatch or ""; prePatch = args.prePatch or "";
patches = args.patches or []; patches = args.patches or [ ];
patchFlags = args.patchFlags or []; patchFlags = args.patchFlags or [ ];
postPatch = args.postPatch or ""; postPatch = args.postPatch or "";
preBuild = args.preBuild or ""; preBuild = args.preBuild or "";
postBuild = args.modPostBuild or ""; postBuild = args.modPostBuild or "";
@ -79,7 +79,9 @@ let
GO111MODULE = "on"; GO111MODULE = "on";
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND" "SOCKS_SERVER" "GOPROXY" "GIT_PROXY_COMMAND"
"SOCKS_SERVER"
"GOPROXY"
]; ];
configurePhase = args.modConfigurePhase or '' configurePhase = args.modConfigurePhase or ''
@ -105,15 +107,15 @@ let
exit 10 exit 10
fi fi
${if proxyVendor then '' ${if proxyVendor then ''
mkdir -p "''${GOPATH}/pkg/mod/cache/download" mkdir -p "''${GOPATH}/pkg/mod/cache/download"
go mod download go mod download
'' else '' '' else ''
if (( "''${NIX_DEBUG:-0}" >= 1 )); then if (( "''${NIX_DEBUG:-0}" >= 1 )); then
goModVendorFlags+=(-v) goModVendorFlags+=(-v)
fi fi
go mod vendor "''${goModVendorFlags[@]}" go mod vendor "''${goModVendorFlags[@]}"
''} ''}
mkdir -p vendor mkdir -p vendor
@ -123,12 +125,12 @@ let
installPhase = args.modInstallPhase or '' installPhase = args.modInstallPhase or ''
runHook preInstall runHook preInstall
${if proxyVendor then '' ${if proxyVendor then ''
rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb" rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb"
cp -r --reflink=auto "''${GOPATH}/pkg/mod/cache/download" $out cp -r --reflink=auto "''${GOPATH}/pkg/mod/cache/download" $out
'' else '' '' else ''
cp -r --reflink=auto vendor $out cp -r --reflink=auto vendor $out
''} ''}
if ! [ "$(ls -A $out)" ]; then if ! [ "$(ls -A $out)" ]; then
echo "vendor folder is empty, please set 'vendorHash = null;' in your expression" echo "vendor folder is empty, please set 'vendorHash = null;' in your expression"