at 15.09-beta 2.4 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cfg = config.services.dnsmasq; 7 dnsmasq = pkgs.dnsmasq; 8 9 dnsmasqConf = pkgs.writeText "dnsmasq.conf" '' 10 ${optionalString cfg.resolveLocalQueries '' 11 conf-file=/etc/dnsmasq-conf.conf 12 resolv-file=/etc/dnsmasq-resolv.conf 13 ''} 14 ${flip concatMapStrings cfg.servers (server: '' 15 server=${server} 16 '')} 17 ${cfg.extraConfig} 18 ''; 19 20in 21 22{ 23 24 ###### interface 25 26 options = { 27 28 services.dnsmasq = { 29 30 enable = mkOption { 31 type = types.bool; 32 default = false; 33 description = '' 34 Whether to run dnsmasq. 35 ''; 36 }; 37 38 resolveLocalQueries = mkOption { 39 type = types.bool; 40 default = true; 41 description = '' 42 Whether dnsmasq should resolve local queries (i.e. add 127.0.0.1 to 43 /etc/resolv.conf). 44 ''; 45 }; 46 47 servers = mkOption { 48 type = types.listOf types.str; 49 default = []; 50 example = [ "8.8.8.8" "8.8.4.4" ]; 51 description = '' 52 The DNS servers which dnsmasq should query. 53 ''; 54 }; 55 56 extraConfig = mkOption { 57 type = types.lines; 58 default = ""; 59 description = '' 60 Extra configuration directives that should be added to 61 <literal>dnsmasq.conf</literal>. 62 ''; 63 }; 64 65 }; 66 67 }; 68 69 70 ###### implementation 71 72 config = mkIf config.services.dnsmasq.enable { 73 74 networking.nameservers = 75 optional cfg.resolveLocalQueries "127.0.0.1"; 76 77 services.dbus.packages = [ dnsmasq ]; 78 79 users.extraUsers = singleton 80 { name = "dnsmasq"; 81 uid = config.ids.uids.dnsmasq; 82 description = "Dnsmasq daemon user"; 83 home = "/var/empty"; 84 }; 85 86 systemd.services.dnsmasq = { 87 description = "Dnsmasq Daemon"; 88 after = [ "network.target" "systemd-resolved.service" ]; 89 wantedBy = [ "multi-user.target" ]; 90 path = [ dnsmasq ]; 91 preStart = '' 92 touch /etc/dnsmasq-{conf,resolv}.conf 93 dnsmasq --test 94 ''; 95 serviceConfig = { 96 Type = "dbus"; 97 BusName = "uk.org.thekelleys.dnsmasq"; 98 ExecStart = "${dnsmasq}/bin/dnsmasq -k --enable-dbus --user=dnsmasq -C ${dnsmasqConf}"; 99 ExecReload = "${dnsmasq}/bin/kill -HUP $MAINPID"; 100 }; 101 restartTriggers = [ config.environment.etc.hosts.source ]; 102 }; 103 104 }; 105 106}