Skip to content
Snippets Groups Projects
client.nix 2.68 KiB
Newer Older
# Client section of our Monitoring VPN config

Florian Sesser's avatar
Florian Sesser committed
{ lib, config, ... }: let
  cfg = config.services.private-storage.monitoring.vpn;
Florian Sesser's avatar
Florian Sesser committed
in {
Florian Sesser's avatar
Florian Sesser committed
  options.services.private-storage.monitoring.vpn.client = {
    enable = lib.mkEnableOption "PrivateStorageio Monitoring VPN client service";
    privateKeyFile = lib.mkOption {
      type = lib.types.str;
      example = lib.literalExample "/var/secrets/monitoring-vpn/host.key";
Florian Sesser's avatar
Florian Sesser committed
      default = "/var/secrets/monitoring-vpn/client.key";
Florian Sesser's avatar
Florian Sesser committed
      description = ''
Florian Sesser's avatar
Florian Sesser committed
        File with base64 private key generated by <command>wg genkey</command>.
Florian Sesser's avatar
Florian Sesser committed
      '';
    };
    publicKeyFile = lib.mkOption {
      type = lib.types.str;
      example = lib.literalExample "/var/secrets/monitoring-vpn/host.pub";
      description = ''
Florian Sesser's avatar
Florian Sesser committed
        File with base64 public key generated by <command>cat private.key | wg pubkey > pubkey.pub</command>.
Florian Sesser's avatar
Florian Sesser committed
      '';
    };
    allowedIPs = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      example = lib.literalExample [ "172.23.23.1/32" ];
      description = ''
        Limits which IPs this client receives data from.
      '';
    };
    ips = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      example = lib.literalExample [ "172.23.23.11/24" ];
      default = [ "172.23.23.21/24" ];
      description = ''
        The IP addresses of the interface.
        See https://github.com/NixOS/nixpkgs/blob/nixos-20.09/nixos/modules/services/networking/wireguard.nix .
      '';
    };
Florian Sesser's avatar
Florian Sesser committed
    endpoint = lib.mkOption {
      type = lib.types.str;
      example = lib.literalExample "vpn.monitoring.private.storage:54321";
      default = "192.168.67.21:54321";
      description = ''
        The address and port number of the server to establish the VPN with.
      '';
    };
    endpointPublicKeyFile = lib.mkOption {
      type = lib.types.str;
      example = lib.literalExample "/var/secrets/monitoring-vpn/server.pub";
      description = ''
        File with base64 public key generated by <command>cat private.key | wg pubkey > pubkey.pub</command>.
      '';
    };
Florian Sesser's avatar
Florian Sesser committed
  };

  config = lib.mkIf cfg.client.enable {
    networking.wireguard.interfaces.monitoringvpn = {
      ips = cfg.client.ips;
      privateKeyFile = cfg.client.privateKeyFile;
      peers = [
        {
          allowedIPs = cfg.client.allowedIPs;
Florian Sesser's avatar
Florian Sesser committed
          endpoint = cfg.client.endpoint;  # meaning: the server.
          publicKey = builtins.readFile(cfg.client.endpointPublicKeyFile);
Florian Sesser's avatar
Florian Sesser committed
        }
      ];
    };
Florian Sesser's avatar
Florian Sesser committed
# v just have all config static (no file systems etc)
# move cfg into global config (like config.privatestorage.monitoring.*)
# parametrize keys
#   - (https://wiki.archlinux.org/index.php/WireGuard
#   -  (wg genkey | tee peer_A.key | wg pubkey > peer_A.pub)