-
Notifications
You must be signed in to change notification settings - Fork 1
/
nix.ts
111 lines (97 loc) · 2.43 KB
/
nix.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
type Map = Record<string, string>;
function concatMap(map: Map) {
return Object.keys(map)
.map((k) => k + "=" + map[k] + ";")
.join("");
}
function Obj(obj: Map) {
return "{" + concatMap(obj) + "}";
}
function Str(str: string) {
return '"' + str + '"';
}
function Let(bindings: Map, expr: string) {
return "let " + concatMap(bindings) + " in " + expr;
}
function Fun(arg: string | string[], body: string) {
if (Array.isArray(arg)) {
return "{" + arg.join(",") + "}" + ": " + body;
}
return arg + ": " + body;
}
function Arr(...items: string[]) {
return "[" + items.join(" ") + "]";
}
function Import(...args: string[]) {
return Call("import", ...args);
}
function With(arg: string, body: string) {
return "with " + arg + "; " + body;
}
function Call(...args: string[]) {
return args.join(" ");
}
function mkStrOptions(options: Record<string, string>) {
var result = {};
Object.keys(options).forEach((k) => {
result = {
...result,
...{
[k]: Call(
"mkOption",
Obj({ type: "types.str", default: Str(options[k]) })
),
},
};
});
return result;
}
function SvcMod(svc: {
name: string;
package: string;
options: Record<string, string>;
args: string[];
}) {
return Fun(
["config", "lib", "pkgs", "..."],
With(
"lib",
Let(
{ cfg: "config.services." + svc.name },
Obj({
options: Obj({
["services." + svc.name]: Obj({
...{
enable: Call(
"mkOption",
Obj({ type: "types.bool", default: "false" })
),
package: Call(
"mkOption",
Obj({ type: "types.package", default: "pkgs." + svc.package })
),
},
...mkStrOptions(svc.options),
}),
}),
config: Call(
"mkIf",
"cfg.enable",
Obj({
"environment.systemPackages": Arr("cfg.package"),
["launchd.daemons." + svc.name + ".serviceConfig"]: Obj({
ProgramArguments: Arr(
Str("${cfg.package}/bin/" + svc.name),
...svc.args
),
RunAtLoad: "true",
KeepAlive: "true",
}),
})
),
})
)
)
);
}
export { Obj, Str, Let, Fun, Arr, Import, With, Call, SvcMod };