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