1{
2 config,
3 pkgs,
4 lib,
5 utils,
6 ...
7}:
8
9let
10 cfg = config.services.vlagent;
11
12 startCLIList = [
13 (lib.getExe cfg.package)
14 ]
15 ++ lib.optionals (cfg.remoteWrite.url != null) [
16 "-remoteWrite.url=${cfg.remoteWrite.url}"
17 "-remoteWrite.tmpDataPath=%C/vlagent/remote_write_tmp"
18 ]
19 ++ lib.optionals (cfg.remoteWrite.basicAuthPasswordFile != null) [
20 "-remoteWrite.basicAuth.passwordFile=%d/remote_write_basic_auth_password"
21 ]
22 ++ lib.optionals (cfg.remoteWrite.basicAuthUsername != null) [
23 "-remoteWrite.basicAuth.username=${cfg.remoteWrite.basicAuthUsername}"
24 ]
25 ++ lib.optionals (cfg.remoteWrite.maxDiskUsagePerUrl != null) [
26 "-remoteWrite.maxDiskUsagePerUrl=${cfg.remoteWrite.maxDiskUsagePerUrl}"
27 ];
28in
29{
30 meta = {
31 maintainers = [ lib.maintainers.shawn8901 ];
32 };
33
34 options.services.vlagent = {
35 enable = lib.mkOption {
36 type = lib.types.bool;
37 default = false;
38 description = ''
39 Whether to enable VictoriaMetrics's `vlagent`.
40
41 `vlagent` is a tiny agent which helps you collect logs from various sources and store them in VictoriaLogs .
42 '';
43 };
44
45 package = lib.mkPackageOption pkgs "vlagent" { };
46
47 remoteWrite = {
48 url = lib.mkOption {
49 default = null;
50 type = lib.types.nullOr lib.types.str;
51 description = ''
52 Endpoint for the victorialogs instance
53 '';
54 };
55 maxDiskUsagePerUrl = lib.mkOption {
56 default = null;
57 type = lib.types.nullOr lib.types.str;
58 description = ''
59 The maximum file-based buffer size in bytes. Supports the following optional suffixes for size values: KB, MB, GB, TB, KiB, MiB, GiB, TiB.
60 See docs for more infomations: <https://docs.victoriametrics.com/vlagent.html#advanced-usage>
61 '';
62 };
63 basicAuthUsername = lib.mkOption {
64 default = null;
65 type = lib.types.nullOr lib.types.str;
66 description = ''
67 Basic Auth username used to connect to remote_write endpoint
68 '';
69 };
70 basicAuthPasswordFile = lib.mkOption {
71 default = null;
72 type = lib.types.nullOr lib.types.str;
73 description = ''
74 File that contains the Basic Auth password used to connect to remote_write endpoint
75 '';
76 };
77 };
78
79 openFirewall = lib.mkOption {
80 type = lib.types.bool;
81 default = false;
82 description = ''
83 Whether to open the firewall for the default ports.
84 '';
85 };
86
87 extraArgs = lib.mkOption {
88 type = lib.types.listOf lib.types.str;
89 default = [ ];
90 description = ''
91 Extra args to pass to `vlagent`. See the docs:
92 <https://docs.victoriametrics.com/vlagent.html#advanced-usage>
93 or {command}`vlagent -help` for more information.
94 '';
95 };
96 };
97
98 config = lib.mkIf cfg.enable {
99
100 assertions = [
101 {
102 assertion =
103 (cfg.remoteWrite.basicAuthUsername == null && cfg.remoteWrite.basicAuthPasswordFile == null)
104 || (cfg.remoteWrite.basicAuthUsername != null && cfg.remoteWrite.basicAuthPasswordFile != null);
105 message = "Both basicAuthUsername and basicAuthPasswordFile must be set together to enable basicAuth functionality, or neither should be set.";
106 }
107 ];
108
109 networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ 9429 ];
110
111 systemd.services.vlagent = {
112 wantedBy = [ "multi-user.target" ];
113 after = [ "network.target" ];
114 description = "vlagent system service";
115 serviceConfig = {
116 DynamicUser = true;
117 User = "vlagent";
118 Group = "vlagent";
119 Type = "simple";
120 Restart = "on-failure";
121 CacheDirectory = "vlagent";
122 ExecStart = lib.concatStringsSep " " [
123 (lib.escapeShellArgs startCLIList)
124 (utils.escapeSystemdExecArgs cfg.extraArgs)
125 ];
126 LoadCredential = lib.optional (
127 cfg.remoteWrite.basicAuthPasswordFile != null
128 ) "remote_write_basic_auth_password:${cfg.remoteWrite.basicAuthPasswordFile}";
129 };
130 };
131 };
132}