forked from numtide/build-go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildGoCache.nix
79 lines (74 loc) · 2.08 KB
/
buildGoCache.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{ buildGoModule
, lib
, rsync
}:
{ importPackagesFile
, src
, vendorHash ? null
, vendorEnv ? null
, proxyVendor ? false
, buildInputs ? [ ]
, nativeBuildInputs ? [ ]
}:
assert (!(vendorHash != null && vendorEnv != null)); # vendorHash and vendorEnv are mutually exclusive
assert (vendorHash != null || vendorEnv != null); # one of vendorHash or vendorEnv must be set
assert (proxyVendor -> vendorEnv == null); # proxyVendor is not compatible with vendorEnv
let
filteredSource = builtins.path {
path = src;
filter = path: type: type == "regular" && (baseNameOf path == "go.sum" || baseNameOf path == "go.mod" || baseNameOf path == "vendor");
name = "go-cache-src";
};
goModules =
if vendorEnv == null then (buildGoModule {
name = "deps";
src = src;
inherit vendorHash proxyVendor;
}).goModules
else vendorEnv;
in
buildGoModule {
name = "go-cache";
src = filteredSource;
inherit buildInputs;
nativeBuildInputs = [ rsync ] ++ nativeBuildInputs;
vendorHash = null;
unpackPhase = ''
mkdir source
cp -r $src/* source
chmod -R +w source
cd source
'';
inherit proxyVendor;
buildPhase = ''
export HOME=$TMPDIR
mkdir -p $out/
export GOFLAGS=-trimpath
${if proxyVendor then ''
export GOPROXY="file://${goModules}"
mkdir -p $out/go-mod-cache
export GOMODCACHE=$out/go-mod-cache
'' else ''
export GOPROXY=off
export GOSUMDB=off
cp -r --reflink=auto ${goModules}/ vendor
export GOFLAGS="''${GOFLAGS} -mod=vendor"
''}
export GOCACHE=$out/go-cache
export GO_NO_VENDOR_CHECKS="1"
export GOPATH=$TMPDIR/go
export GOBIN=$GOPATH/bin
mkdir -p $GOPATH/src
xargs go build <${importPackagesFile}
mkdir -p $out/nix-support
cat > $out/nix-support/setup-hook <<EOF
mkdir -p $TMPDIR
cp --reflink=auto -r $out/go-cache $TMPDIR/go-cache
chmod -R +w $TMPDIR/go-cache
${lib.optionalString proxyVendor ''export GOMODCACHE="$out/go-mod-cache"''}
EOF
'';
doCheck = false;
allowGoReference = true;
phases = [ "unpackPhase" "buildPhase" ];
}