-
Notifications
You must be signed in to change notification settings - Fork 889
/
Copy pathvoyager.nix
215 lines (210 loc) · 6.35 KB
/
voyager.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
{ self, ... }:
{
perSystem =
{
self',
pkgs,
system,
config,
crane,
stdenv,
dbg,
mkCi,
...
}:
let
attrs = {
crateDirFromRoot = "voyager";
extraEnv = {
SQLX_OFFLINE = "1";
};
};
voy-modules-list = builtins.filter (
member:
(pkgs.lib.hasPrefix "voyager/modules" member) || (pkgs.lib.hasPrefix "voyager/plugins" member)
) (builtins.fromTOML (builtins.readFile ../Cargo.toml)).workspace.members;
voyager-modules = crane.buildWorkspaceMember {
crateDirFromRoot = voy-modules-list;
pname = "voyager-modules";
version = "0.0.0";
dev = true;
};
voyager = crane.buildWorkspaceMember attrs;
voyager-dev =
pkgs.lib.warn "voyager-dev is not intended to be used in production" crane.buildWorkspaceMember
(attrs // { dev = true; });
in
{
packages =
voyager.packages
// {
# voyager-modules-names = (
# builtins.toFile "voyager-modules-list.json" (
# builtins.toJSON (
# map (
# p: (builtins.fromTOML (builtins.readFile "${../.}/${p}/Cargo.toml")).package.name
# ) voy-modules-list
# )
# )
# );
voyager-dev = mkCi false voyager-dev.packages.voyager-dev;
}
// voyager-modules.packages;
# we don't actually have any tests currently
# checks = voyager.checks // voyager-modules.checks;
};
flake.nixosModules.voyager =
{
lib,
pkgs,
config,
...
}:
with lib;
let
cfg = config.services.voyager;
in
{
options.services.voyager = {
enable = mkEnableOption "Voyager service";
package = mkOption {
type = types.package;
default = self.packages.${pkgs.system}.voyager;
};
modules = mkOption {
type = types.attrs;
};
plugins = mkOption {
# The configuration design is breaking quite often, would be a waste
# of effort to fix the type for now.
type = types.listOf (
types.submodule {
options = {
enabled = mkOption {
type = types.bool;
default = true;
};
path = mkOption { type = types.path; };
config = mkOption { type = types.attrs; };
};
}
);
};
optimizer-delay-milliseconds = mkOption {
type = types.int;
default = 100;
};
workers = mkOption {
type = types.int;
default = 20;
};
runtime-max-secs = mkOption {
type = types.int;
default = 1800;
};
db-url = mkOption {
type = types.str;
default = "postgres://voyager:voyager@localhost/voyager";
};
db-min-conn = mkOption {
type = types.int;
default = 20;
};
db-max-conn = mkOption {
type = types.int;
default = 20;
};
log-level = mkOption {
type = types.str;
default = "info";
# TODO: Support RUST_LOG per plugin (this will need to be done in voyager)
description = "RUST_LOG passed to voyager and all of the plugins.";
example = "voyager=debug";
};
log-format = mkOption {
type = types.enum [
"json"
"text"
];
default = "json";
# TODO: This is kinda dirty, find a better way? Probably through each plugin's config
description = "The log format for voyager. This will also be passed to all of the plugins as RUST_LOG_FORMAT.";
example = "text";
};
stack-size = mkOption {
type = types.nullOr types.number;
description = "The stack size (in bytes) for worker threads. See <https://docs.rs/tokio/1.40.0/tokio/runtime/struct.Builder.html#method.thread_stack_size> for more information.";
default = null;
example = 20971520;
};
rest_laddr = mkOption {
type = types.str;
default = "0.0.0.0:7177";
example = "0.0.0.0:7177";
};
rpc_laddr = mkOption {
type = types.str;
default = "0.0.0.0:7178";
example = "0.0.0.0:7178";
};
voyager-extra = mkOption {
type = types.attrs;
default = { };
};
};
config =
let
configJson = pkgs.writeText "config.json" (
builtins.toJSON {
inherit (cfg) plugins modules;
voyager = cfg.voyager-extra // {
num_workers = cfg.workers;
queue = {
type = "pg-queue";
database_url = cfg.db-url;
min_connections = cfg.db-min-conn;
max_connections = cfg.db-max-conn;
idle_timeout = null;
max_lifetime = null;
};
};
}
);
in
mkIf cfg.enable {
environment.systemPackages = [
(pkgs.writeShellApplication {
name = "voyager";
runtimeInputs = [ cfg.package ];
text = ''
${pkgs.lib.getExe cfg.package} --config-file-path ${configJson} "$@"
'';
})
];
systemd.services = {
voyager = {
wantedBy = [ "multi-user.target" ];
description = "Voyager";
serviceConfig = {
Type = "simple";
ExecStart = ''
${pkgs.lib.getExe cfg.package} \
--config-file-path ${configJson} \
-l ${cfg.log-format} ${
pkgs.lib.optionalString (cfg.stack-size != null) "--stack-size ${toString cfg.stack-size}"
} \
start
'';
Restart = mkForce "always";
RestartSec = 10;
RuntimeMaxSec = cfg.runtime-max-secs;
};
environment = {
RUST_LOG = "${cfg.log-level}";
RUST_LOG_FORMAT = "${cfg.log-format}";
};
};
};
};
};
}