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