git.iko.soy/typeable/nixpkgs
on master help sign in
resolvconf service: init

This is a refactor of how resolvconf is managed on NixOS. We split it into a separate service which is enabled internally depending on whether we want /etc/resolv.conf to be managed by it. Various services now take advantage of those configuration options. We also now use systemd instead of activation scripts to update resolv.conf. NetworkManager now uses the right option for rc-manager DNS automatically, so the configuration option shouldn't be exposed.

commit01b90dce78ee3906def0fc8d800217a3f9f40aa7 authorNikolay Amiantov <ab@fmap.me> dateMon 15 Jul 2019, 20:18 (7 years ago) parent267c8d6
13 files changed+216−145
nixos/modules/config/networking.nix−104 nixos/modules/config/resolvconf.nix+149 nixos/modules/module-list.nix+1 nixos/modules/rename.nix+6 nixos/modules/services/networking/bind.nix+3−1 nixos/modules/services/networking/dnsmasq.nix+10−1 nixos/modules/services/networking/networkmanager.nix+3−21 nixos/modules/services/networking/rdnssd.nix+5 nixos/modules/services/networking/unbound.nix+2 nixos/modules/system/boot/resolved.nix+31−12 nixos/modules/system/boot/stage-2.nix+5−4 nixos/modules/tasks/network-interfaces-scripted.nix+1−1 nixos/modules/tasks/network-interfaces.nix−1
nixos/modules/config/networking.nix−104
@@ -7,16 +7,6 @@ with lib;
77let
88
99 cfg = config.networking;
10− dnsmasqResolve = config.services.dnsmasq.enable &&
11− config.services.dnsmasq.resolveLocalQueries;
12− hasLocalResolver = config.services.bind.enable ||
13− config.services.unbound.enable ||
14− dnsmasqResolve;
15−
16− resolvconfOptions = cfg.resolvconfOptions
17− ++ optional cfg.dnsSingleRequest "single-request"
18− ++ optional cfg.dnsExtensionMechanism "edns0";
19−
2010
2111 localhostMapped4 = cfg.hosts ? "127.0.0.1" && elem "localhost" cfg.hosts."127.0.0.1";
2212 localhostMapped6 = cfg.hosts ? "::1" && elem "localhost" cfg.hosts."::1";
@@ -64,48 +54,6 @@ in
6454 '';
6555 };
6656
67− networking.dnsSingleRequest = lib.mkOption {
68− type = types.bool;
69− default = false;
70− description = ''
71− Recent versions of glibc will issue both ipv4 (A) and ipv6 (AAAA)
72− address queries at the same time, from the same port. Sometimes upstream
73− routers will systemically drop the ipv4 queries. The symptom of this problem is
74− that 'getent hosts example.com' only returns ipv6 (or perhaps only ipv4) addresses. The
75− workaround for this is to specify the option 'single-request' in
76− /etc/resolv.conf. This option enables that.
77− '';
78− };
79−
80− networking.dnsExtensionMechanism = lib.mkOption {
81− type = types.bool;
82− default = true;
83− description = ''
84− Enable the <code>edns0</code> option in <filename>resolv.conf</filename>. With
85− that option set, <code>glibc</code> supports use of the extension mechanisms for
86− DNS (EDNS) specified in RFC 2671. The most popular user of that feature is DNSSEC,
87− which does not work without it.
88− '';
89− };
90−
91− networking.extraResolvconfConf = lib.mkOption {
92− type = types.lines;
93− default = "";
94− example = "libc=NO";
95− description = ''
96− Extra configuration to append to <filename>resolvconf.conf</filename>.
97− '';
98− };
99−
100− networking.resolvconfOptions = lib.mkOption {
101− type = types.listOf types.str;
102− default = [];
103− example = [ "ndots:1" "rotate" ];
104− description = ''
105− Set the options in <filename>/etc/resolv.conf</filename>.
106− '';
107− };
108−
10957 networking.timeServers = mkOption {
11058 default = [
11159 "0.nixos.pool.ntp.org"
@@ -240,35 +188,6 @@ in
240188 # /etc/host.conf: resolver configuration file
241189 "host.conf".text = cfg.hostConf;
242190
243− # /etc/resolvconf.conf: Configuration for openresolv.
244− "resolvconf.conf".text =
245− ''
246− # This is the default, but we must set it here to prevent
247− # a collision with an apparently unrelated environment
248− # variable with the same name exported by dhcpcd.
249− interface_order='lo lo[0-9]*'
250− '' + optionalString config.services.nscd.enable ''
251− # Invalidate the nscd cache whenever resolv.conf is
252− # regenerated.
253− libc_restart='${pkgs.systemd}/bin/systemctl try-restart --no-block nscd.service 2> /dev/null'
254− '' + optionalString (length resolvconfOptions > 0) ''
255− # Options as described in resolv.conf(5)
256− resolv_conf_options='${concatStringsSep " " resolvconfOptions}'
257− '' + optionalString hasLocalResolver ''
258− # This hosts runs a full-blown DNS resolver.
259− name_servers='127.0.0.1'
260− '' + optionalString dnsmasqResolve ''
261− dnsmasq_conf=/etc/dnsmasq-conf.conf
262− dnsmasq_resolv=/etc/dnsmasq-resolv.conf
263− '' + cfg.extraResolvconfConf + ''
264− '';
265−
266− } // optionalAttrs config.services.resolved.enable {
267− # symlink the dynamic stub resolver of resolv.conf as recommended by upstream:
268− # https://www.freedesktop.org/software/systemd/man/systemd-resolved.html#/etc/resolv.conf
269− "resolv.conf".source = "/run/systemd/resolve/stub-resolv.conf";
270− } // optionalAttrs (config.services.resolved.enable && dnsmasqResolve) {
271− "dnsmasq-resolv.conf".source = "/run/systemd/resolve/resolv.conf";
272191 } // optionalAttrs (pkgs.stdenv.hostPlatform.libc == "glibc") {
273192 # /etc/rpc: RPC program numbers.
274193 "rpc".source = pkgs.glibc.out + "/etc/rpc";
@@ -295,29 +214,6 @@ in
295214 # Install the proxy environment variables
296215 environment.sessionVariables = cfg.proxy.envVars;
297216
298− # This is needed when /etc/resolv.conf is being overriden by networkd
299− # and other configurations. If the file is destroyed by an environment
300− # activation then it must be rebuilt so that applications which interface
301− # with /etc/resolv.conf directly don't break.
302− system.activationScripts.resolvconf = stringAfter [ "etc" "specialfs" "var" ]
303− ''
304− # Systemd resolved controls its own resolv.conf
305− rm -f /run/resolvconf/interfaces/systemd
306− ${optionalString config.services.resolved.enable ''
307− rm -rf /run/resolvconf/interfaces
308− mkdir -p /run/resolvconf/interfaces
309− ln -s /run/systemd/resolve/resolv.conf /run/resolvconf/interfaces/systemd
310− ''}
311−
312− # Make sure resolv.conf is up to date if not managed manually, by systemd or
313− # by NetworkManager
314− ${optionalString (!config.environment.etc?"resolv.conf" &&
315− (cfg.networkmanager.enable ->
316− cfg.networkmanager.rc-manager == "resolvconf")) ''
317− ${pkgs.openresolv}/bin/resolvconf -u
318− ''}
319− '';
320−
321217 };
322218
323219}
nixos/modules/config/resolvconf.nixnew file+149
@@ -0,0 +1,149 @@
1+# /etc files related to networking, such as /etc/services.
2+
3+{ config, lib, pkgs, ... }:
4+
5+with lib;
6+
7+let
8+
9+ cfg = config.networking.resolvconf;
10+
11+ resolvconfOptions = cfg.extraOptions
12+ ++ optional cfg.dnsSingleRequest "single-request"
13+ ++ optional cfg.dnsExtensionMechanism "edns0";
14+
15+ configText =
16+ ''
17+ # This is the default, but we must set it here to prevent
18+ # a collision with an apparently unrelated environment
19+ # variable with the same name exported by dhcpcd.
20+ interface_order='lo lo[0-9]*'
21+ '' + optionalString config.services.nscd.enable ''
22+ # Invalidate the nscd cache whenever resolv.conf is
23+ # regenerated.
24+ libc_restart='${pkgs.systemd}/bin/systemctl try-restart --no-block nscd.service 2> /dev/null'
25+ '' + optionalString (length resolvconfOptions > 0) ''
26+ # Options as described in resolv.conf(5)
27+ resolv_conf_options='${concatStringsSep " " resolvconfOptions}'
28+ '' + optionalString cfg.useLocalResolver ''
29+ # This hosts runs a full-blown DNS resolver.
30+ name_servers='127.0.0.1'
31+ '' + cfg.extraConfig;
32+
33+in
34+
35+{
36+
37+ options = {
38+
39+ networking.resolvconf = {
40+
41+ enable = mkOption {
42+ type = types.bool;
43+ default = false;
44+ internal = true;
45+ description = ''
46+ DNS configuration is managed by resolvconf.
47+ '';
48+ };
49+
50+ useHostResolvConf = mkOption {
51+ type = types.bool;
52+ default = false;
53+ description = ''
54+ In containers, whether to use the
55+ <filename>resolv.conf</filename> supplied by the host.
56+ '';
57+ };
58+
59+ dnsSingleRequest = lib.mkOption {
60+ type = types.bool;
61+ default = false;
62+ description = ''
63+ Recent versions of glibc will issue both ipv4 (A) and ipv6 (AAAA)
64+ address queries at the same time, from the same port. Sometimes upstream
65+ routers will systemically drop the ipv4 queries. The symptom of this problem is
66+ that 'getent hosts example.com' only returns ipv6 (or perhaps only ipv4) addresses. The
67+ workaround for this is to specify the option 'single-request' in
68+ /etc/resolv.conf. This option enables that.
69+ '';
70+ };
71+
72+ dnsExtensionMechanism = mkOption {
73+ type = types.bool;
74+ default = true;
75+ description = ''
76+ Enable the <code>edns0</code> option in <filename>resolv.conf</filename>. With
77+ that option set, <code>glibc</code> supports use of the extension mechanisms for
78+ DNS (EDNS) specified in RFC 2671. The most popular user of that feature is DNSSEC,
79+ which does not work without it.
80+ '';
81+ };
82+
83+ extraConfig = mkOption {
84+ type = types.lines;
85+ default = "";
86+ example = "libc=NO";
87+ description = ''
88+ Extra configuration to append to <filename>resolvconf.conf</filename>.
89+ '';
90+ };
91+
92+ extraOptions = mkOption {
93+ type = types.listOf types.str;
94+ default = [];
95+ example = [ "ndots:1" "rotate" ];
96+ description = ''
97+ Set the options in <filename>/etc/resolv.conf</filename>.
98+ '';
99+ };
100+
101+ useLocalResolver = mkOption {
102+ type = types.bool;
103+ default = false;
104+ description = ''
105+ Use local DNS server for resolving.
106+ '';
107+ };
108+
109+ };
110+
111+ };
112+
113+ config = mkMerge [
114+ {
115+ networking.resolvconf.enable = !(config.environment.etc ? "resolv.conf");
116+
117+ environment.etc."resolvconf.conf".text =
118+ if !cfg.enable then
119+ # Force-stop any attempts to use resolvconf
120+ ''
121+ echo "resolvconf is disabled on this system but was used anyway:" >&2
122+ echo "$0 $*" >&2
123+ exit 1
124+ ''
125+ else configText;
126+ }
127+
128+ (mkIf cfg.enable {
129+ environment.systemPackages = [ pkgs.openresolv ];
130+
131+ systemd.services.resolvconf = {
132+ description = "resolvconf update";
133+
134+ before = [ "network-pre.target" ];
135+ wants = [ "network-pre.target" ];
136+ wantedBy = [ "multi-user.target" ];
137+ restartTriggers = [ config.environment.etc."resolvconf.conf".source ];
138+
139+ serviceConfig = {
140+ Type = "oneshot";
141+ ExecStart = "${pkgs.openresolv}/bin/resolvconf -u";
142+ RemainAfterExit = true;
143+ };
144+ };
145+
146+ })
147+ ];
148+
149+}
nixos/modules/module-list.nix+1
@@ -25,6 +25,7 @@
2525 ./config/nsswitch.nix
2626 ./config/power-management.nix
2727 ./config/pulseaudio.nix
28+ ./config/resolvconf.nix
2829 ./config/shells-environment.nix
2930 ./config/swap.nix
3031 ./config/sysctl.nix
nixos/modules/rename.nix+6
@@ -244,6 +244,12 @@ with lib;
244244 # KSM
245245 (mkRenamedOptionModule [ "hardware" "enableKSM" ] [ "hardware" "ksm" "enable" ])
246246
247+ # resolvconf
248+ (mkRenamedOptionModule [ "networking" "dnsSingleRequest" ] [ "networking" "resolvconf" "dnsSingleRequest" ])
249+ (mkRenamedOptionModule [ "networking" "dnsExtensionMechanism" ] [ "networking" "resolvconf" "dnsExtensionMechanism" ])
250+ (mkRenamedOptionModule [ "networking" "extraResolvconfConf" ] [ "networking" "resolvconf" "extraConfig" ])
251+ (mkRenamedOptionModule [ "networking" "resolvconfOptions" ] [ "networking" "resolvconf" "extraOptions" ])
252+
247253 ] ++ (flip map [ "blackboxExporter" "collectdExporter" "fritzboxExporter"
248254 "jsonExporter" "minioExporter" "nginxExporter" "nodeExporter"
249255 "snmpExporter" "unifiExporter" "varnishExporter" ]
nixos/modules/services/networking/bind.nix+3−1
@@ -168,7 +168,9 @@ in
168168
169169 ###### implementation
170170
171− config = mkIf config.services.bind.enable {
171+ config = mkIf cfg.enable {
172+
173+ networking.resolvconf.useLocalResolver = mkDefault true;
172174
173175 users.users = singleton
174176 { name = bindUser;
nixos/modules/services/networking/dnsmasq.nix+10−1
@@ -79,7 +79,7 @@ in
7979
8080 ###### implementation
8181
82− config = mkIf config.services.dnsmasq.enable {
82+ config = mkIf cfg.enable {
8383
8484 networking.nameservers =
8585 optional cfg.resolveLocalQueries "127.0.0.1";
@@ -92,6 +92,15 @@ in
9292 description = "Dnsmasq daemon user";
9393 };
9494
95+ networking.resolvconf = mkIf cfg.resolveLocalQueries {
96+ useLocalResolver = mkDefault true;
97+
98+ extraConfig = ''
99+ dnsmasq_conf=/etc/dnsmasq-conf.conf
100+ dnsmasq_resolv=/etc/dnsmasq-resolv.conf
101+ '';
102+ };
103+
95104 systemd.services.dnsmasq = {
96105 description = "Dnsmasq Daemon";
97106 after = [ "network.target" "systemd-resolved.service" ];
nixos/modules/services/networking/networkmanager.nix+3−21
@@ -17,7 +17,8 @@ let
1717 plugins=keyfile
1818 dhcp=${cfg.dhcp}
1919 dns=${cfg.dns}
20− rc-manager=${cfg.rc-manager}
20+ # If resolvconf is disabled that means that resolv.conf is managed by some other module.
21+ rc-manager=${if config.networking.resolvconf.enable then "resolvconf" else "unmanaged"}
2122
2223 [keyfile]
2324 ${optionalString (cfg.unmanaged != [])
@@ -268,25 +269,6 @@ in {
268269 '';
269270 };
270271
271− rc-manager = mkOption {
272− type = types.enum [ "symlink" "file" "resolvconf" "netconfig" "unmanaged" "none" ];
273− default = "resolvconf";
274− description = ''
275− Set the <literal>resolv.conf</literal> management mode.
276− </para>
277− <para>
278− A description of these modes can be found in the main section of
279− <link xlink:href="https://developer.gnome.org/NetworkManager/stable/NetworkManager.conf.html">
280− https://developer.gnome.org/NetworkManager/stable/NetworkManager.conf.html
281− </link>
282− or in
283− <citerefentry>
284− <refentrytitle>NetworkManager.conf</refentrytitle>
285− <manvolnum>5</manvolnum>
286− </citerefentry>.
287− '';
288− };
289−
290272 dispatcherScripts = mkOption {
291273 type = types.listOf (types.submodule {
292274 options = {
@@ -512,7 +494,7 @@ in {
512494 networking = {
513495 useDHCP = false;
514496 # use mkDefault to trigger the assertion about the conflict above
515− wireless.enable = lib.mkDefault false;
497+ wireless.enable = mkDefault false;
516498 };
517499
518500 security.polkit.extraConfig = polkitConf;
nixos/modules/services/networking/rdnssd.nix+5
@@ -35,6 +35,11 @@ in
3535
3636 config = mkIf config.services.rdnssd.enable {
3737
38+ assertions = [{
39+ assertion = config.networking.resolvconf.enable;
40+ message = "rdnssd needs resolvconf to work (probably something sets up a static resolv.conf)";
41+ }];
42+
3843 systemd.services.rdnssd = {
3944 description = "RDNSS daemon";
4045 after = [ "network.target" ];
nixos/modules/services/networking/unbound.nix+2
@@ -101,6 +101,8 @@ in
101101 isSystemUser = true;
102102 };
103103
104+ networking.resolvconf.useLocalResolver = mkDefault true;
105+
104106 systemd.services.unbound = {
105107 description = "Unbound recursive Domain Name Server";
106108 after = [ "network.target" ];
nixos/modules/system/boot/resolved.nix+31−12
@@ -3,6 +3,10 @@
33with lib;
44let
55 cfg = config.services.resolved;
6+
7+ dnsmasqResolve = config.services.dnsmasq.enable &&
8+ config.services.dnsmasq.resolveLocalQueries;
9+
610in
711{
812
@@ -126,6 +130,12 @@ in
126130
127131 config = mkIf cfg.enable {
128132
133+ assertions = [
134+ { assertion = !config.networking.useHostResolvConf;
135+ message = "Using host resolv.conf is not supported with systemd-resolved";
136+ }
137+ ];
138+
129139 systemd.additionalUpstreamSystemUnits = [
130140 "systemd-resolved.service"
131141 ];
@@ -135,21 +145,30 @@ in
135145 restartTriggers = [ config.environment.etc."systemd/resolved.conf".source ];
136146 };
137147
138− environment.etc."systemd/resolved.conf".text = ''
139− [Resolve]
140− ${optionalString (config.networking.nameservers != [])
141− "DNS=${concatStringsSep " " config.networking.nameservers}"}
142− ${optionalString (cfg.fallbackDns != [])
143− "FallbackDNS=${concatStringsSep " " cfg.fallbackDns}"}
144− ${optionalString (cfg.domains != [])
145− "Domains=${concatStringsSep " " cfg.domains}"}
146− LLMNR=${cfg.llmnr}
147− DNSSEC=${cfg.dnssec}
148− ${config.services.resolved.extraConfig}
149− '';
148+ environment.etc = {
149+ "systemd/resolved.conf".text = ''
150+ [Resolve]
151+ ${optionalString (config.networking.nameservers != [])
152+ "DNS=${concatStringsSep " " config.networking.nameservers}"}
153+ ${optionalString (cfg.fallbackDns != [])
154+ "FallbackDNS=${concatStringsSep " " cfg.fallbackDns}"}
155+ ${optionalString (cfg.domains != [])
156+ "Domains=${concatStringsSep " " cfg.domains}"}
157+ LLMNR=${cfg.llmnr}
158+ DNSSEC=${cfg.dnssec}
159+ ${config.services.resolved.extraConfig}
160+ '';
161+
162+ # symlink the dynamic stub resolver of resolv.conf as recommended by upstream:
163+ # https://www.freedesktop.org/software/systemd/man/systemd-resolved.html#/etc/resolv.conf
164+ "resolv.conf".source = "/run/systemd/resolve/stub-resolv.conf";
165+ } // optionalAttrs dnsmasqResolve {
166+ "dnsmasq-resolv.conf".source = "/run/systemd/resolve/resolv.conf";
167+ };
150168
151169 # If networkmanager is enabled, ask it to interface with resolved.
152170 networking.networkmanager.dns = "systemd-resolved";
171+
153172 };
154173
155174}
nixos/modules/system/boot/stage-2.nix+5−4
@@ -4,19 +4,20 @@ with lib;
44
55let
66
7+ useHostResolvConf = config.networking.resolvconf.enable && config.networking.useHostResolvConf;
8+
79 bootStage2 = pkgs.substituteAll {
810 src = ./stage-2-init.sh;
911 shellDebug = "${pkgs.bashInteractive}/bin/bash";
1012 shell = "${pkgs.bash}/bin/bash";
1113 isExecutable = true;
1214 inherit (config.nix) readOnlyStore;
13− inherit (config.networking) useHostResolvConf;
15+ inherit useHostResolvConf;
1416 inherit (config.system.build) earlyMountScript;
15− path = lib.makeBinPath [
17+ path = lib.makeBinPath ([
1618 pkgs.coreutils
1719 pkgs.utillinux
18− pkgs.openresolv
19− ];
20+ ] ++ lib.optional useHostResolvConf pkgs.openresolv);
2021 fsPackagesPath = lib.makeBinPath config.system.fsPackages;
2122 postBootCommands = pkgs.writeText "local-cmds"
2223 ''
nixos/modules/tasks/network-interfaces-scripted.nix+1−1
@@ -103,7 +103,7 @@ let
103103
104104 script =
105105 ''
106− ${optionalString (!config.environment.etc?"resolv.conf") ''
106+ ${optionalString config.networking.resolvconf.enable ''
107107 # Set the static DNS configuration, if given.
108108 ${pkgs.openresolv}/sbin/resolvconf -m 1 -a static <<EOF
109109 ${optionalString (cfg.nameservers != [] && cfg.domain != null) ''
nixos/modules/tasks/network-interfaces.nix−1
@@ -1017,7 +1017,6 @@ in
10171017 pkgs.iproute
10181018 pkgs.iputils
10191019 pkgs.nettools
1020− pkgs.openresolv
10211020 ]
10221021 ++ optionals config.networking.wireless.enable [
10231022 pkgs.wirelesstools # FIXME: obsolete?