about summary refs log tree commit diff
path: root/nixos/modules
diff options
context:
space:
mode:
authorLeona Maroni <dev@leona.is>2024-04-21 23:02:44 +0200
committerLeona Maroni <dev@leona.is>2024-04-28 13:55:47 +0200
commit6c69cfb804fd1f3e55e87b90d219394edf9e6fb9 (patch)
tree73118993cb910697a4c5283f5d6b473973bf0cfe /nixos/modules
parente0638909ae5b988b6154b75a28482b44e96270fc (diff)
nixos/vmagent: use dynamic user and cache directory
Diffstat (limited to 'nixos/modules')
-rw-r--r--nixos/modules/services/monitoring/vmagent.nix94
1 files changed, 46 insertions, 48 deletions
diff --git a/nixos/modules/services/monitoring/vmagent.nix b/nixos/modules/services/monitoring/vmagent.nix
index 0ce23c5ec7314..4838e0709d09e 100644
--- a/nixos/modules/services/monitoring/vmagent.nix
+++ b/nixos/modules/services/monitoring/vmagent.nix
@@ -4,41 +4,40 @@ let
   cfg = config.services.vmagent;
   settingsFormat = pkgs.formats.json { };
 in {
+  imports = [
+    (lib.mkRemovedOptionModule [ "services" "vmagent" "dataDir" ] "dataDir has been deprecated in favor of systemd provided CacheDirectory")
+    (lib.mkRemovedOptionModule [ "services" "vmagent" "user" ] "user has been deprecated in favor of systemd DynamicUser")
+    (lib.mkRemovedOptionModule [ "services" "vmagent" "group" ] "group has been deprecated in favor of systemd DynamicUser")
+    (lib.mkRenamedOptionModule [ "services" "vmagent" "remoteWriteUrl" ] [ "services" "vmagent" "remoteWrite" "url" ])
+  ];
+
   options.services.vmagent = {
     enable = lib.mkEnableOption "vmagent";
 
-    user = lib.mkOption {
-      default = "vmagent";
-      type = lib.types.str;
-      description = ''
-        User account under which vmagent runs.
-      '';
-    };
-
-    group = lib.mkOption {
-      type = lib.types.str;
-      default = "vmagent";
-      description = ''
-        Group under which vmagent runs.
-      '';
-    };
-
     package = lib.mkPackageOption pkgs "vmagent" { };
 
-    dataDir = lib.mkOption {
-      type = lib.types.str;
-      default = "/var/lib/vmagent";
-      description = ''
-        The directory where vmagent stores its data files.
-      '';
-    };
-
-    remoteWriteUrl = lib.mkOption {
-      default = "http://localhost:8428/api/v1/write";
-      type = lib.types.str;
-      description = ''
-        The storage endpoint such as VictoriaMetrics
-      '';
+    remoteWrite = {
+      url = lib.mkOption {
+        default = null;
+        type = lib.types.nullOr lib.types.str;
+        description = ''
+          Endpoint for prometheus compatible remote_write
+        '';
+      };
+      basicAuthUsername = lib.mkOption {
+        default = null;
+        type = lib.types.nullOr lib.types.str;
+        description = ''
+          Basic Auth username used to connect to remote_write endpoint
+        '';
+      };
+      basicAuthPasswordFile = lib.mkOption {
+        default = null;
+        type = lib.types.nullOr lib.types.str;
+        description = ''
+          File that contains the Basic Auth password used to connect to remote_write endpoint
+        '';
+      };
     };
 
     prometheusConfig = lib.mkOption {
@@ -68,36 +67,35 @@ in {
   };
 
   config = lib.mkIf cfg.enable {
-    users.groups = lib.mkIf (cfg.group == "vmagent") { vmagent = { }; };
-
-    users.users = lib.mkIf (cfg.user == "vmagent") {
-      vmagent = {
-        group = cfg.group;
-        description = "vmagent daemon user";
-        home = cfg.dataDir;
-        isSystemUser = true;
-      };
-    };
-
     networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ 8429 ];
 
     systemd.services.vmagent = let
       prometheusConfig = settingsFormat.generate "prometheusConfig.yaml" cfg.prometheusConfig;
+      startCommandLine = lib.concatStringsSep " " ([
+        "${cfg.package}/bin/vmagent"
+        "-promscrape.config=${prometheusConfig}"
+      ] ++ cfg.extraArgs
+        ++ lib.optionals (cfg.remoteWrite.url != null) [
+        "-remoteWrite.url=${cfg.remoteWrite.url}"
+        "-remoteWrite.tmpDataPath=%C/vmagent/remote_write_tmp"
+      ] ++ lib.optional (cfg.remoteWrite.basicAuthUsername != null) "-remoteWrite.basicAuth.username=${cfg.remoteWrite.basicAuthUsername}"
+        ++ lib.optional (cfg.remoteWrite.basicAuthPasswordFile != null) "-remoteWrite.basicAuth.passwordFile=\${CREDENTIALS_DIRECTORY}/remote_write_basic_auth_password");
     in {
       wantedBy = [ "multi-user.target" ];
       after = [ "network.target" ];
       description = "vmagent system service";
       serviceConfig = {
-        User = cfg.user;
-        Group = cfg.group;
+        DynamicUser = true;
+        User = "vmagent";
+        Group = "vmagent";
         Type = "simple";
         Restart = "on-failure";
-        WorkingDirectory = cfg.dataDir;
-        ExecStart = "${cfg.package}/bin/vmagent -remoteWrite.url=${cfg.remoteWriteUrl} -promscrape.config=${prometheusConfig} ${lib.escapeShellArgs cfg.extraArgs}";
+        CacheDirectory = "vmagent";
+        ExecStart = startCommandLine;
+        LoadCredential = lib.optional (cfg.remoteWrite.basicAuthPasswordFile != null) [
+          "remote_write_basic_auth_password:${cfg.remoteWrite.basicAuthPasswordFile}"
+        ];
       };
     };
-
-    systemd.tmpfiles.rules =
-      [ "d '${cfg.dataDir}' 0755 ${cfg.user} ${cfg.group} -" ];
   };
 }