{ lib, config, ... }:

with lib;

{
  config = {
    services.borgsnap = {
      enable = true;
      target = "borg.vidhar:.";

      extraConfig = mkForce {
        daily = "31";
        monthly = "-1";
      };

      sshConfig = ''
        Include /etc/ssh/ssh_config

        ControlMaster auto
        ControlPath /var/lib/borg/.borgssh-master-%r@%n:%p
        ControlPersist yes

        Host borg.vidhar
          HostName vidhar.yggdrasil.li
          User borg
          IdentityFile ${config.sops.secrets."append.borg.vidhar".path}
          IdentitiesOnly yes

          BatchMode yes
          ServerAliveInterval 10
          ServerAliveCountMax 30

          IPQoS cs1
      '';
    };

    sops.secrets."append.borg.vidhar" = {
      format = "binary";
      sopsFile = ../vidhar/borg/jotnar/surtr;
      owner = "borg";
      group = "borg";
      mode = "0400";
    };

    users.users.borg = {
      useDefaultShell = true;
      isSystemUser = true;
      group = "borg";
    };
    users.groups.borg = {};
  };
}