1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 cfg = config.services.technitium-dns-server;
10 inherit (lib)
11 mkEnableOption
12 mkPackageOption
13 mkOption
14 mkIf
15 types
16 ;
17in
18{
19 options.services.technitium-dns-server = {
20 enable = mkEnableOption "Technitium DNS Server";
21
22 package = mkPackageOption pkgs "technitium-dns-server" { };
23
24 openFirewall = mkOption {
25 type = types.bool;
26 default = false;
27 description = ''
28 Whether to open ports in the firewall.
29 Standard ports are 53 (UDP and TCP, for DNS), 5380 and 53443 (TCP, HTTP and HTTPS for web interface).
30 Specify different or additional ports in options firewallUDPPorts and firewallTCPPorts if necessary.
31 '';
32 };
33
34 firewallUDPPorts = mkOption {
35 type = with types; listOf int;
36 default = [ 53 ];
37 description = ''
38 List of UDP ports to open in firewall.
39 '';
40 };
41
42 firewallTCPPorts = mkOption {
43 type = with types; listOf int;
44 default = [
45 53
46 5380 # web interface HTTP
47 53443 # web interface HTTPS
48 ];
49 description = ''
50 List of TCP ports to open in firewall.
51 You might want to open ports 443 and 853 if you intend to use DNS over HTTPS or DNS over TLS.
52 '';
53 };
54 };
55
56 config = mkIf cfg.enable {
57 systemd.services.technitium-dns-server = {
58 description = "Technitium DNS Server";
59 wantedBy = [ "multi-user.target" ];
60 after = [ "network.target" ];
61
62 serviceConfig = {
63 ExecStart = "${cfg.package}/bin/technitium-dns-server $STATE_DIRECTORY";
64
65 DynamicUser = true;
66
67 StateDirectory = "technitium-dns-server";
68
69 Restart = "always";
70 RestartSec = 10;
71 TimeoutStopSec = 10;
72 KillSignal = "SIGINT";
73
74 # Harden the service
75 LockPersonality = true;
76 NoNewPrivileges = true;
77 PrivateDevices = true;
78 PrivateMounts = true;
79 PrivateTmp = true;
80 ProtectClock = true;
81 ProtectControlGroups = true;
82 ProtectHome = true;
83 ProtectHostname = true;
84 ProtectKernelLogs = true;
85 ProtectKernelModules = true;
86 ProtectKernelTunables = true;
87 ProtectSystem = "strict";
88 RemoveIPC = true;
89 RestrictAddressFamilies = "AF_INET AF_INET6 AF_UNIX AF_NETLINK";
90 RestrictNamespaces = true;
91 RestrictRealtime = true;
92 RestrictSUIDSGID = true;
93
94 AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
95 CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
96 };
97 };
98
99 networking.firewall = mkIf cfg.openFirewall {
100 allowedUDPPorts = cfg.firewallUDPPorts;
101 allowedTCPPorts = cfg.firewallTCPPorts;
102 };
103 };
104
105 meta.maintainers = with lib.maintainers; [ fabianrig ];
106}