1{ lib, pkgs, config, ... }:
2
3with lib;
4
5let
6 inherit (lib.types) attrsOf coercedTo listOf oneOf str int bool;
7 cfg = config.services.smartdns;
8
9 confFile = pkgs.writeText "smartdns.conf" (with generators;
10 toKeyValue {
11 mkKeyValue = mkKeyValueDefault {
12 mkValueString = v:
13 if isBool v then
14 if v then "yes" else "no"
15 else
16 mkValueStringDefault { } v;
17 } " ";
18 listsAsDuplicateKeys =
19 true; # Allowing duplications because we need to deal with multiple entries with the same key.
20 } cfg.settings);
21in {
22 options.services.smartdns = {
23 enable = mkEnableOption (lib.mdDoc "SmartDNS DNS server");
24
25 bindPort = mkOption {
26 type = types.port;
27 default = 53;
28 description = lib.mdDoc "DNS listening port number.";
29 };
30
31 settings = mkOption {
32 type =
33 let atom = oneOf [ str int bool ];
34 in attrsOf (coercedTo atom toList (listOf atom));
35 example = literalExpression ''
36 {
37 bind = ":5353 -no-rule -group example";
38 cache-size = 4096;
39 server-tls = [ "8.8.8.8:853" "1.1.1.1:853" ];
40 server-https = "https://cloudflare-dns.com/dns-query -exclude-default-group";
41 prefetch-domain = true;
42 speed-check-mode = "ping,tcp:80";
43 };
44 '';
45 description = lib.mdDoc ''
46 A set that will be generated into configuration file, see the [SmartDNS README](https://github.com/pymumu/smartdns/blob/master/ReadMe_en.md#configuration-parameter) for details of configuration parameters.
47 You could override the options here like {option}`services.smartdns.bindPort` by writing `settings.bind = ":5353 -no-rule -group example";`.
48 '';
49 };
50 };
51
52 config = lib.mkIf cfg.enable {
53 services.smartdns.settings.bind = mkDefault ":${toString cfg.bindPort}";
54
55 systemd.packages = [ pkgs.smartdns ];
56 systemd.services.smartdns.wantedBy = [ "multi-user.target" ];
57 systemd.services.smartdns.restartTriggers = [ confFile ];
58 environment.etc."smartdns/smartdns.conf".source = confFile;
59 environment.etc."default/smartdns".source =
60 "${pkgs.smartdns}/etc/default/smartdns";
61 };
62}