2020-10-29 03:27:20 +00:00
|
|
|
{ go, cacert, git, lib, stdenv, vend }:
|
2019-03-08 19:17:05 +00:00
|
|
|
|
2019-03-17 16:29:20 +00:00
|
|
|
{ name ? "${args'.pname}-${args'.version}"
|
2019-03-08 19:17:05 +00:00
|
|
|
, src
|
|
|
|
, buildInputs ? []
|
|
|
|
, nativeBuildInputs ? []
|
|
|
|
, passthru ? {}
|
|
|
|
, patches ? []
|
|
|
|
|
2019-04-23 15:15:45 +01:00
|
|
|
# A function to override the go-modules derivation
|
|
|
|
, overrideModAttrs ? (_oldAttrs : {})
|
|
|
|
|
2019-12-15 22:58:16 +00:00
|
|
|
# path to go.mod and go.sum directory
|
|
|
|
, modRoot ? "./"
|
|
|
|
|
2020-04-30 04:08:21 +01:00
|
|
|
# vendorSha256 is the sha256 of the vendored dependencies
|
2019-12-28 20:36:42 +00:00
|
|
|
#
|
2020-04-30 04:08:21 +01:00
|
|
|
# if vendorSha256 is null, then we won't fetch any dependencies and
|
|
|
|
# rely on the vendor folder within the source.
|
2020-06-04 05:01:51 +01:00
|
|
|
, vendorSha256
|
2020-04-30 04:08:21 +01:00
|
|
|
# Whether to delete the vendor folder supplied with the source.
|
|
|
|
, deleteVendor ? false
|
2020-06-04 03:54:39 +01:00
|
|
|
# Whether to run the vend tool to regenerate the vendor directory.
|
|
|
|
# This is useful if any dependency contain C files.
|
|
|
|
, runVend ? false
|
2020-04-30 04:08:21 +01:00
|
|
|
|
2019-03-08 19:17:05 +00:00
|
|
|
# We want parallel builds by default
|
|
|
|
, enableParallelBuilding ? true
|
|
|
|
|
|
|
|
# Do not enable this without good reason
|
|
|
|
# IE: programs coupled with the compiler
|
|
|
|
, allowGoReference ? false
|
|
|
|
|
|
|
|
, meta ? {}
|
|
|
|
|
2020-12-16 19:40:26 +00:00
|
|
|
# Not needed with buildGoModule
|
2021-02-15 17:53:04 +00:00
|
|
|
, goPackagePath ? ""
|
2020-12-16 19:40:26 +00:00
|
|
|
|
2019-03-08 19:17:05 +00:00
|
|
|
, ... }@args':
|
|
|
|
|
|
|
|
with builtins;
|
|
|
|
|
2021-02-15 17:53:04 +00:00
|
|
|
assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`";
|
|
|
|
|
2019-03-08 19:17:05 +00:00
|
|
|
let
|
2021-02-15 17:53:04 +00:00
|
|
|
args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" ];
|
2019-03-08 19:17:05 +00: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 00:35:33 +00:00
|
|
|
go-modules = if vendorSha256 != null then stdenv.mkDerivation (let modArgs = {
|
2020-04-30 04:08:21 +01:00
|
|
|
|
2019-03-08 19:17:05 +00:00
|
|
|
name = "${name}-go-modules";
|
|
|
|
|
2020-04-30 04:08:21 +01:00
|
|
|
nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ go git cacert ];
|
2019-03-08 19:17:05 +00:00
|
|
|
|
|
|
|
inherit (args) src;
|
|
|
|
inherit (go) GOOS GOARCH;
|
|
|
|
|
|
|
|
patches = args.patches or [];
|
2020-04-30 04:08:21 +01:00
|
|
|
preBuild = args.preBuild or "";
|
|
|
|
sourceRoot = args.sourceRoot or "";
|
2019-03-08 19:17:05 +00:00
|
|
|
|
|
|
|
GO111MODULE = "on";
|
|
|
|
|
|
|
|
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
|
|
|
|
"GIT_PROXY_COMMAND" "SOCKS_SERVER"
|
|
|
|
];
|
|
|
|
|
|
|
|
configurePhase = args.modConfigurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
|
|
|
|
export GOCACHE=$TMPDIR/go-cache
|
|
|
|
export GOPATH="$TMPDIR/go"
|
2019-12-15 22:58:16 +00:00
|
|
|
cd "${modRoot}"
|
2019-03-08 19:17:05 +00:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = args.modBuildPhase or ''
|
|
|
|
runHook preBuild
|
2020-10-20 00:03:20 +01: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 04:08:21 +01:00
|
|
|
fi
|
2020-10-20 00:03:20 +01:00
|
|
|
'' + ''
|
2020-08-12 11:14:58 +01:00
|
|
|
if [ -d vendor ]; then
|
2020-08-12 10:33:03 +01:00
|
|
|
echo "vendor folder exists, please set 'vendorSha256 = null;' in your expression"
|
2020-04-30 04:08:21 +01:00
|
|
|
exit 10
|
|
|
|
fi
|
2020-06-04 03:54:39 +01:00
|
|
|
|
2020-10-20 00:03:20 +01:00
|
|
|
${if runVend then ''
|
|
|
|
echo "running 'vend' to rewrite vendor folder"
|
|
|
|
${vend}/bin/vend
|
|
|
|
'' else ''
|
|
|
|
go mod vendor
|
|
|
|
''}
|
|
|
|
|
2020-04-30 04:08:21 +01:00
|
|
|
mkdir -p vendor
|
2019-03-08 19:17:05 +00:00
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = args.modInstallPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
2019-10-12 18:00:00 +01:00
|
|
|
# remove cached lookup results and tiles
|
2020-04-30 04:08:21 +01:00
|
|
|
cp -r --reflink=auto vendor $out
|
2019-03-08 19:17:05 +00:00
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
dontFixup = true;
|
2019-12-28 20:36:42 +00:00
|
|
|
}; in modArgs // (
|
|
|
|
{
|
|
|
|
outputHashMode = "recursive";
|
|
|
|
outputHashAlgo = "sha256";
|
2020-04-30 04:08:21 +01:00
|
|
|
outputHash = vendorSha256;
|
2019-12-28 20:36:42 +00:00
|
|
|
}
|
2020-04-30 04:08:21 +01:00
|
|
|
) // overrideModAttrs modArgs) else "";
|
2019-03-08 19:17:05 +00: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 00:35:33 +00:00
|
|
|
package = stdenv.mkDerivation (args // {
|
2020-10-29 03:27:20 +00:00
|
|
|
nativeBuildInputs = [ go ] ++ nativeBuildInputs;
|
2019-03-08 19:17:05 +00:00
|
|
|
|
|
|
|
inherit (go) GOOS GOARCH;
|
|
|
|
|
|
|
|
GO111MODULE = "on";
|
2020-10-29 03:27:20 +00:00
|
|
|
GOFLAGS = [ "-mod=vendor" ] ++ lib.optionals (!allowGoReference) [ "-trimpath" ];
|
2019-03-08 19:17:05 +00:00
|
|
|
|
|
|
|
configurePhase = args.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
|
|
|
|
export GOCACHE=$TMPDIR/go-cache
|
|
|
|
export GOPATH="$TMPDIR/go"
|
2019-12-01 21:20:00 +00:00
|
|
|
export GOSUMDB=off
|
2020-04-30 04:08:21 +01:00
|
|
|
export GOPROXY=off
|
2019-12-15 22:58:16 +00:00
|
|
|
cd "$modRoot"
|
2020-10-22 13:50:46 +01:00
|
|
|
'' + lib.optionalString (go-modules != "") ''
|
|
|
|
rm -rf vendor
|
|
|
|
cp -r --reflink=auto ${go-modules} vendor
|
|
|
|
'' + ''
|
2019-12-15 22:58:16 +00:00
|
|
|
|
2019-03-08 19:17:05 +00:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = args.buildPhase or ''
|
|
|
|
runHook preBuild
|
|
|
|
|
|
|
|
buildGoDir() {
|
|
|
|
local d; local cmd;
|
|
|
|
cmd="$1"
|
|
|
|
d="$2"
|
|
|
|
. $TMPDIR/buildFlagsArray
|
|
|
|
echo "$d" | grep -q "\(/_\|examples\|Godeps\|testdata\)" && return 0
|
|
|
|
[ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0
|
|
|
|
local OUT
|
|
|
|
if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" -v -p $NIX_BUILD_CORES $d 2>&1)"; then
|
|
|
|
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 06:22:02 +01:00
|
|
|
echo "$subPackages" | sed "s,\(^\| \),\1./,g"
|
2019-03-08 19:17:05 +00:00
|
|
|
else
|
|
|
|
find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique
|
|
|
|
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 06:22:02 +01:00
|
|
|
echo "Building subPackage $pkg"
|
2019-03-08 19:17:05 +00: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 01:35:19 +01:00
|
|
|
doCheck = args.doCheck or true;
|
2019-03-08 19:17:05 +00:00
|
|
|
checkPhase = args.checkPhase or ''
|
|
|
|
runHook preCheck
|
|
|
|
|
|
|
|
for pkg in $(getGoDirs test); do
|
|
|
|
buildGoDir test "$pkg"
|
|
|
|
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 16:27:27 +00:00
|
|
|
strictDeps = true;
|
|
|
|
|
2019-03-08 19:17:05 +00:00
|
|
|
disallowedReferences = lib.optional (!allowGoReference) go;
|
|
|
|
|
2020-04-30 04:08:21 +01:00
|
|
|
passthru = passthru // { inherit go go-modules vendorSha256 ; };
|
2019-03-08 19:17:05 +00:00
|
|
|
|
|
|
|
meta = {
|
|
|
|
# Add default meta information
|
|
|
|
platforms = go.meta.platforms or lib.platforms.all;
|
|
|
|
} // meta // {
|
|
|
|
# add an extra maintainer to every package
|
|
|
|
maintainers = (meta.maintainers or []) ++
|
2020-07-18 08:45:08 +01:00
|
|
|
[ lib.maintainers.kalbasit ];
|
2019-03-08 19:17:05 +00:00
|
|
|
};
|
|
|
|
});
|
2021-02-15 17:53:04 +00:00
|
|
|
in
|
2019-03-08 19:17:05 +00:00
|
|
|
package
|