-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathlib.nix
85 lines (85 loc) · 2.66 KB
/
lib.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
{
# Create an attrsOf module wrapper (`services.${name}`) around the given submodule.
#
# where module filename is of form `${name}.nix`. The submodule takes this
# 'name' parameter, and is expected to set the final process-compose config in
# its `outputs.settings` option.
multiService = mod:
{ config, pkgs, lib, ... }:
let
# Derive name from filename
service = lib.pipe mod [
builtins.baseNameOf
(lib.strings.splitString ".")
builtins.head
];
serviceModule = { config, name, ... }: {
options = {
enable = lib.mkEnableOption "Enable the ${service}.<name> service";
dataDir = lib.mkOption {
type = lib.types.str;
default = "./data/${name}";
description = "The directory where all data for `${service}.<name>` is stored";
};
namespace = lib.mkOption {
description = ''
Namespace for the ${service} service
'';
default = "${service}.${name}";
type = lib.types.str;
};
outputs = {
defaultProcessSettings = lib.mkOption {
type = lib.types.deferredModule;
internal = true;
readOnly = true;
description = ''
Default settings for all processes under the ${service} service
'';
default = {
namespace = lib.mkDefault config.namespace;
};
};
settings = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
internal = true;
description = ''
process-compose settings for the processes under the ${service} service
'';
apply = v: v // {
processes = lib.flip lib.mapAttrs v.processes (_: cfg:
{ imports = [ config.outputs.defaultProcessSettings cfg ]; }
);
};
};
};
};
};
in
{
options = {
services.${service} = lib.mkOption {
description = ''
${service} service
'';
default = { };
type = lib.types.attrsOf (lib.types.submoduleWith {
specialArgs = { inherit pkgs; };
modules = [
serviceModule
mod
];
});
};
};
config = {
settings = {
imports =
lib.pipe config.services.${service} [
(lib.filterAttrs (_: cfg: cfg.enable))
(lib.mapAttrsToList (_: cfg: cfg.outputs.settings))
];
};
};
};
}