-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.nix
100 lines (100 loc) · 2.9 KB
/
service.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
{
config,
lib,
name,
pkgs,
options,
...
}:
let
inherit (lib) mkOption;
t = lib.types;
depConditionDescriptions = {
process_completed = "is the type for waiting until a process has completed (any exit code)";
process_completed_successfully = "is the type for waiting until a process has completed successfully (exit code 0)";
process_healthy = "is the type for waiting until a process is healthy";
process_started = "is the type for waiting until a process has started (default)";
};
dependsOnType = t.submodule {
options = {
startOverride = mkOption {
type = t.nullOr t.bool;
default = null;
description = ''
Whether the dependency should be started when this service starts.
If null, then value of startDeps will be used.
'';
};
condition = mkOption {
type = t.enum (builtins.attrNames depConditionDescriptions);
default = "process_started";
description = ''
Aequired state the dependency needs to reach in order for dependant to start.
One of:
${builtins.mapAttrs (k: v: "- ${k}: ${v}")}
see: https://github.com/F1bonacc1/process-compose#-define-process-dependencies
'';
};
};
};
in
{
options = {
package = mkOption {
type = t.package;
};
exe = mkOption {
type = t.path;
default = lib.getExe config.package;
};
runtimeConfigType = mkOption {
type = t.optionType;
default = t.attrsOf (t.nullOr t.str);
};
runtimeConfig = mkOption {
type = config.runtimeConfigType;
default = {};
};
runtimeConfigToEnv = mkOption {
type = t.functionTo (t.attrsOf t.str);
default = attrs:
let nonNullAttrs = lib.filterAttrs (n: v: n != null) attrs;
in lib.extra.attrsToAttsOfJsonStrings attrs;
};
runtimeEnv = mkOption {
type = t.attrsOf t.str;
};
runtimeEnvJson = mkOption {
type = t.str;
};
dependsOn = mkOption {
type = t.attrsOf dependsOnType;
default = {};
};
startDeps = mkOption {
type = t.bool;
default = true;
description = "Whether dependencies should be started when this service starts.";
};
depsToStart = mkOption {
type = t.listOf t.str;
description = "List of deps to start when this service starts, calculated based on dependsOn.<dep>.startOverride and startDeps.";
};
};
config = {
runtimeEnv = config.runtimeConfigToEnv config.runtimeConfig;
runtimeEnvJson = builtins.toJSON config.runtimeEnv;
depsToStart =
let
depNames = builtins.attrNames config.dependsOn;
shouldStart = depName:
let
startOverride = config.dependsOn.${depName}.startOverride;
in
if startOverride == null then config.startDeps else startOverride
;
in
builtins.filter shouldStart depNames
;
};
}