2022-01-07 00:01:39 +01:00
|
|
|
{ go, cacert, git, lib, stdenv }:
|
2019-03-08 20:17:05 +01:00
|
|
|
|
2019-03-17 17:29:20 +01:00
|
|
|
{ name ? "${args'.pname}-${args'.version}"
|
2019-03-08 20:17:05 +01:00
|
|
|
, src
|
|
|
|
, buildInputs ? []
|
|
|
|
, nativeBuildInputs ? []
|
|
|
|
, passthru ? {}
|
|
|
|
, patches ? []
|
|
|
|
|
2021-06-03 17:45:04 +02:00
|
|
|
# Go linker flags, passed to go via -ldflags
|
|
|
|
, ldflags ? []
|
|
|
|
|
2021-07-31 06:57:54 +02:00
|
|
|
# Go tags, passed to go via -tag
|
|
|
|
, tags ? []
|
|
|
|
|
2019-04-23 16:15:45 +02:00
|
|
|
# A function to override the go-modules derivation
|
|
|
|
, overrideModAttrs ? (_oldAttrs : {})
|
|
|
|
|
2019-12-15 23:58:16 +01:00
|
|
|
# path to go.mod and go.sum directory
|
|
|
|
, modRoot ? "./"
|
|
|
|
|
2022-07-29 00:46:48 +02:00
|
|
|
# vendorHash is the SRI hash of the vendored dependencies
|
2019-12-28 21:36:42 +01:00
|
|
|
#
|
2022-07-29 00:46:48 +02:00
|
|
|
# if vendorHash is null, then we won't fetch any dependencies and
|
2020-04-30 05:08:21 +02:00
|
|
|
# rely on the vendor folder within the source.
|
2022-07-29 00:46:48 +02:00
|
|
|
, vendorHash ? "_unset"
|
|
|
|
# same as vendorHash, but outputHashAlgo is hardcoded to sha256
|
|
|
|
# so regular base32 sha256 hashes work
|
|
|
|
, vendorSha256 ? "_unset"
|
2020-04-30 05:08:21 +02:00
|
|
|
# Whether to delete the vendor folder supplied with the source.
|
|
|
|
, deleteVendor ? false
|
2021-07-25 11:33:20 +02:00
|
|
|
# Whether to fetch (go mod download) and proxy the vendor directory.
|
2021-12-31 13:41:54 +01:00
|
|
|
# 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
|
2022-07-29 00:46:48 +02:00
|
|
|
# conflicts which will produce platform dependant `vendorHash` checksums.
|
2021-07-25 11:33:20 +02:00
|
|
|
, proxyVendor ? false
|
2020-04-30 05:08:21 +02:00
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
# We want parallel builds by default
|
|
|
|
, enableParallelBuilding ? true
|
|
|
|
|
|
|
|
# Do not enable this without good reason
|
|
|
|
# IE: programs coupled with the compiler
|
|
|
|
, allowGoReference ? false
|
|
|
|
|
2022-06-14 05:04:55 +02:00
|
|
|
, CGO_ENABLED ? go.CGO_ENABLED
|
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
, meta ? {}
|
|
|
|
|
2020-12-16 20:40:26 +01:00
|
|
|
# Not needed with buildGoModule
|
2021-02-15 18:53:04 +01:00
|
|
|
, goPackagePath ? ""
|
2020-12-16 20:40:26 +01:00
|
|
|
|
2021-08-23 06:55:51 +02:00
|
|
|
# needed for buildFlags{,Array} warning
|
2021-08-01 00:00:39 +02:00
|
|
|
, buildFlags ? ""
|
2021-08-23 06:55:51 +02:00
|
|
|
, buildFlagsArray ? ""
|
2021-08-01 00:00:39 +02:00
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
, ... }@args':
|
|
|
|
|
|
|
|
with builtins;
|
|
|
|
|
2021-02-15 18:53:04 +01:00
|
|
|
assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`";
|
2022-07-29 00:46:48 +02:00
|
|
|
assert (vendorSha256 == "_unset" && vendorHash == "_unset") -> throw "either `vendorHash` or `vendorSha256` is required";
|
|
|
|
assert (vendorSha256 != "_unset" && vendorHash != "_unset") -> throw "both `vendorHash` and `vendorSha256` set. only one can be set.";
|
2021-02-15 18:53:04 +01:00
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
let
|
2022-07-29 00:46:48 +02:00
|
|
|
hasAnyVendorHash = (vendorSha256 != null && vendorSha256 != "_unset") || (vendorHash != null && vendorHash != "_unset");
|
|
|
|
vendorHashType =
|
|
|
|
if hasAnyVendorHash then
|
|
|
|
if vendorSha256 != null && vendorSha256 != "_unset" then
|
|
|
|
"sha256"
|
|
|
|
else
|
|
|
|
"sri"
|
|
|
|
else
|
|
|
|
null;
|
|
|
|
|
|
|
|
args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" "vendorHash" ];
|
2019-03-08 20:17:05 +01:00
|
|
|
|
2022-07-29 00:46:48 +02:00
|
|
|
go-modules = if hasAnyVendorHash then stdenv.mkDerivation (let modArgs = {
|
2020-04-30 05:08:21 +02:00
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
name = "${name}-go-modules";
|
|
|
|
|
2020-04-30 05:08:21 +02:00
|
|
|
nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ go git cacert ];
|
2019-03-08 20:17:05 +01:00
|
|
|
|
|
|
|
inherit (args) src;
|
|
|
|
inherit (go) GOOS GOARCH;
|
|
|
|
|
|
|
|
patches = args.patches or [];
|
2021-09-19 16:51:14 +02:00
|
|
|
patchFlags = args.patchFlags or [];
|
2020-04-30 05:08:21 +02:00
|
|
|
preBuild = args.preBuild or "";
|
|
|
|
sourceRoot = args.sourceRoot or "";
|
2019-03-08 20:17:05 +01:00
|
|
|
|
|
|
|
GO111MODULE = "on";
|
|
|
|
|
|
|
|
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
|
2022-05-15 05:57:44 +02:00
|
|
|
"GIT_PROXY_COMMAND" "SOCKS_SERVER" "GOPROXY"
|
2019-03-08 20:17:05 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
configurePhase = args.modConfigurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
export GOCACHE=$TMPDIR/go-cache
|
|
|
|
export GOPATH="$TMPDIR/go"
|
2019-12-15 23:58:16 +01:00
|
|
|
cd "${modRoot}"
|
2019-03-08 20:17:05 +01:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = args.modBuildPhase or ''
|
|
|
|
runHook preBuild
|
2020-10-20 01:03:20 +02:00
|
|
|
'' + lib.optionalString (deleteVendor == true) ''
|
|
|
|
if [ ! -d vendor ]; then
|
|
|
|
echo "vendor folder does not exist, 'deleteVendor' is not needed"
|
|
|
|
exit 10
|
|
|
|
else
|
|
|
|
rm -rf vendor
|
2020-04-30 05:08:21 +02:00
|
|
|
fi
|
2020-10-20 01:03:20 +02:00
|
|
|
'' + ''
|
2020-08-12 12:14:58 +02:00
|
|
|
if [ -d vendor ]; then
|
2022-07-29 00:46:48 +02:00
|
|
|
echo "vendor folder exists, please set 'vendorHash = null;' or 'vendorSha256 = null;' in your expression"
|
2020-04-30 05:08:21 +02:00
|
|
|
exit 10
|
|
|
|
fi
|
2020-06-04 04:54:39 +02:00
|
|
|
|
2022-01-07 00:01:39 +01:00
|
|
|
${if proxyVendor then ''
|
2021-07-25 11:33:20 +02:00
|
|
|
mkdir -p "''${GOPATH}/pkg/mod/cache/download"
|
|
|
|
go mod download
|
2020-10-20 01:03:20 +02:00
|
|
|
'' else ''
|
2022-05-25 19:06:11 +02:00
|
|
|
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
|
|
|
|
goModVendorFlags+=(-v)
|
|
|
|
fi
|
|
|
|
go mod vendor "''${goModVendorFlags[@]}"
|
2020-10-20 01:03:20 +02:00
|
|
|
''}
|
|
|
|
|
2020-04-30 05:08:21 +02:00
|
|
|
mkdir -p vendor
|
2019-03-08 20:17:05 +01:00
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = args.modInstallPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
2021-07-25 11:33:20 +02:00
|
|
|
${if proxyVendor then ''
|
|
|
|
rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb"
|
|
|
|
cp -r --reflink=auto "''${GOPATH}/pkg/mod/cache/download" $out
|
|
|
|
'' else ''
|
2020-04-30 05:08:21 +02:00
|
|
|
cp -r --reflink=auto vendor $out
|
2021-07-25 11:33:20 +02:00
|
|
|
''}
|
2019-03-08 20:17:05 +01:00
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
dontFixup = true;
|
2019-12-28 21:36:42 +01:00
|
|
|
}; in modArgs // (
|
|
|
|
{
|
|
|
|
outputHashMode = "recursive";
|
2022-07-29 00:46:48 +02:00
|
|
|
} // (if (vendorHashType == "sha256") then {
|
2019-12-28 21:36:42 +01:00
|
|
|
outputHashAlgo = "sha256";
|
2020-04-30 05:08:21 +02:00
|
|
|
outputHash = vendorSha256;
|
2022-07-29 00:46:48 +02:00
|
|
|
} else {
|
|
|
|
outputHash = vendorHash;
|
|
|
|
}) // (lib.optionalAttrs (vendorHashType == "sri" && vendorHash == "") {
|
|
|
|
outputHashAlgo = "sha256";
|
|
|
|
})
|
2020-04-30 05:08:21 +02:00
|
|
|
) // overrideModAttrs modArgs) else "";
|
2019-03-08 20:17:05 +01:00
|
|
|
|
buildGoModule: fix cross-compilation with CGO_ENABLED=1
When building a go binary that's linking against some .so/.a, while
cross-compiling, we need to pass the correct compiler, otherwise
`go build` will fail with the not-so helpful error message:
```
gcc_arm64.S: Assembler messages:
gcc_arm64.S:28: Error: no such instruction: `stp x29,x30,[sp,'
gcc_arm64.S:32: Error: too many memory references for `mov'
gcc_arm64.S:34: Error: no such instruction: `stp x19,x20,[sp,'
gcc_arm64.S:37: Error: no such instruction: `stp x21,x22,[sp,'
gcc_arm64.S:40: Error: no such instruction: `stp x23,x24,[sp,'
gcc_arm64.S:43: Error: no such instruction: `stp x25,x26,[sp,'
gcc_arm64.S:46: Error: no such instruction: `stp x27,x28,[sp,'
gcc_arm64.S:50: Error: too many memory references for `mov'
gcc_arm64.S:51: Error: too many memory references for `mov'
gcc_arm64.S:52: Error: too many memory references for `mov'
gcc_arm64.S:54: Error: no such instruction: `blr x20'
gcc_arm64.S:55: Error: no such instruction: `blr x19'
gcc_arm64.S:57: Error: no such instruction: `ldp x27,x28,[sp,'
gcc_arm64.S:60: Error: no such instruction: `ldp x25,x26,[sp,'
gcc_arm64.S:63: Error: no such instruction: `ldp x23,x24,[sp,'
gcc_arm64.S:66: Error: no such instruction: `ldp x21,x22,[sp,'
gcc_arm64.S:69: Error: no such instruction: `ldp x19,x20,[sp,'
gcc_arm64.S:72: Error: no such instruction: `ldp x29,x30,[sp],'
```
2020-12-25 01:35:33 +01:00
|
|
|
package = stdenv.mkDerivation (args // {
|
2020-10-29 04:27:20 +01:00
|
|
|
nativeBuildInputs = [ go ] ++ nativeBuildInputs;
|
2019-03-08 20:17:05 +01:00
|
|
|
|
|
|
|
inherit (go) GOOS GOARCH;
|
|
|
|
|
|
|
|
GO111MODULE = "on";
|
2021-07-25 11:33:20 +02:00
|
|
|
GOFLAGS = lib.optionals (!proxyVendor) [ "-mod=vendor" ] ++ lib.optionals (!allowGoReference) [ "-trimpath" ];
|
2022-06-14 05:04:55 +02:00
|
|
|
inherit CGO_ENABLED;
|
2019-03-08 20:17:05 +01:00
|
|
|
|
|
|
|
configurePhase = args.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
|
|
|
|
export GOCACHE=$TMPDIR/go-cache
|
|
|
|
export GOPATH="$TMPDIR/go"
|
2022-03-16 12:40:15 +01:00
|
|
|
export GOPROXY=off
|
2019-12-01 22:20:00 +01:00
|
|
|
export GOSUMDB=off
|
2019-12-15 23:58:16 +01:00
|
|
|
cd "$modRoot"
|
2022-07-29 00:46:48 +02:00
|
|
|
'' + lib.optionalString hasAnyVendorHash ''
|
2021-07-25 11:33:20 +02:00
|
|
|
${if proxyVendor then ''
|
|
|
|
export GOPROXY=file://${go-modules}
|
|
|
|
'' else ''
|
|
|
|
rm -rf vendor
|
|
|
|
cp -r --reflink=auto ${go-modules} vendor
|
|
|
|
''}
|
2020-10-22 14:50:46 +02:00
|
|
|
'' + ''
|
2019-12-15 23:58:16 +01:00
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = args.buildPhase or ''
|
|
|
|
runHook preBuild
|
|
|
|
|
2022-02-25 23:06:28 +01:00
|
|
|
exclude='\(/_\|examples\|Godeps\|testdata'
|
|
|
|
if [[ -n "$excludedPackages" ]]; then
|
|
|
|
IFS=' ' read -r -a excludedArr <<<$excludedPackages
|
|
|
|
printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}"
|
|
|
|
excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf
|
2022-04-04 04:29:02 +02:00
|
|
|
exclude+='\|'"$excludedAlternates"
|
2022-02-25 23:06:28 +01:00
|
|
|
fi
|
|
|
|
exclude+='\)'
|
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
buildGoDir() {
|
2022-05-02 01:15:08 +02:00
|
|
|
local cmd="$1" dir="$2"
|
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
. $TMPDIR/buildFlagsArray
|
2022-05-02 01:15:08 +02:00
|
|
|
|
|
|
|
declare -a flags
|
|
|
|
flags+=($buildFlags "''${buildFlagsArray[@]}")
|
|
|
|
flags+=(''${tags:+-tags=${lib.concatStringsSep "," tags}})
|
|
|
|
flags+=(''${ldflags:+-ldflags="$ldflags"})
|
|
|
|
flags+=("-v" "-p" "$NIX_BUILD_CORES")
|
|
|
|
|
|
|
|
if [ "$cmd" = "test" ]; then
|
|
|
|
flags+=($checkFlags)
|
|
|
|
fi
|
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
local OUT
|
2022-05-02 01:15:08 +02:00
|
|
|
if ! OUT="$(go $cmd "''${flags[@]}" $dir 2>&1)"; then
|
2019-03-08 20:17:05 +01:00
|
|
|
if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
|
|
|
|
echo "$OUT" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ -n "$OUT" ]; then
|
|
|
|
echo "$OUT" >&2
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
getGoDirs() {
|
|
|
|
local type;
|
|
|
|
type="$1"
|
|
|
|
if [ -n "$subPackages" ]; then
|
2019-06-30 07:22:02 +02:00
|
|
|
echo "$subPackages" | sed "s,\(^\| \),\1./,g"
|
2019-03-08 20:17:05 +01:00
|
|
|
else
|
2022-04-08 20:21:45 +02:00
|
|
|
find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique | grep -v "$exclude"
|
2019-03-08 20:17:05 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
|
|
|
|
buildFlagsArray+=(-x)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ''${#buildFlagsArray[@]} -ne 0 ]; then
|
|
|
|
declare -p buildFlagsArray > $TMPDIR/buildFlagsArray
|
|
|
|
else
|
|
|
|
touch $TMPDIR/buildFlagsArray
|
|
|
|
fi
|
|
|
|
if [ -z "$enableParallelBuilding" ]; then
|
|
|
|
export NIX_BUILD_CORES=1
|
|
|
|
fi
|
|
|
|
for pkg in $(getGoDirs ""); do
|
2019-06-30 07:22:02 +02:00
|
|
|
echo "Building subPackage $pkg"
|
2019-03-08 20:17:05 +01:00
|
|
|
buildGoDir install "$pkg"
|
|
|
|
done
|
|
|
|
'' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
|
|
|
|
# normalize cross-compiled builds w.r.t. native builds
|
|
|
|
(
|
|
|
|
dir=$GOPATH/bin/${go.GOOS}_${go.GOARCH}
|
|
|
|
if [[ -n "$(shopt -s nullglob; echo $dir/*)" ]]; then
|
|
|
|
mv $dir/* $dir/..
|
|
|
|
fi
|
|
|
|
if [[ -d $dir ]]; then
|
|
|
|
rmdir $dir
|
|
|
|
fi
|
|
|
|
)
|
|
|
|
'' + ''
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
2020-08-04 02:35:19 +02:00
|
|
|
doCheck = args.doCheck or true;
|
2019-03-08 20:17:05 +01:00
|
|
|
checkPhase = args.checkPhase or ''
|
|
|
|
runHook preCheck
|
2022-07-24 08:45:55 +02:00
|
|
|
# We do not set trimpath for tests, in case they reference test assets
|
|
|
|
export GOFLAGS=''${GOFLAGS//-trimpath/}
|
2019-03-08 20:17:05 +01:00
|
|
|
|
|
|
|
for pkg in $(getGoDirs test); do
|
2022-05-02 01:15:08 +02:00
|
|
|
buildGoDir test "$pkg"
|
2019-03-08 20:17:05 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
runHook postCheck
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = args.installPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
|
|
|
mkdir -p $out
|
|
|
|
dir="$GOPATH/bin"
|
|
|
|
[ -e "$dir" ] && cp -r $dir $out
|
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
2020-03-17 17:27:27 +01:00
|
|
|
strictDeps = true;
|
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
disallowedReferences = lib.optional (!allowGoReference) go;
|
|
|
|
|
2022-07-29 00:46:48 +02:00
|
|
|
passthru = passthru // { inherit go go-modules vendorSha256 vendorHash; };
|
2019-03-08 20:17:05 +01:00
|
|
|
|
2021-07-04 04:49:55 +02:00
|
|
|
enableParallelBuilding = enableParallelBuilding;
|
|
|
|
|
2019-03-08 20:17:05 +01:00
|
|
|
meta = {
|
|
|
|
# Add default meta information
|
|
|
|
platforms = go.meta.platforms or lib.platforms.all;
|
2022-07-20 18:28:36 +02:00
|
|
|
} // meta;
|
2019-03-08 20:17:05 +01:00
|
|
|
});
|
2021-02-15 18:53:04 +01:00
|
|
|
in
|
2021-08-23 06:55:51 +02:00
|
|
|
lib.warnIf (buildFlags != "" || buildFlagsArray != "")
|
|
|
|
"Use the `ldflags` and/or `tags` attributes instead of `buildFlags`/`buildFlagsArray`"
|
2019-03-08 20:17:05 +01:00
|
|
|
package
|