1{ config, pkgs, lib, ... }:
2let cfg = config.services.victoriametrics; in
3{
4 options.services.victoriametrics = with lib; {
5 enable = mkEnableOption "victoriametrics";
6 package = mkOption {
7 type = types.package;
8 default = pkgs.victoriametrics;
9 defaultText = "pkgs.victoriametrics";
10 description = ''
11 The VictoriaMetrics distribution to use.
12 '';
13 };
14 listenAddress = mkOption {
15 default = ":8428";
16 type = types.str;
17 description = ''
18 The listen address for the http interface.
19 '';
20 };
21 retentionPeriod = mkOption {
22 type = types.int;
23 default = 1;
24 description = ''
25 Retention period in months.
26 '';
27 };
28 extraOptions = mkOption {
29 type = types.listOf types.str;
30 default = [];
31 description = ''
32 Extra options to pass to VictoriaMetrics. See the README: <link
33 xlink:href="https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/README.md" />
34 or <command>victoriametrics -help</command> for more
35 information.
36 '';
37 };
38 };
39 config = lib.mkIf cfg.enable {
40 systemd.services.victoriametrics = {
41 description = "VictoriaMetrics time series database";
42 after = [ "network.target" ];
43 startLimitBurst = 5;
44 serviceConfig = {
45 Restart = "on-failure";
46 RestartSec = 1;
47 StateDirectory = "victoriametrics";
48 DynamicUser = true;
49 ExecStart = ''
50 ${cfg.package}/bin/victoria-metrics \
51 -storageDataPath=/var/lib/victoriametrics \
52 -httpListenAddr ${cfg.listenAddress} \
53 -retentionPeriod ${toString cfg.retentionPeriod} \
54 ${lib.escapeShellArgs cfg.extraOptions}
55 '';
56 };
57 wantedBy = [ "multi-user.target" ];
58
59 postStart =
60 let
61 bindAddr = (lib.optionalString (lib.hasPrefix ":" cfg.listenAddress) "127.0.0.1") + cfg.listenAddress;
62 in
63 lib.mkBefore ''
64 until ${lib.getBin pkgs.curl}/bin/curl -s -o /dev/null http://${bindAddr}/ping; do
65 sleep 1;
66 done
67 '';
68 };
69 };
70}