summary refs log tree commit diff stats
path: root/system/modules/nextdns.nix
blob: 830215e677afe79c9cf55e0b8329a3a323e3ecdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{ config, lib, pkgs, ... }:

with lib;

let
  inherit (pkgs) stdenv;

  cfg = config.networking.nextdns;

  identifyingPrefix = if cfg.identifyDevice then "${config.networking.hostName}-" else "";

  kresdConfig = {
    enable = true;
    extraConfig = ''
      policy.add(policy.all(policy.TLS_FORWARD({
        {'45.90.28.0', hostname='${identifyingPrefix}${cfg.configID}.dns1.nextdns.io'},
        {'2a07:a8c0::', hostname='${identifyingPrefix}${cfg.configID}.dns1.nextdns.io'},
        {'45.90.30.0', hostname='${identifyingPrefix}${cfg.configID}.dns2.nextdns.io'},
        {'2a07:a8c1::', hostname='${identifyingPrefix}${cfg.configID}.dns2.nextdns.io'}
      })))
    '';
  };

  stubbyConfig = {
    enable = true;
    fallbackProtocols = lib.mkDefault [ "GETDNS_TRANSPORT_TLS" ];
    roundRobinUpstreams = lib.mkDefault false;
    upstreamServers = ''
      - address_data: 45.90.28.0
        tls_auth_name: "${identifyingPrefix}${cfg.configID}.dns1.nextdns.io"
      - address_data: 2a07:a8c0::0
        tls_auth_name: "${identifyingPrefix}${cfg.configID}.dns1.nextdns.io"
      - address_data: 45.90.30.0
        tls_auth_name: "${identifyingPrefix}${cfg.configID}.dns2.nextdns.io"
      - address_data: 2a07:a8c1::0
        tls_auth_name: "${identifyingPrefix}${cfg.configID}.dns2.nextdns.io"
    '';
  };
in
{
  options = {
    networking.nextdns.enable = mkOption {
      type = types.bool;
      default = false;
      description = "Whether to enable DNS resolution via NextDNS";
    };

    networking.nextdns.configID = mkOption {
      type = types.str;
      default = "";
      example = literalExample "abcdef";
      description = "NextDNS configuration ID";
    };

    networking.nextdns.identifyDevice = mkOption {
      type = types.bool;
      default = false;
      description = "Whether to send hostname for identifying in your logs";
    };

    networking.nextdns.resolver = mkOption {
      type = types.enum [ "kresd" "stubby" ];
      default = if stdenv.isDarwin then "stubby" else "kresd";
      description = "Resolver to use";
    };
  };

  config = mkIf cfg.enable {

    assertions = [
      {
        assertion = !(stdenv.isDarwin && cfg.resolver == "kresd");
        message = "kresd is not supported on Darwin";
      }
    ];
    networking = if stdenv.isDarwin then
    {
      dns = [
        "::1"
        "127.0.0.1"
        "2a07:a8c0::ab:d6e5"
        "2a07:a8c1::ab:d6e5"
        "45.90.28.25"
        "45.90.30.25"
      ];
    } else {
      networkmanager.dns = "none";
      resolvconf.useLocalResolver = true;
    };
    services = {
      stubby = mkIf (cfg.resolver == "stubby") stubbyConfig;
    } // mkIf (!stdenv.isDarwin) {
      kresd = mkIf (cfg.resolver == "kresd") kresdConfig;
    };
  };
}