Skip to content

Commit 55eaae9

Browse files
committed
feat(voyager): multi instance nixos module
wip add user wip update update udpate update wip wip wip update wip wip update wip
1 parent f6fc5d7 commit 55eaae9

File tree

1 file changed

+48
-107
lines changed

1 file changed

+48
-107
lines changed

voyager/voyager.nix

Lines changed: 48 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{
55
pkgs,
66
crane,
7-
dbg,
87
...
98
}:
109
let
@@ -53,14 +52,20 @@
5352
config,
5453
...
5554
}:
56-
with lib;
5755
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+
);
5862
cfg = config.services.voyager;
5963
in
6064
{
6165
options.services.voyager = {
6266
enable = mkEnableOption "Voyager services";
6367
instances = mkOption {
68+
default = [ ];
6469
type = types.listOf (
6570
types.submodule {
6671
options = {
@@ -201,91 +206,22 @@
201206
};
202207
};
203208
};
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; };
275211
};
276212
}
277213
);
278214
};
279215
};
280216

281-
config = attrsets.mergeAttrsList (
282-
flip map cfg.instances (
217+
config = mkMergeTopLevel [ "systemd" "environment" "users" ] (
218+
map (
283219
instance:
284220
let
285-
configJson = pkgs.writeText "config.json" (
221+
configJson = pkgs.writeText "config-${instance.name}.json" (
286222
builtins.toJSON (
287-
recursiveUpdate
288-
(filterAttrsRecursive (_n: v: v != null) {
223+
lib.recursiveUpdate
224+
(lib.filterAttrsRecursive (_n: v: v != null) {
289225
inherit (instance)
290226
equivalent_chain_ids
291227
modules
@@ -300,45 +236,50 @@
300236
)
301237
);
302238
in
303-
mkIf cfg.enable
304-
&& instance.enable {
305-
environment.systemPackages = [
239+
{
240+
environment.systemPackages = lib.mkIf (cfg.enable && instance.enable) [
306241
(pkgs.writeShellApplication {
307242
name = "voyager-${instance.name}";
308243
runtimeInputs = [ instance.package ];
309244
text = ''
310-
${getExe instance.package} --config-file-path ${configJson} "$@"
245+
${lib.getExe instance.package} --config-file-path ${configJson} "$@"
311246
'';
312247
})
313248
];
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;
338278
};
279+
groups.${instance.name} = { };
339280
};
340281
}
341-
)
282+
) cfg.instances
342283
);
343284
};
344285
}

0 commit comments

Comments
 (0)