1 { config, lib, pkgs, ... }:
3 cfg = config.services.dnscache;
5 dnscache-root = pkgs.runCommand "dnscache-root" { preferLocalBuild = true; } ''
6 mkdir -p $out/{servers,ip}
8 ${lib.concatMapStrings (ip: ''
9 touch "$out/ip/"${lib.escapeShellArg ip}
12 ${lib.concatStrings (lib.mapAttrsToList (host: ips: ''
13 ${lib.concatMapStrings (ip: ''
14 echo ${lib.escapeShellArg ip} >> "$out/servers/"${lib.escapeShellArg host}
16 '') cfg.domainServers)}
18 # if a list of root servers was not provided in config, copy it
19 # over. (this is also done by dnscache-conf, but we 'rm -rf
20 # /var/lib/dnscache/root' below & replace it wholesale with this,
21 # so we have to ensure servers/@ exists ourselves.)
22 if [ ! -e $out/servers/@ ]; then
23 # symlink does not work here, due chroot
24 cp ${pkgs.djbdns}/etc/dnsroots.global $out/servers/@;
35 enable = lib.mkOption {
37 type = lib.types.bool;
38 description = "Whether to run the dnscache caching dns server.";
44 description = "IP address on which to listen for connections.";
47 clientIps = lib.mkOption {
48 default = [ "127.0.0.1" ];
49 type = lib.types.listOf lib.types.str;
50 description = "Client IP addresses (or prefixes) from which to accept connections.";
51 example = ["192.168" "172.23.75.82"];
54 domainServers = lib.mkOption {
56 type = lib.types.attrsOf (lib.types.listOf lib.types.str);
58 Table of {hostname: server} pairs to use as authoritative servers for hosts (and subhosts).
59 If entry for @ is not specified predefined list of root servers is used.
61 example = lib.literalExpression ''
63 "@" = ["8.8.8.8" "8.8.4.4"];
64 "example.com" = ["192.168.100.100"];
69 forwardOnly = lib.mkOption {
71 type = lib.types.bool;
73 Whether to treat root servers (for @) as caching
74 servers, requesting addresses the same way a client does. This is
75 needed if you want to use e.g. Google DNS as your upstream DNS.
84 config = lib.mkIf config.services.dnscache.enable {
85 environment.systemPackages = [ pkgs.djbdns ];
86 users.users.dnscache = {
90 users.groups.dnscache = {};
92 systemd.services.dnscache = {
93 description = "djbdns dnscache server";
94 wantedBy = [ "multi-user.target" ];
95 path = with pkgs; [ bash daemontools djbdns ];
97 rm -rf /var/lib/dnscache
98 dnscache-conf dnscache dnscache /var/lib/dnscache ${config.services.dnscache.ip}
99 rm -rf /var/lib/dnscache/root
100 ln -sf ${dnscache-root} /var/lib/dnscache/root
103 cd /var/lib/dnscache/
104 ${lib.optionalString cfg.forwardOnly "export FORWARDONLY=1"}