1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.kapacitor;
7
8 kapacitorConf = pkgs.writeTextFile {
9 name = "kapacitord.conf";
10 text = ''
11 hostname="${config.networking.hostName}"
12 data_dir="${cfg.dataDir}"
13
14 [http]
15 bind-address = "${cfg.bind}:${toString cfg.port}"
16 log-enabled = false
17 auth-enabled = false
18
19 [task]
20 dir = "${cfg.dataDir}/tasks"
21 snapshot-interval = "${cfg.taskSnapshotInterval}"
22
23 [replay]
24 dir = "${cfg.dataDir}/replay"
25
26 [storage]
27 boltdb = "${cfg.dataDir}/kapacitor.db"
28
29 ${optionalString (cfg.loadDirectory != null) ''
30 [load]
31 enabled = true
32 dir = "${cfg.loadDirectory}"
33 ''}
34
35 ${optionalString (cfg.defaultDatabase.enable) ''
36 [[influxdb]]
37 name = "default"
38 enabled = true
39 default = true
40 urls = [ "${cfg.defaultDatabase.url}" ]
41 username = "${cfg.defaultDatabase.username}"
42 password = "${cfg.defaultDatabase.password}"
43 ''}
44
45 ${optionalString (cfg.alerta.enable) ''
46 [alerta]
47 enabled = true
48 url = "${cfg.alerta.url}"
49 token = "${cfg.alerta.token}"
50 environment = "${cfg.alerta.environment}"
51 origin = "${cfg.alerta.origin}"
52 ''}
53
54 ${cfg.extraConfig}
55 '';
56 };
57in
58{
59 options.services.kapacitor = {
60 enable = mkEnableOption "kapacitor";
61
62 dataDir = mkOption {
63 type = types.path;
64 example = "/var/lib/kapacitor";
65 default = "/var/lib/kapacitor";
66 description = "Location where Kapacitor stores its state";
67 };
68
69 port = mkOption {
70 type = types.int;
71 default = 9092;
72 description = "Port of Kapacitor";
73 };
74
75 bind = mkOption {
76 type = types.str;
77 default = "";
78 example = literalExample "0.0.0.0";
79 description = "Address to bind to. The default is to bind to all addresses";
80 };
81
82 extraConfig = mkOption {
83 description = "These lines go into kapacitord.conf verbatim.";
84 default = "";
85 type = types.lines;
86 };
87
88 user = mkOption {
89 type = types.str;
90 default = "kapacitor";
91 description = "User account under which Kapacitor runs";
92 };
93
94 group = mkOption {
95 type = types.str;
96 default = "kapacitor";
97 description = "Group under which Kapacitor runs";
98 };
99
100 taskSnapshotInterval = mkOption {
101 type = types.str;
102 description = "Specifies how often to snapshot the task state (in InfluxDB time units)";
103 default = "1m0s";
104 example = "1m0s";
105 };
106
107 loadDirectory = mkOption {
108 type = types.nullOr types.path;
109 description = "Directory where to load services from, such as tasks, templates and handlers (or null to disable service loading on startup)";
110 default = null;
111 };
112
113 defaultDatabase = {
114 enable = mkEnableOption "kapacitor.defaultDatabase";
115
116 url = mkOption {
117 description = "The URL to an InfluxDB server that serves as the default database";
118 example = "http://localhost:8086";
119 type = types.str;
120 };
121
122 username = mkOption {
123 description = "The username to connect to the remote InfluxDB server";
124 type = types.str;
125 };
126
127 password = mkOption {
128 description = "The password to connect to the remote InfluxDB server";
129 type = types.str;
130 };
131 };
132
133 alerta = {
134 enable = mkEnableOption "kapacitor alerta integration";
135
136 url = mkOption {
137 description = "The URL to the Alerta REST API";
138 default = "http://localhost:5000";
139 example = "http://localhost:5000";
140 type = types.str;
141 };
142
143 token = mkOption {
144 description = "Default Alerta authentication token";
145 type = types.str;
146 default = "";
147 };
148
149 environment = mkOption {
150 description = "Default Alerta environment";
151 type = types.str;
152 default = "Production";
153 };
154
155 origin = mkOption {
156 description = "Default origin of alert";
157 type = types.str;
158 default = "kapacitor";
159 };
160 };
161 };
162
163 config = mkIf cfg.enable {
164 environment.systemPackages = [ pkgs.kapacitor ];
165
166 systemd.tmpfiles.rules = [
167 "d '${cfg.dataDir}' - ${cfg.user} ${cfg.group} - -"
168 ];
169
170 systemd.services.kapacitor = {
171 description = "Kapacitor Real-Time Stream Processing Engine";
172 wantedBy = [ "multi-user.target" ];
173 after = [ "networking.target" ];
174 serviceConfig = {
175 ExecStart = "${pkgs.kapacitor}/bin/kapacitord -config ${kapacitorConf}";
176 User = "kapacitor";
177 Group = "kapacitor";
178 };
179 };
180
181 users.users.kapacitor = {
182 uid = config.ids.uids.kapacitor;
183 description = "Kapacitor user";
184 home = cfg.dataDir;
185 };
186
187 users.groups.kapacitor = {
188 gid = config.ids.gids.kapacitor;
189 };
190 };
191}