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 (lib.mdDoc "kapacitor");
61
62 dataDir = mkOption {
63 type = types.path;
64 default = "/var/lib/kapacitor";
65 description = lib.mdDoc "Location where Kapacitor stores its state";
66 };
67
68 port = mkOption {
69 type = types.port;
70 default = 9092;
71 description = lib.mdDoc "Port of Kapacitor";
72 };
73
74 bind = mkOption {
75 type = types.str;
76 default = "";
77 example = "0.0.0.0";
78 description = lib.mdDoc "Address to bind to. The default is to bind to all addresses";
79 };
80
81 extraConfig = mkOption {
82 description = lib.mdDoc "These lines go into kapacitord.conf verbatim.";
83 default = "";
84 type = types.lines;
85 };
86
87 user = mkOption {
88 type = types.str;
89 default = "kapacitor";
90 description = lib.mdDoc "User account under which Kapacitor runs";
91 };
92
93 group = mkOption {
94 type = types.str;
95 default = "kapacitor";
96 description = lib.mdDoc "Group under which Kapacitor runs";
97 };
98
99 taskSnapshotInterval = mkOption {
100 type = types.str;
101 description = lib.mdDoc "Specifies how often to snapshot the task state (in InfluxDB time units)";
102 default = "1m0s";
103 };
104
105 loadDirectory = mkOption {
106 type = types.nullOr types.path;
107 description = lib.mdDoc "Directory where to load services from, such as tasks, templates and handlers (or null to disable service loading on startup)";
108 default = null;
109 };
110
111 defaultDatabase = {
112 enable = mkEnableOption (lib.mdDoc "kapacitor.defaultDatabase");
113
114 url = mkOption {
115 description = lib.mdDoc "The URL to an InfluxDB server that serves as the default database";
116 example = "http://localhost:8086";
117 type = types.str;
118 };
119
120 username = mkOption {
121 description = lib.mdDoc "The username to connect to the remote InfluxDB server";
122 type = types.str;
123 };
124
125 password = mkOption {
126 description = lib.mdDoc "The password to connect to the remote InfluxDB server";
127 type = types.str;
128 };
129 };
130
131 alerta = {
132 enable = mkEnableOption (lib.mdDoc "kapacitor alerta integration");
133
134 url = mkOption {
135 description = lib.mdDoc "The URL to the Alerta REST API";
136 default = "http://localhost:5000";
137 type = types.str;
138 };
139
140 token = mkOption {
141 description = lib.mdDoc "Default Alerta authentication token";
142 type = types.str;
143 default = "";
144 };
145
146 environment = mkOption {
147 description = lib.mdDoc "Default Alerta environment";
148 type = types.str;
149 default = "Production";
150 };
151
152 origin = mkOption {
153 description = lib.mdDoc "Default origin of alert";
154 type = types.str;
155 default = "kapacitor";
156 };
157 };
158 };
159
160 config = mkIf cfg.enable {
161 environment.systemPackages = [ pkgs.kapacitor ];
162
163 systemd.tmpfiles.rules = [
164 "d '${cfg.dataDir}' - ${cfg.user} ${cfg.group} - -"
165 ];
166
167 systemd.services.kapacitor = {
168 description = "Kapacitor Real-Time Stream Processing Engine";
169 wantedBy = [ "multi-user.target" ];
170 after = [ "networking.target" ];
171 serviceConfig = {
172 ExecStart = "${pkgs.kapacitor}/bin/kapacitord -config ${kapacitorConf}";
173 User = "kapacitor";
174 Group = "kapacitor";
175 };
176 };
177
178 users.users.kapacitor = {
179 uid = config.ids.uids.kapacitor;
180 description = "Kapacitor user";
181 home = cfg.dataDir;
182 };
183
184 users.groups.kapacitor = {
185 gid = config.ids.gids.kapacitor;
186 };
187 };
188}