1 { config, lib, pkgs, ... }:
6 cfg = config.services.dnsmasq;
7 dnsmasq = pkgs.dnsmasq;
8 stateDir = "/var/lib/dnsmasq";
10 dnsmasqConf = pkgs.writeText "dnsmasq.conf" ''
11 dhcp-leasefile=${stateDir}/dnsmasq.leases
12 ${optionalString cfg.resolveLocalQueries ''
13 conf-file=/etc/dnsmasq-conf.conf
14 resolv-file=/etc/dnsmasq-resolv.conf
16 ${flip concatMapStrings cfg.servers (server: ''
35 description = lib.mdDoc ''
36 Whether to run dnsmasq.
40 resolveLocalQueries = mkOption {
43 description = lib.mdDoc ''
44 Whether dnsmasq should resolve local queries (i.e. add 127.0.0.1 to
50 type = types.listOf types.str;
52 example = [ "8.8.8.8" "8.8.4.4" ];
53 description = lib.mdDoc ''
54 The DNS servers which dnsmasq should query.
58 alwaysKeepRunning = mkOption {
61 description = lib.mdDoc ''
62 If enabled, systemd will always respawn dnsmasq even if shut down manually. The default, disabled, will only restart it on error.
66 extraConfig = mkOption {
69 description = lib.mdDoc ''
70 Extra configuration directives that should be added to
82 config = mkIf cfg.enable {
84 networking.nameservers =
85 optional cfg.resolveLocalQueries "127.0.0.1";
87 services.dbus.packages = [ dnsmasq ];
89 users.users.dnsmasq = {
92 description = "Dnsmasq daemon user";
94 users.groups.dnsmasq = {};
96 networking.resolvconf = mkIf cfg.resolveLocalQueries {
97 useLocalResolver = mkDefault true;
100 dnsmasq_conf=/etc/dnsmasq-conf.conf
101 dnsmasq_resolv=/etc/dnsmasq-resolv.conf
105 systemd.services.dnsmasq = {
106 description = "Dnsmasq Daemon";
107 after = [ "network.target" "systemd-resolved.service" ];
108 wantedBy = [ "multi-user.target" ];
111 mkdir -m 755 -p ${stateDir}
112 touch ${stateDir}/dnsmasq.leases
113 chown -R dnsmasq ${stateDir}
114 touch /etc/dnsmasq-{conf,resolv}.conf
119 BusName = "uk.org.thekelleys.dnsmasq";
120 ExecStart = "${dnsmasq}/bin/dnsmasq -k --enable-dbus --user=dnsmasq -C ${dnsmasqConf}";
121 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
123 ProtectSystem = true;
125 Restart = if cfg.alwaysKeepRunning then "always" else "on-failure";
127 restartTriggers = [ config.environment.etc.hosts.source ];