Skip to content
Snippets Groups Projects
server.nix 2.35 KiB
Newer Older
Florian Sesser's avatar
Florian Sesser committed
# Server section of our Monitoring VPN config

{ lib, config, ... }: let
  cfg = config.services.private-storage.monitoring.vpn;

in {
  options.services.private-storage.monitoring.vpn.server = {
    enable = lib.mkEnableOption "PrivateStorageio Monitoring VPN server service";
    privateKeyFile = lib.mkOption {
      type = lib.types.path;
      example = lib.literalExample /var/secrets/monitoringvpn/server.key;
      default = /var/secrets/monitoringvpn/server.key;
      description = ''
        File with base64 private key generated by <command>wg genkey</command>.
      '';
    };
    publicKeyFile = lib.mkOption {
      type = lib.types.path;
      example = lib.literalExample /var/secrets/monitoringvpn/server.pub;
      default = /var/secrets/monitoringvpn/server.pub;
      description = ''
        File with base64 public key generated by <command>cat private.key | wg pubkey > pubkey.pub</command>.
      '';
    };
    presharedKeyFile = lib.mkOption {
      type = lib.types.path;
      example = lib.literalExample /var/secrets/monitoringvpn/preshared.key;
      default = /var/secrets/monitoringvpn/preshared.key;
      description = ''
        File with base64 preshared key generated by <command>wg genpsk</command>.
      '';
    };
Florian Sesser's avatar
Florian Sesser committed
    ip = lib.mkOption {
      type = lib.types.str;
      example = lib.literalExample [ "172.23.23.23" ];
Florian Sesser's avatar
Florian Sesser committed
      description = ''
Florian Sesser's avatar
Florian Sesser committed
        The IP address of the interface.
Florian Sesser's avatar
Florian Sesser committed
      '';
    };
    port = lib.mkOption {
      type = lib.types.port;
      example = lib.literalExample 54321;
      default = 54321;
      description = ''
        The UDP port to listen on.
      '';
    };
  };

  config = lib.mkIf cfg.server.enable {
    networking.firewall.allowedUDPPorts = [ cfg.server.port ];

    networking.wireguard.interfaces.monitoringvpn = {
Florian Sesser's avatar
Florian Sesser committed
      ips = [ "${cfg.server.ip}/24" ];
Florian Sesser's avatar
Florian Sesser committed
      listenPort = cfg.server.port;
      privateKeyFile = toString cfg.server.privateKeyFile;
      peers = [
        { # node1
Florian Sesser's avatar
Florian Sesser committed
          allowedIPs = [ "172.23.23.11/32" ];
Florian Sesser's avatar
Florian Sesser committed
          publicKey = "tZ295cvD98ixt/VH4dwPKNgHf9MuhuzsossOWBOOoGU=";
          presharedKeyFile = toString cfg.server.presharedKeyFile;
Florian Sesser's avatar
Florian Sesser committed
        }
        { # node2
Florian Sesser's avatar
Florian Sesser committed
          allowedIPs = [ "172.23.23.12/32" ];
Florian Sesser's avatar
Florian Sesser committed
          publicKey = "zDxWTejJDXRRmUiMZPC7eVSCDdyFikN9VI6cqapQ6RY=";
          presharedKeyFile = toString cfg.server.presharedKeyFile;