8 cfg = config.services.dnscache;
10 dnscache-root = pkgs.runCommand "dnscache-root" { preferLocalBuild = true; } ''
11 mkdir -p $out/{servers,ip}
13 ${lib.concatMapStrings (ip: ''
14 touch "$out/ip/"${lib.escapeShellArg ip}
18 lib.mapAttrsToList (host: ips: ''
19 ${lib.concatMapStrings (ip: ''
20 echo ${lib.escapeShellArg ip} >> "$out/servers/"${lib.escapeShellArg host}
25 # if a list of root servers was not provided in config, copy it
26 # over. (this is also done by dnscache-conf, but we 'rm -rf
27 # /var/lib/dnscache/root' below & replace it wholesale with this,
28 # so we have to ensure servers/@ exists ourselves.)
29 if [ ! -e $out/servers/@ ]; then
30 # symlink does not work here, due chroot
31 cp ${pkgs.djbdns}/etc/dnsroots.global $out/servers/@;
43 enable = lib.mkOption {
45 type = lib.types.bool;
46 description = "Whether to run the dnscache caching dns server.";
52 description = "IP address on which to listen for connections.";
55 clientIps = lib.mkOption {
56 default = [ "127.0.0.1" ];
57 type = lib.types.listOf lib.types.str;
58 description = "Client IP addresses (or prefixes) from which to accept connections.";
65 domainServers = lib.mkOption {
67 type = lib.types.attrsOf (lib.types.listOf lib.types.str);
69 Table of {hostname: server} pairs to use as authoritative servers for hosts (and subhosts).
70 If entry for @ is not specified predefined list of root servers is used.
72 example = lib.literalExpression ''
74 "@" = ["8.8.8.8" "8.8.4.4"];
75 "example.com" = ["192.168.100.100"];
80 forwardOnly = lib.mkOption {
82 type = lib.types.bool;
84 Whether to treat root servers (for @) as caching
85 servers, requesting addresses the same way a client does. This is
86 needed if you want to use e.g. Google DNS as your upstream DNS.
95 config = lib.mkIf config.services.dnscache.enable {
96 environment.systemPackages = [ pkgs.djbdns ];
97 users.users.dnscache = {
101 users.groups.dnscache = { };
103 systemd.services.dnscache = {
104 description = "djbdns dnscache server";
105 wantedBy = [ "multi-user.target" ];
112 rm -rf /var/lib/dnscache
113 dnscache-conf dnscache dnscache /var/lib/dnscache ${config.services.dnscache.ip}
114 rm -rf /var/lib/dnscache/root
115 ln -sf ${dnscache-root} /var/lib/dnscache/root
118 cd /var/lib/dnscache/
119 ${lib.optionalString cfg.forwardOnly "export FORWARDONLY=1"}