{ config, flake, ... }:
let
  inherit (flake.config.services) instances;
  inherit (flake.config.machines.devices) mars deimos ceres;
  service = instances.wireGuard;
in
{
  networking = {
    nat = {
      enable = true;
      enableIPv6 = true;
      externalInterface = "enp10s0";
      internalInterfaces = [
        "wg0"
        "br-vms"
      ];
    };

    wireguard.interfaces = {
      wg0 = {
        ips = [ "${ceres.wireguard.ip0}/24" ];
        listenPort = service.ports.port1;
        privateKeyFile = config.sops.secrets."${service.name}-private".path;
        peers = [
          # if you need to create a new key pair
          # wg genkey | save --raw --force privatekey
          # open privatekey | wg pubkey | save --raw --force publickey
          {
            publicKey = "9zfRPxkxTLHM9tABC8lIaDMrzdjcF2l1mtG82uqGKUQ=";
            allowedIPs = [ "${mars.wireguard.ip0}/32" ];
          }
          {
            publicKey = "hKbvOlvKdWAlq45rfV3ggwOI8xqiqVWweXV+2GQx/0I=";
            allowedIPs = [ "${deimos.wireguard.ip0}/32" ];
          }
        ];
      };
    };
  };

  sops =
    let
      sopsPath = secret: {
        path = "${service.sops.path0}/${service.name}-${secret}-pass";
        owner = "root";
        mode = "600";
      };
    in
    {
      secrets = builtins.listToAttrs (
        map
          (secret: {
            name = "${service.name}-${secret}";
            value = sopsPath secret;
          })
          [
            "private"
            "public"
          ]
      );
    };
}
