|
4 | 4 | { |
5 | 5 | pkgs, |
6 | 6 | crane, |
7 | | - dbg, |
8 | 7 | ... |
9 | 8 | }: |
10 | 9 | let |
|
53 | 52 | config, |
54 | 53 | ... |
55 | 54 | }: |
56 | | - with lib; |
57 | 55 | let |
| 56 | + inherit (lib) mkOption mkEnableOption types; |
| 57 | + mkMergeTopLevel = |
| 58 | + names: attrs: |
| 59 | + lib.getAttrs names ( |
| 60 | + lib.mapAttrs (_k: v: lib.mkMerge v) (lib.foldAttrs (n: a: [ n ] ++ a) [ ] attrs) |
| 61 | + ); |
58 | 62 | cfg = config.services.voyager; |
59 | 63 | in |
60 | 64 | { |
61 | 65 | options.services.voyager = { |
62 | 66 | enable = mkEnableOption "Voyager services"; |
63 | 67 | instances = mkOption { |
| 68 | + default = [ ]; |
64 | 69 | type = types.listOf ( |
65 | 70 | types.submodule { |
66 | 71 | options = { |
|
201 | 206 | }; |
202 | 207 | }; |
203 | 208 | }; |
204 | | - modules = |
205 | | - let |
206 | | - moduleConfigType = |
207 | | - infoOptions: |
208 | | - mkOption { |
209 | | - type = types.listOf ( |
210 | | - types.submodule { |
211 | | - options = { |
212 | | - enabled = mkOption { |
213 | | - type = types.bool; |
214 | | - default = true; |
215 | | - }; |
216 | | - path = mkOption { |
217 | | - type = types.path; |
218 | | - }; |
219 | | - config = mkOption { |
220 | | - type = types.attrs; |
221 | | - default = { }; |
222 | | - }; |
223 | | - info = mkOption { |
224 | | - type = types.submodule { |
225 | | - options = infoOptions; |
226 | | - }; |
227 | | - }; |
228 | | - }; |
229 | | - } |
230 | | - ); |
231 | | - }; |
232 | | - in |
233 | | - mkOption { |
234 | | - type = types.submodule { |
235 | | - options = { |
236 | | - client = moduleConfigType { |
237 | | - client_type = mkOption { type = types.str; }; |
238 | | - consensus_type = mkOption { type = types.str; }; |
239 | | - ibc_interface = mkOption { type = types.str; }; |
240 | | - ibc_spec_id = mkOption { type = types.str; }; |
241 | | - }; |
242 | | - client_bootstrap = moduleConfigType { |
243 | | - chain_id = mkOption { type = types.str; }; |
244 | | - client_type = mkOption { type = types.str; }; |
245 | | - }; |
246 | | - consensus = moduleConfigType { |
247 | | - chain_id = mkOption { type = types.str; }; |
248 | | - consensus_type = mkOption { type = types.str; }; |
249 | | - }; |
250 | | - proof = moduleConfigType { |
251 | | - chain_id = mkOption { type = types.str; }; |
252 | | - ibc_spec_id = mkOption { type = types.str; }; |
253 | | - }; |
254 | | - state = moduleConfigType { |
255 | | - chain_id = mkOption { type = types.str; }; |
256 | | - ibc_spec_id = mkOption { type = types.str; }; |
257 | | - }; |
258 | | - }; |
259 | | - }; |
260 | | - }; |
261 | | - plugins = mkOption { |
262 | | - type = types.listOf ( |
263 | | - types.submodule { |
264 | | - options = { |
265 | | - enabled = mkOption { |
266 | | - type = types.bool; |
267 | | - default = true; |
268 | | - }; |
269 | | - path = mkOption { type = types.path; }; |
270 | | - config = mkOption { type = types.attrs; }; |
271 | | - }; |
272 | | - } |
273 | | - ); |
274 | | - }; |
| 209 | + modules = mkOption { type = types.attrs; }; |
| 210 | + plugins = mkOption { type = types.listOf types.attrs; }; |
275 | 211 | }; |
276 | 212 | } |
277 | 213 | ); |
278 | 214 | }; |
279 | 215 | }; |
280 | 216 |
|
281 | | - config = attrsets.mergeAttrsList ( |
282 | | - flip map cfg.instances ( |
| 217 | + config = mkMergeTopLevel [ "systemd" "environment" "users" ] ( |
| 218 | + map ( |
283 | 219 | instance: |
284 | 220 | let |
285 | | - configJson = pkgs.writeText "config.json" ( |
| 221 | + configJson = pkgs.writeText "config-${instance.name}.json" ( |
286 | 222 | builtins.toJSON ( |
287 | | - recursiveUpdate |
288 | | - (filterAttrsRecursive (_n: v: v != null) { |
| 223 | + lib.recursiveUpdate |
| 224 | + (lib.filterAttrsRecursive (_n: v: v != null) { |
289 | 225 | inherit (instance) |
290 | 226 | equivalent_chain_ids |
291 | 227 | modules |
|
300 | 236 | ) |
301 | 237 | ); |
302 | 238 | in |
303 | | - mkIf cfg.enable |
304 | | - && instance.enable { |
305 | | - environment.systemPackages = [ |
| 239 | + { |
| 240 | + environment.systemPackages = lib.mkIf (cfg.enable && instance.enable) [ |
306 | 241 | (pkgs.writeShellApplication { |
307 | 242 | name = "voyager-${instance.name}"; |
308 | 243 | runtimeInputs = [ instance.package ]; |
309 | 244 | text = '' |
310 | | - ${getExe instance.package} --config-file-path ${configJson} "$@" |
| 245 | + ${lib.getExe instance.package} --config-file-path ${configJson} "$@" |
311 | 246 | ''; |
312 | 247 | }) |
313 | 248 | ]; |
314 | | - systemd.services = { |
315 | | - "voyager-${instance.name}" = { |
316 | | - wantedBy = [ "multi-user.target" ]; |
317 | | - description = "Voyager ${instance.name}"; |
318 | | - serviceConfig = { |
319 | | - Type = "simple"; |
320 | | - ExecStart = '' |
321 | | - ${getExe instance.package} \ |
322 | | - --config-file-path ${configJson} \ |
323 | | - -l ${instance.settings.log-format} ${ |
324 | | - optionalString ( |
325 | | - instance.settings.stack-size != null |
326 | | - ) "--stack-size ${toString instance.settings.stack-size}" |
327 | | - } \ |
328 | | - start |
329 | | - ''; |
330 | | - Restart = mkForce "always"; |
331 | | - RestartSec = 10; |
332 | | - RuntimeMaxSec = instance.settings.runtime-max-secs; |
333 | | - }; |
334 | | - environment = { |
335 | | - RUST_LOG = "${instance.settings.log-level}"; |
336 | | - RUST_LOG_FORMAT = "${instance.settings.log-format}"; |
337 | | - }; |
| 249 | + systemd.services."voyager-${instance.name}" = lib.mkIf (cfg.enable && instance.enable) { |
| 250 | + wantedBy = [ "multi-user.target" ]; |
| 251 | + description = "Voyager ${instance.name}"; |
| 252 | + serviceConfig = { |
| 253 | + Type = "simple"; |
| 254 | + User = instance.name; |
| 255 | + ExecStart = '' |
| 256 | + ${lib.getExe instance.package} \ |
| 257 | + --config-file-path ${configJson} \ |
| 258 | + -l ${instance.settings.log-format} ${ |
| 259 | + lib.optionalString ( |
| 260 | + instance.settings.stack-size != null |
| 261 | + ) "--stack-size ${toString instance.settings.stack-size}" |
| 262 | + } \ |
| 263 | + start |
| 264 | + ''; |
| 265 | + Restart = lib.mkForce "always"; |
| 266 | + RestartSec = 10; |
| 267 | + RuntimeMaxSec = instance.settings.runtime-max-secs; |
| 268 | + }; |
| 269 | + environment = { |
| 270 | + RUST_LOG = "${instance.settings.log-level}"; |
| 271 | + RUST_LOG_FORMAT = "${instance.settings.log-format}"; |
| 272 | + }; |
| 273 | + }; |
| 274 | + users = { |
| 275 | + users.${instance.name} = { |
| 276 | + isSystemUser = true; |
| 277 | + group = instance.name; |
338 | 278 | }; |
| 279 | + groups.${instance.name} = { }; |
339 | 280 | }; |
340 | 281 | } |
341 | | - ) |
| 282 | + ) cfg.instances |
342 | 283 | ); |
343 | 284 | }; |
344 | 285 | } |
0 commit comments