{ config, lib, ... }:
let
  cfg = config.services.victorialogs;
in
{
  options.services.victorialogs.bindAddress = lib.mkOption {
    readOnly = true;
    type = lib.types.str;
    description = ''
      Final address on which victorialogs listens.
    '';
  };

  config = {
    services.victorialogs = {
      enable = true;
      bindAddress =
        (lib.optionalString (lib.hasPrefix ":" cfg.listenAddress) "127.0.0.1") + cfg.listenAddress;
    };

    services.journald.upload = {
      enable = true;
      settings.Upload = {
        URL = "http://${cfg.bindAddress}/insert/journald";
        NetworkTimeoutSec = "20s";
      };
    };
    systemd.services."systemd-journal-upload".after = [ "victorialogs.service" ];
  };
}
