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;
|
2023-10-05 19:46:12 +02:00
|
|
|
dnsmasq = cfg.package;
|
2015-12-24 22:36:19 +01:00
|
|
|
stateDir = "/var/lib/dnsmasq";
|
2011-04-13 16:09:02 +02:00
|
|
|
|
2022-10-14 20:08:38 +02:00
|
|
|
# True values are just put as `name` instead of `name=true`, and false values
|
|
|
|
# are turned to comments (false values are expected to be overrides e.g.
|
|
|
|
# mkForce)
|
|
|
|
formatKeyValue =
|
|
|
|
name: value:
|
|
|
|
if value == true
|
|
|
|
then name
|
|
|
|
else if value == false
|
|
|
|
then "# setting `${name}` explicitly set to false"
|
|
|
|
else generators.mkKeyValueDefault { } "=" name value;
|
|
|
|
|
|
|
|
settingsFormat = pkgs.formats.keyValue {
|
|
|
|
mkKeyValue = formatKeyValue;
|
|
|
|
listsAsDuplicateKeys = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Because formats.generate is outputting a file, we use of conf-file. Once
|
|
|
|
# `extraConfig` is deprecated we can just use
|
|
|
|
# `dnsmasqConf = format.generate "dnsmasq.conf" cfg.settings`
|
2012-11-12 17:58:48 +01:00
|
|
|
dnsmasqConf = pkgs.writeText "dnsmasq.conf" ''
|
2022-10-14 20:08:38 +02:00
|
|
|
conf-file=${settingsFormat.generate "dnsmasq.conf" cfg.settings}
|
2014-09-03 00:36:25 +02:00
|
|
|
${cfg.extraConfig}
|
2012-11-12 17:58:48 +01:00
|
|
|
'';
|
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2022-10-14 20:08:38 +02:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "dnsmasq" "servers" ] [ "services" "dnsmasq" "settings" "server" ])
|
|
|
|
];
|
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
###### 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;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2011-04-13 16:09:02 +02:00
|
|
|
Whether to run dnsmasq.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-11-30 19:03:14 +01:00
|
|
|
package = mkPackageOption pkgs "dnsmasq" {};
|
2023-10-05 19:46:12 +02:00
|
|
|
|
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;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2014-07-03 00:59:35 +02:00
|
|
|
Whether dnsmasq should resolve local queries (i.e. add 127.0.0.1 to
|
2017-09-23 16:36:05 +02:00
|
|
|
/etc/resolv.conf).
|
2014-07-03 00:59:35 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-10-11 17:13:37 +02:00
|
|
|
alwaysKeepRunning = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2017-10-11 17:13:37 +02:00
|
|
|
If enabled, systemd will always respawn dnsmasq even if shut down manually. The default, disabled, will only restart it on error.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-10-14 20:08:38 +02:00
|
|
|
settings = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
|
|
|
options.server = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "8.8.8.8" "8.8.4.4" ];
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2022-10-14 20:08:38 +02:00
|
|
|
The DNS servers which dnsmasq should query.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
default = { };
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2022-10-14 20:08:38 +02:00
|
|
|
Configuration of dnsmasq. Lists get added one value per line (empty
|
|
|
|
lists and false values don't get added, though false values get
|
|
|
|
turned to comments). Gets merged with
|
|
|
|
|
|
|
|
{
|
|
|
|
dhcp-leasefile = "${stateDir}/dnsmasq.leases";
|
|
|
|
conf-file = optional cfg.resolveLocalQueries "/etc/dnsmasq-conf.conf";
|
|
|
|
resolv-file = optional cfg.resolveLocalQueries "/etc/dnsmasq-resolv.conf";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
domain-needed = true;
|
|
|
|
dhcp-range = [ "192.168.0.2,192.168.0.254" ];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
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 = "";
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2012-11-12 17:58:48 +01:00
|
|
|
Extra configuration directives that should be added to
|
2015-04-16 19:12:12 +02:00
|
|
|
`dnsmasq.conf`.
|
2022-10-14 20:08:38 +02:00
|
|
|
|
|
|
|
This option is deprecated, please use {option}`settings` instead.
|
2012-11-12 17:58:48 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-04-13 16:09:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2019-07-15 19:18:49 +02:00
|
|
|
config = mkIf cfg.enable {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2022-10-14 20:08:38 +02:00
|
|
|
warnings = lib.optional (cfg.extraConfig != "") "Text based config is deprecated, dnsmasq now supports `services.dnsmasq.settings` for an attribute-set based config";
|
|
|
|
|
|
|
|
services.dnsmasq.settings = {
|
|
|
|
dhcp-leasefile = mkDefault "${stateDir}/dnsmasq.leases";
|
|
|
|
conf-file = mkDefault (optional cfg.resolveLocalQueries "/etc/dnsmasq-conf.conf");
|
|
|
|
resolv-file = mkDefault (optional cfg.resolveLocalQueries "/etc/dnsmasq-resolv.conf");
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2019-09-14 19:51:29 +02:00
|
|
|
users.users.dnsmasq = {
|
2021-08-08 14:00:00 +02:00
|
|
|
isSystemUser = true;
|
|
|
|
group = "dnsmasq";
|
2015-12-24 22:36:19 +01:00
|
|
|
description = "Dnsmasq daemon user";
|
|
|
|
};
|
2021-08-08 14:00:00 +02:00
|
|
|
users.groups.dnsmasq = {};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2019-07-15 19:18:49 +02:00
|
|
|
networking.resolvconf = mkIf cfg.resolveLocalQueries {
|
|
|
|
useLocalResolver = mkDefault true;
|
|
|
|
|
|
|
|
extraConfig = ''
|
|
|
|
dnsmasq_conf=/etc/dnsmasq-conf.conf
|
|
|
|
dnsmasq_resolv=/etc/dnsmasq-resolv.conf
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
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";
|
2017-10-11 17:13:37 +02:00
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectSystem = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
Restart = if cfg.alwaysKeepRunning then "always" else "on-failure";
|
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
|
|
|
};
|
2023-05-13 22:54:48 +02:00
|
|
|
|
|
|
|
meta.doc = ./dnsmasq.md;
|
2011-04-13 16:09:02 +02:00
|
|
|
}
|