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