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