-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.nix
81 lines (75 loc) · 2.01 KB
/
util.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
80
81
{ lib
, self
, system
, nix-filter
, runCommand
, runCommandLocal
, protobuf
, protoc-gen-go
, protoc-gen-go-grpc
, writeScriptBin
, yaegi
, ...
}:
let
inherit (lib) sources;
in
rec {
cleanNixFiles = src: nix-filter {
root = src;
exclude = [ (nix-filter.matchExt "nix") ];
};
devGoSrc = cleanNixFiles (sources.cleanSource ./dev-go);
cleanSourceForGoService = name: nix-filter {
root = devGoSrc;
include = [ "lib" "vendor" "go.mod" "go.sum" "services/${name}" ];
};
/**
Produces protos for serviceName as files under $out
*/
protosFor = serviceName:
let
svc = "${cleanSourceForGoService serviceName}/services";
protoPath = "${svc}/${serviceName}/def.proto";
# go_opt = "module=github.com/Cottand/selfosted/dev-go/lib/proto";
go_opt="paths=source_relative";
in
runCommand "protos-for-${serviceName}"
{
nativeBuildInputs = [ protobuf protoc-gen-go protoc-gen-go-grpc ];
}
''
mkdir $out
${if (builtins.pathExists protoPath)
then
''
pushd ${svc}
protoc -I=./ --go_out=$out --go_opt=${go_opt} --go-grpc_out=$out --go-grpc_opt=${go_opt} ${serviceName}/*.proto
popd
mv $out/${serviceName}/* $out
rm -rf $out/${serviceName}
''
else
""}
'';
protosForAllServices =
let
services = builtins.attrNames self.legacyPackages.${system}.services;
perServiceCommand = map
(name: (if (self.legacyPackages.${system}.services.${name} ? "protos") then
''
generated=${protosFor name}
dest="$out/${name}"
mkdir -p $dest
cp -r $generated/*.pb.go $dest
'' else ""
))
services;
concatted = builtins.concatStringsSep "\n" perServiceCommand;
in
runCommandLocal "protos-all-services" { } concatted;
buildYaegiScript = name: filePath: writeScriptBin name ''
#! ${yaegi}/bin/yaegi
${builtins.readFile filePath}
'';
}