summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/knot-resolver.nix
blob: 4aa8d47f110ab4db448b194aca13b4ce4d2e54de (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.knot-resolver;
  # pkgs.writers.yaml_1_1.generate with additional kresctl validate
  configFile = pkgs.callPackage (
    {
      runCommandLocal,
      remarshal,
      stdenv,
    }:
    runCommandLocal "knot-resolver.yaml"
      {
        nativeBuildInputs = [ remarshal ];
        value = builtins.toJSON cfg.settings;
        passAsFile = [ "value" ];
      }
      ''
        remarshal --from json --to yaml-1.1 "$valuePath" "$out"
        ${
          # We skip validation if the build platform cannot execute # the binary targeting the host platform.
          lib.optionalString (stdenv.buildPlatform.canExecute stdenv.hostPlatform) ''
            ${cfg.managerPackage}/bin/kresctl validate "$out"
          ''
        }
      ''
  ) { };
in
{
  meta.maintainers = [
    lib.maintainers.vcunat # upstream developer
    lib.maintainers.leona
    lib.maintainers.osnyx
  ];

  ###### interface
  options.services.knot-resolver = {
    enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Whether to enable knot-resolver (version 6) domain name server.
        DNSSEC validation is turned on by default.
        If you want to use knot-resolver 5, please use services.kresd.
      '';
    };
    managerPackage = lib.mkPackageOption pkgs "knot-resolver-manager_6" {
      example = "pkgs.knot-resolver-manager_6.override { extraFeatures = true; }";
    };
    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = (pkgs.formats.yaml { }).type;
        options = {
          network.listen = lib.mkOption {
            type = lib.types.listOf (
              lib.types.submodule {
                freeformType = (pkgs.formats.yaml { }).type;
              }
            );
            description = "List of interfaces to listen to and its configuration.";
            default = [
              {
                interface = [ "127.0.0.1" ];
                kind = "dns";
                freebind = false;
              }
            ]
            ++ lib.optionals config.networking.enableIPv6 [
              {
                interface = [ "::1" ];
                kind = "dns";
                freebind = false;
              }
            ];
            defaultText = lib.literalExpression ''
              [
                {
                  interface = [ "127.0.0.1" ];
                  kind = "dns";
                  freebind = false;
                }
              ]
              ++ lib.optionals config.networking.enableIPv6 [
                {
                  interface = [ "::1" ];
                  kind = "dns";
                  freebind = false;
                }
               ];
            '';
          };
          workers = lib.mkOption {
            type = lib.types.oneOf [
              (lib.types.enum [ "auto" ])
              lib.types.ints.unsigned
            ];
            default = 1;
            description = ''
              The number of running kresd (Knot Resolver daemon) workers. If set to 'auto', it is equal to number of CPUs available.
            '';
          };
        };
      };
      default = { };
      description = ''
        Nix-based (RFC 42) configuration for Knot Resolver.
        For configuration reference (described as YAML) see
        <https://www.knot-resolver.cz/documentation/latest/config-overview.html>
      '';
    };
  };

  ###### implementation
  config = lib.mkIf cfg.enable {
    users.users.knot-resolver = {
      isSystemUser = true;
      group = "knot-resolver";
      description = "Knot-resolver daemon user";
    };
    users.groups.knot-resolver = { };
    networking.resolvconf.useLocalResolver = lib.mkDefault true;

    environment = {
      etc."knot-resolver/config.yaml".source = configFile;
      systemPackages = [
        # We just avoid including the other binaries, e.g. supervisorctl.
        (pkgs.runCommandLocal "knot-resolver-cmds" { } ''
          mkdir -p "$out/bin"
          ln -s '${cfg.managerPackage}/bin/kresctl' "$out/bin/"
        '')
      ];
    };

    systemd.packages = [ cfg.managerPackage.kresd ]; # the unit gets patched a bit just below
    systemd.services."knot-resolver" = {
      wantedBy = [ "multi-user.target" ];
      stopIfChanged = false;
      reloadTriggers = [
        configFile
      ];
      serviceConfig = {
        ExecStart = "${cfg.managerPackage}/bin/knot-resolver";
        ExecReload = "${cfg.managerPackage}/bin/kresctl reload";

        StateDirectory = "knot-resolver";
        StateDirectoryMode = "0770";

        RuntimeDirectory = "knot-resolver";
        RuntimeDirectoryMode = "0770";

        CacheDirectory = "knot-resolver";
        CacheDirectoryMode = "0770";
      };
    };
  };
}