2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2009-03-06 12:27:13 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
cfg = config.services.bind;
|
|
|
|
|
2012-07-26 22:07:25 +00:00
|
|
|
bindUser = "named";
|
|
|
|
|
2021-04-29 15:47:43 +00:00
|
|
|
bindZoneCoerce = list: builtins.listToAttrs (lib.forEach list (zone: { name = zone.name; value = zone; }));
|
|
|
|
|
|
|
|
bindZoneOptions = { name, config, ... }: {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = name;
|
|
|
|
description = "Name of the zone.";
|
|
|
|
};
|
|
|
|
master = mkOption {
|
|
|
|
description = "Master=false means slave server";
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
file = mkOption {
|
|
|
|
type = types.either types.str types.path;
|
|
|
|
description = "Zone file resource records contain columns of data, separated by whitespace, that define the record.";
|
|
|
|
};
|
|
|
|
masters = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "List of servers for inclusion in stub and secondary zones.";
|
|
|
|
};
|
|
|
|
slaves = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "Addresses who may request zone transfers.";
|
2021-05-08 21:13:58 +00:00
|
|
|
default = [ ];
|
2021-04-29 15:47:43 +00:00
|
|
|
};
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Extra zone config to be appended at the end of the zone section.";
|
|
|
|
default = "";
|
|
|
|
};
|
2021-01-20 10:47:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
confFile = pkgs.writeText "named.conf"
|
|
|
|
''
|
2017-06-17 19:05:07 +00:00
|
|
|
include "/etc/bind/rndc.key";
|
|
|
|
controls {
|
|
|
|
inet 127.0.0.1 allow {localhost;} keys {"rndc-key";};
|
|
|
|
};
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
acl cachenetworks { ${concatMapStrings (entry: " ${entry}; ") cfg.cacheNetworks} };
|
|
|
|
acl badnetworks { ${concatMapStrings (entry: " ${entry}; ") cfg.blockedNetworks} };
|
|
|
|
|
|
|
|
options {
|
2017-06-10 10:19:07 +00:00
|
|
|
listen-on { ${concatMapStrings (entry: " ${entry}; ") cfg.listenOn} };
|
|
|
|
listen-on-v6 { ${concatMapStrings (entry: " ${entry}; ") cfg.listenOnIpv6} };
|
2009-10-12 16:36:19 +00:00
|
|
|
allow-query { cachenetworks; };
|
|
|
|
blackhole { badnetworks; };
|
|
|
|
forward first;
|
2013-08-26 19:36:01 +00:00
|
|
|
forwarders { ${concatMapStrings (entry: " ${entry}; ") cfg.forwarders} };
|
2018-12-19 21:39:28 +00:00
|
|
|
directory "/run/named";
|
|
|
|
pid-file "/run/named/named.pid";
|
2018-05-07 01:05:30 +00:00
|
|
|
${cfg.extraOptions}
|
2009-03-06 12:27:13 +00:00
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2015-09-29 09:50:39 +00:00
|
|
|
${cfg.extraConfig}
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
${ concatMapStrings
|
2019-07-20 15:36:05 +00:00
|
|
|
({ name, file, master ? true, slaves ? [], masters ? [], extraConfig ? "" }:
|
2011-09-14 18:20:50 +00:00
|
|
|
''
|
2009-10-12 16:36:19 +00:00
|
|
|
zone "${name}" {
|
|
|
|
type ${if master then "master" else "slave"};
|
|
|
|
file "${file}";
|
|
|
|
${ if master then
|
|
|
|
''
|
|
|
|
allow-transfer {
|
|
|
|
${concatMapStrings (ip: "${ip};\n") slaves}
|
|
|
|
};
|
|
|
|
''
|
|
|
|
else
|
|
|
|
''
|
|
|
|
masters {
|
|
|
|
${concatMapStrings (ip: "${ip};\n") masters}
|
|
|
|
};
|
|
|
|
''
|
|
|
|
}
|
|
|
|
allow-query { any; };
|
2019-07-20 15:36:05 +00:00
|
|
|
${extraConfig}
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
|
|
|
'')
|
2021-04-29 15:47:43 +00:00
|
|
|
(attrValues cfg.zones) }
|
2009-10-12 16:36:19 +00:00
|
|
|
'';
|
|
|
|
|
2009-03-06 12:27:13 +00:00
|
|
|
in
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
{
|
2009-03-06 12:27:13 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
services.bind = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2020-04-20 18:05:26 +00:00
|
|
|
enable = mkEnableOption "BIND domain name server";
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
cacheNetworks = mkOption {
|
2021-05-08 21:13:58 +00:00
|
|
|
default = [ "127.0.0.0/24" ];
|
2021-01-20 10:47:49 +00:00
|
|
|
type = types.listOf types.str;
|
2009-10-12 16:36:19 +00:00
|
|
|
description = "
|
2019-10-23 15:50:47 +00:00
|
|
|
What networks are allowed to use us as a resolver. Note
|
|
|
|
that this is for recursive queries -- all networks are
|
|
|
|
allowed to query zones configured with the `zones` option.
|
|
|
|
It is recommended that you limit cacheNetworks to avoid your
|
|
|
|
server being used for DNS amplification attacks.
|
2009-10-12 16:36:19 +00:00
|
|
|
";
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
blockedNetworks = mkOption {
|
2021-05-08 21:13:58 +00:00
|
|
|
default = [ ];
|
2021-01-20 10:47:49 +00:00
|
|
|
type = types.listOf types.str;
|
2009-10-12 16:36:19 +00:00
|
|
|
description = "
|
|
|
|
What networks are just blocked.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2012-06-10 14:50:44 +00:00
|
|
|
ipv4Only = mkOption {
|
|
|
|
default = false;
|
2021-01-20 10:47:49 +00:00
|
|
|
type = types.bool;
|
2012-06-10 14:50:44 +00:00
|
|
|
description = "
|
2013-08-26 19:36:01 +00:00
|
|
|
Only use ipv4, even if the host supports ipv6.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
forwarders = mkOption {
|
|
|
|
default = config.networking.nameservers;
|
2021-01-20 10:47:49 +00:00
|
|
|
type = types.listOf types.str;
|
2013-08-26 19:36:01 +00:00
|
|
|
description = "
|
|
|
|
List of servers we should forward requests to.
|
2012-06-10 14:50:44 +00:00
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2017-06-10 10:19:07 +00:00
|
|
|
listenOn = mkOption {
|
2021-05-08 21:13:58 +00:00
|
|
|
default = [ "any" ];
|
2017-06-10 10:19:07 +00:00
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "
|
|
|
|
Interfaces to listen on.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
listenOnIpv6 = mkOption {
|
2021-05-08 21:13:58 +00:00
|
|
|
default = [ "any" ];
|
2017-06-10 10:19:07 +00:00
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "
|
|
|
|
Ipv6 interfaces to listen on.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
zones = mkOption {
|
2021-05-08 21:13:58 +00:00
|
|
|
default = [ ];
|
2021-04-29 15:47:43 +00:00
|
|
|
type = with types; coercedTo (listOf attrs) bindZoneCoerce (attrsOf (types.submodule bindZoneOptions));
|
2009-10-12 16:36:19 +00:00
|
|
|
description = "
|
|
|
|
List of zones we claim authority over.
|
|
|
|
";
|
2021-04-29 15:47:43 +00:00
|
|
|
example = {
|
|
|
|
"example.com" = {
|
|
|
|
master = false;
|
|
|
|
file = "/var/dns/example.com";
|
2021-05-08 21:13:58 +00:00
|
|
|
masters = [ "192.168.0.1" ];
|
|
|
|
slaves = [ ];
|
2021-04-29 15:47:43 +00:00
|
|
|
extraConfig = "";
|
|
|
|
};
|
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2015-09-29 09:50:39 +00:00
|
|
|
extraConfig = mkOption {
|
2016-10-23 17:33:41 +00:00
|
|
|
type = types.lines;
|
2015-09-29 09:50:39 +00:00
|
|
|
default = "";
|
|
|
|
description = "
|
|
|
|
Extra lines to be added verbatim to the generated named configuration file.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2018-05-07 01:05:30 +00:00
|
|
|
extraOptions = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra lines to be added verbatim to the options section of the
|
|
|
|
generated named configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-11-03 19:13:52 +00:00
|
|
|
configFile = mkOption {
|
2016-01-17 18:34:55 +00:00
|
|
|
type = types.path;
|
2011-11-03 19:13:52 +00:00
|
|
|
default = confFile;
|
2016-01-17 18:34:55 +00:00
|
|
|
defaultText = "confFile";
|
2011-11-03 19:13:52 +00:00
|
|
|
description = "
|
|
|
|
Overridable config file to use for named. By default, that
|
|
|
|
generated by nixos.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2008-07-01 12:15:56 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2008-07-01 12:15:56 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
###### implementation
|
2008-07-01 12:15:56 +00:00
|
|
|
|
2019-07-15 17:18:49 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
networking.resolvconf.useLocalResolver = mkDefault true;
|
2009-03-06 12:27:13 +00:00
|
|
|
|
2019-09-14 17:51:29 +00:00
|
|
|
users.users.${bindUser} =
|
2021-05-08 21:13:58 +00:00
|
|
|
{
|
|
|
|
uid = config.ids.uids.bind;
|
2012-07-26 22:07:25 +00:00
|
|
|
description = "BIND daemon user";
|
|
|
|
};
|
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
systemd.services.bind = {
|
2016-09-08 20:19:43 +00:00
|
|
|
description = "BIND Domain Name Server";
|
2016-09-10 18:14:09 +00:00
|
|
|
after = [ "network.target" ];
|
2016-01-06 06:50:18 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2009-03-06 12:27:13 +00:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
preStart = ''
|
2017-06-17 19:05:07 +00:00
|
|
|
mkdir -m 0755 -p /etc/bind
|
|
|
|
if ! [ -f "/etc/bind/rndc.key" ]; then
|
2019-06-10 22:52:50 +00:00
|
|
|
${pkgs.bind.out}/sbin/rndc-confgen -c /etc/bind/rndc.key -u ${bindUser} -a -A hmac-sha256 2>/dev/null
|
2017-06-17 19:05:07 +00:00
|
|
|
fi
|
|
|
|
|
2018-12-19 21:39:28 +00:00
|
|
|
${pkgs.coreutils}/bin/mkdir -p /run/named
|
|
|
|
chown ${bindUser} /run/named
|
2016-01-06 06:50:18 +00:00
|
|
|
'';
|
2008-07-01 12:15:56 +00:00
|
|
|
|
2017-06-17 19:05:07 +00:00
|
|
|
serviceConfig = {
|
2021-05-08 21:13:58 +00:00
|
|
|
ExecStart = "${pkgs.bind.out}/sbin/named -u ${bindUser} ${optionalString cfg.ipv4Only "-4"} -c ${cfg.configFile} -f";
|
2017-06-17 19:05:07 +00:00
|
|
|
ExecReload = "${pkgs.bind.out}/sbin/rndc -k '/etc/bind/rndc.key' reload";
|
2021-05-08 21:13:58 +00:00
|
|
|
ExecStop = "${pkgs.bind.out}/sbin/rndc -k '/etc/bind/rndc.key' stop";
|
2017-06-17 19:05:07 +00:00
|
|
|
};
|
|
|
|
|
2016-09-08 20:19:43 +00:00
|
|
|
unitConfig.Documentation = "man:named(8)";
|
2016-01-06 06:50:18 +00:00
|
|
|
};
|
2009-03-06 12:27:13 +00:00
|
|
|
};
|
|
|
|
}
|