2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-04-13 16:09:02 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2011-04-13 16:09:02 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.dnsmasq;
|
|
|
|
dnsmasq = pkgs.dnsmasq;
|
2015-12-24 22:36:19 +01:00
|
|
|
stateDir = "/var/lib/dnsmasq";
|
2011-04-13 16:09:02 +02:00
|
|
|
|
2012-11-12 17:58:48 +01:00
|
|
|
dnsmasqConf = pkgs.writeText "dnsmasq.conf" ''
|
2015-12-24 22:36:19 +01:00
|
|
|
dhcp-leasefile=${stateDir}/dnsmasq.leases
|
2014-07-03 00:59:35 +02:00
|
|
|
${optionalString cfg.resolveLocalQueries ''
|
|
|
|
conf-file=/etc/dnsmasq-conf.conf
|
|
|
|
resolv-file=/etc/dnsmasq-resolv.conf
|
|
|
|
''}
|
2014-09-03 00:36:25 +02:00
|
|
|
${flip concatMapStrings cfg.servers (server: ''
|
|
|
|
server=${server}
|
|
|
|
'')}
|
|
|
|
${cfg.extraConfig}
|
2012-11-12 17:58:48 +01:00
|
|
|
'';
|
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
services.dnsmasq = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2015-04-16 19:12:12 +02:00
|
|
|
type = types.bool;
|
2011-04-19 15:37:31 +02:00
|
|
|
default = false;
|
2011-04-13 16:09:02 +02:00
|
|
|
description = ''
|
|
|
|
Whether to run dnsmasq.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-07-03 00:59:35 +02:00
|
|
|
resolveLocalQueries = mkOption {
|
2015-04-16 19:12:12 +02:00
|
|
|
type = types.bool;
|
2014-07-03 00:59:35 +02:00
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether dnsmasq should resolve local queries (i.e. add 127.0.0.1 to
|
2015-04-16 19:12:12 +02:00
|
|
|
/etc/resolv.conf).
|
2014-07-03 00:59:35 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
servers = mkOption {
|
2015-06-15 18:18:46 +02:00
|
|
|
type = types.listOf types.str;
|
2011-04-13 16:09:02 +02:00
|
|
|
default = [];
|
|
|
|
example = [ "8.8.8.8" "8.8.4.4" ];
|
|
|
|
description = ''
|
2014-09-03 18:03:03 +02:00
|
|
|
The DNS servers which dnsmasq should query.
|
2011-04-13 16:09:02 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-11-12 17:58:48 +01:00
|
|
|
extraConfig = mkOption {
|
2015-04-16 19:12:12 +02:00
|
|
|
type = types.lines;
|
2012-11-12 17:58:48 +01:00
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra configuration directives that should be added to
|
2015-04-16 19:12:12 +02:00
|
|
|
<literal>dnsmasq.conf</literal>.
|
2012-11-12 17:58:48 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.services.dnsmasq.enable {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2014-09-03 00:36:25 +02:00
|
|
|
networking.nameservers =
|
|
|
|
optional cfg.resolveLocalQueries "127.0.0.1";
|
2011-04-13 16:09:02 +02:00
|
|
|
|
2014-07-03 00:59:35 +02:00
|
|
|
services.dbus.packages = [ dnsmasq ];
|
2011-04-13 16:09:02 +02:00
|
|
|
|
2015-12-24 22:36:19 +01:00
|
|
|
users.extraUsers = singleton {
|
|
|
|
name = "dnsmasq";
|
|
|
|
uid = config.ids.uids.dnsmasq;
|
|
|
|
description = "Dnsmasq daemon user";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2014-07-03 00:59:35 +02:00
|
|
|
systemd.services.dnsmasq = {
|
2015-04-16 19:12:12 +02:00
|
|
|
description = "Dnsmasq Daemon";
|
2015-02-25 18:22:16 +01:00
|
|
|
after = [ "network.target" "systemd-resolved.service" ];
|
2014-07-03 00:59:35 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2014-09-03 00:36:25 +02:00
|
|
|
path = [ dnsmasq ];
|
|
|
|
preStart = ''
|
2015-12-24 22:36:19 +01:00
|
|
|
mkdir -m 755 -p ${stateDir}
|
|
|
|
touch ${stateDir}/dnsmasq.leases
|
|
|
|
chown -R dnsmasq ${stateDir}
|
2014-09-03 00:36:25 +02:00
|
|
|
touch /etc/dnsmasq-{conf,resolv}.conf
|
|
|
|
dnsmasq --test
|
|
|
|
'';
|
2014-07-03 00:59:35 +02:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "dbus";
|
|
|
|
BusName = "uk.org.thekelleys.dnsmasq";
|
|
|
|
ExecStart = "${dnsmasq}/bin/dnsmasq -k --enable-dbus --user=dnsmasq -C ${dnsmasqConf}";
|
2015-09-08 19:23:17 +02:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2014-07-03 00:59:35 +02:00
|
|
|
};
|
2015-08-05 12:35:24 +02:00
|
|
|
restartTriggers = [ config.environment.etc.hosts.source ];
|
2014-07-03 00:59:35 +02:00
|
|
|
};
|
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
}
|