2020-07-23 09:28:54 +00:00
|
|
|
{ stdenv
|
|
|
|
, runCommand
|
|
|
|
, buildEnv
|
|
|
|
, lib
|
|
|
|
, fetchgit
|
|
|
|
, removeReferencesTo
|
2021-02-10 14:50:58 +00:00
|
|
|
, go
|
2020-07-23 09:28:54 +00:00
|
|
|
}:
|
2020-07-22 12:54:30 +00:00
|
|
|
let
|
|
|
|
|
2021-03-29 14:38:03 +00:00
|
|
|
parseGoMod = import ./parser.nix;
|
|
|
|
|
2020-07-22 12:54:30 +00:00
|
|
|
removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}'';
|
|
|
|
|
|
|
|
buildGoApplication =
|
2020-12-03 15:16:08 +00:00
|
|
|
{ modules
|
|
|
|
, src
|
2021-03-29 14:38:03 +00:00
|
|
|
, pwd ? null
|
2020-12-03 15:16:08 +00:00
|
|
|
, CGO_ENABLED ? "0"
|
|
|
|
, nativeBuildInputs ? [ ]
|
|
|
|
, allowGoReference ? false
|
|
|
|
, meta ? { }
|
|
|
|
, passthru ? { }
|
|
|
|
, ...
|
|
|
|
}@attrs:
|
|
|
|
let
|
2020-07-22 12:54:30 +00:00
|
|
|
modulesStruct = builtins.fromTOML (builtins.readFile modules);
|
|
|
|
|
2021-03-29 14:38:03 +00:00
|
|
|
goMod = parseGoMod (builtins.readFile "${builtins.toString pwd}/go.mod");
|
|
|
|
localReplaceCommands =
|
|
|
|
let
|
|
|
|
localReplaceAttrs = lib.filterAttrs (n: v: lib.hasAttr "path" v) goMod.replace;
|
|
|
|
commands = (
|
|
|
|
lib.mapAttrsToList
|
|
|
|
(name: value: (
|
|
|
|
''
|
|
|
|
mkdir -p $(dirname vendor/${name})
|
|
|
|
ln -s ${pwd + "/${value.path}"} vendor/${name}
|
|
|
|
''
|
|
|
|
))
|
|
|
|
localReplaceAttrs);
|
|
|
|
in
|
|
|
|
if pwd != null then commands else [ ];
|
|
|
|
|
2020-12-03 15:16:08 +00:00
|
|
|
vendorEnv = runCommand "vendor-env"
|
|
|
|
{
|
|
|
|
nativeBuildInputs = [ go ];
|
|
|
|
json = builtins.toJSON modulesStruct;
|
|
|
|
|
|
|
|
sources = builtins.toJSON (lib.mapAttrs
|
|
|
|
(goPackagePath: meta:
|
|
|
|
let
|
|
|
|
src = fetchgit {
|
|
|
|
inherit (meta.fetch) url sha256 rev;
|
|
|
|
fetchSubmodules = true;
|
|
|
|
};
|
|
|
|
srcPath = "${src}/${meta.relPath or ""}";
|
|
|
|
in
|
|
|
|
srcPath)
|
|
|
|
modulesStruct);
|
|
|
|
|
|
|
|
passAsFile = [ "json" "sources" ];
|
|
|
|
}
|
|
|
|
(
|
|
|
|
''
|
|
|
|
mkdir vendor
|
|
|
|
|
|
|
|
export GOCACHE=$TMPDIR/go-cache
|
|
|
|
export GOPATH="$TMPDIR/go"
|
|
|
|
|
|
|
|
go run ${./symlink.go}
|
2021-03-29 14:38:03 +00:00
|
|
|
${lib.concatStringsSep "\n" localReplaceCommands}
|
|
|
|
|
|
|
|
find vendor
|
2020-12-03 15:16:08 +00:00
|
|
|
|
|
|
|
mv vendor $out
|
|
|
|
''
|
|
|
|
);
|
2020-07-22 12:54:30 +00:00
|
|
|
|
|
|
|
removeReferences = [ ] ++ lib.optional (!allowGoReference) go;
|
|
|
|
|
2021-11-27 08:30:38 +00:00
|
|
|
package = stdenv.mkDerivation (attrs // {
|
2020-07-22 12:54:30 +00:00
|
|
|
nativeBuildInputs = [ removeReferencesTo go ] ++ nativeBuildInputs;
|
|
|
|
|
|
|
|
inherit (go) GOOS GOARCH;
|
|
|
|
inherit CGO_ENABLED;
|
|
|
|
|
|
|
|
GO_NO_VENDOR_CHECKS = "1";
|
|
|
|
|
|
|
|
GO111MODULE = "on";
|
|
|
|
GOFLAGS = "-mod=vendor";
|
|
|
|
|
|
|
|
configurePhase = attrs.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
|
|
|
|
export GOCACHE=$TMPDIR/go-cache
|
|
|
|
export GOPATH="$TMPDIR/go"
|
|
|
|
export GOSUMDB=off
|
|
|
|
export GOPROXY=off
|
|
|
|
cd "$modRoot"
|
|
|
|
if [ -n "${vendorEnv}" ]; then
|
|
|
|
rm -rf vendor
|
|
|
|
ln -s ${vendorEnv} vendor
|
|
|
|
fi
|
|
|
|
|
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = attrs.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
|
|
|
|
echo "$subPackages" | sed "s,\(^\| \),\1./,g"
|
|
|
|
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
|
|
|
|
echo "Building subPackage $pkg"
|
|
|
|
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
|
|
|
|
'';
|
|
|
|
|
|
|
|
doCheck = attrs.doCheck or true;
|
|
|
|
checkPhase = attrs.checkPhase or ''
|
|
|
|
runHook preCheck
|
|
|
|
|
|
|
|
for pkg in $(getGoDirs test); do
|
|
|
|
buildGoDir test "$pkg"
|
|
|
|
done
|
|
|
|
|
|
|
|
runHook postCheck
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = attrs.installPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
|
|
|
mkdir -p $out
|
|
|
|
dir="$GOPATH/bin"
|
|
|
|
[ -e "$dir" ] && cp -r $dir $out
|
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
preFixup = (attrs.preFixup or "") + ''
|
|
|
|
find $out/{bin,libexec,lib} -type f 2>/dev/null | xargs -r ${removeExpr removeReferences} || true
|
|
|
|
'';
|
|
|
|
|
|
|
|
strictDeps = true;
|
|
|
|
|
|
|
|
disallowedReferences = lib.optional (!allowGoReference) go;
|
|
|
|
|
|
|
|
passthru = passthru // { inherit go vendorEnv; };
|
|
|
|
|
|
|
|
meta = { platforms = go.meta.platforms or lib.platforms.all; } // meta;
|
|
|
|
});
|
|
|
|
|
2020-12-03 15:16:08 +00:00
|
|
|
in
|
|
|
|
package;
|
2020-07-22 12:54:30 +00:00
|
|
|
|
2020-12-03 15:16:08 +00:00
|
|
|
in
|
|
|
|
buildGoApplication
|