1# /etc files related to networking, such as /etc/services.
2
3{ config, lib, pkgs, ... }:
4
5with lib;
6
7let
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='/run/current-system/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
33in
34
35{
36 imports = [
37 (mkRenamedOptionModule [ "networking" "dnsSingleRequest" ] [ "networking" "resolvconf" "dnsSingleRequest" ])
38 (mkRenamedOptionModule [ "networking" "dnsExtensionMechanism" ] [ "networking" "resolvconf" "dnsExtensionMechanism" ])
39 (mkRenamedOptionModule [ "networking" "extraResolvconfConf" ] [ "networking" "resolvconf" "extraConfig" ])
40 (mkRenamedOptionModule [ "networking" "resolvconfOptions" ] [ "networking" "resolvconf" "extraOptions" ])
41 (mkRemovedOptionModule [ "networking" "resolvconf" "useHostResolvConf" ] "This option was never used for anything anyways")
42 ];
43
44 options = {
45
46 networking.resolvconf = {
47
48 enable = mkOption {
49 type = types.bool;
50 default = !(config.environment.etc ? "resolv.conf");
51 defaultText = literalExpression ''!(config.environment.etc ? "resolv.conf")'';
52 description = lib.mdDoc ''
53 Whether DNS configuration is managed by resolvconf.
54 '';
55 };
56
57 package = mkOption {
58 type = types.package;
59 default = pkgs.openresolv;
60 defaultText = literalExpression "pkgs.openresolv";
61 description = lib.mdDoc ''
62 The package that provides the system-wide resolvconf command. Defaults to `openresolv`
63 if this module is enabled. Otherwise, can be used by other modules (for example {option}`services.resolved`) to
64 provide a compatibility layer.
65
66 This option generally shouldn't be set by the user.
67 '';
68 };
69
70 dnsSingleRequest = lib.mkOption {
71 type = types.bool;
72 default = false;
73 description = lib.mdDoc ''
74 Recent versions of glibc will issue both ipv4 (A) and ipv6 (AAAA)
75 address queries at the same time, from the same port. Sometimes upstream
76 routers will systemically drop the ipv4 queries. The symptom of this problem is
77 that 'getent hosts example.com' only returns ipv6 (or perhaps only ipv4) addresses. The
78 workaround for this is to specify the option 'single-request' in
79 /etc/resolv.conf. This option enables that.
80 '';
81 };
82
83 dnsExtensionMechanism = mkOption {
84 type = types.bool;
85 default = true;
86 description = lib.mdDoc ''
87 Enable the `edns0` option in {file}`resolv.conf`. With
88 that option set, `glibc` supports use of the extension mechanisms for
89 DNS (EDNS) specified in RFC 2671. The most popular user of that feature is DNSSEC,
90 which does not work without it.
91 '';
92 };
93
94 extraConfig = mkOption {
95 type = types.lines;
96 default = "";
97 example = "libc=NO";
98 description = lib.mdDoc ''
99 Extra configuration to append to {file}`resolvconf.conf`.
100 '';
101 };
102
103 extraOptions = mkOption {
104 type = types.listOf types.str;
105 default = [];
106 example = [ "ndots:1" "rotate" ];
107 description = lib.mdDoc ''
108 Set the options in {file}`/etc/resolv.conf`.
109 '';
110 };
111
112 useLocalResolver = mkOption {
113 type = types.bool;
114 default = false;
115 description = lib.mdDoc ''
116 Use local DNS server for resolving.
117 '';
118 };
119
120 };
121
122 };
123
124 config = mkMerge [
125 {
126 environment.etc."resolvconf.conf".text =
127 if !cfg.enable then
128 # Force-stop any attempts to use resolvconf
129 ''
130 echo "resolvconf is disabled on this system but was used anyway:" >&2
131 echo "$0 $*" >&2
132 exit 1
133 ''
134 else configText;
135
136 environment.systemPackages = [ cfg.package ];
137 }
138
139 (mkIf cfg.enable {
140 networking.resolvconf.package = pkgs.openresolv;
141
142 systemd.services.resolvconf = {
143 description = "resolvconf update";
144
145 before = [ "network-pre.target" ];
146 wants = [ "network-pre.target" ];
147 wantedBy = [ "multi-user.target" ];
148 restartTriggers = [ config.environment.etc."resolvconf.conf".source ];
149
150 serviceConfig = {
151 Type = "oneshot";
152 ExecStart = "${cfg.package}/bin/resolvconf -u";
153 RemainAfterExit = true;
154 };
155 };
156
157 })
158 ];
159
160}