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