at v206 7.5 kB view raw
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; 10 dnsmasqResolve = config.services.dnsmasq.enable && 11 config.services.dnsmasq.resolveLocalQueries; 12 hasLocalResolver = config.services.bind.enable || dnsmasqResolve; 13 14in 15 16{ 17 18 options = { 19 20 networking.extraHosts = lib.mkOption { 21 type = types.lines; 22 default = ""; 23 example = "192.168.0.1 lanlocalhost"; 24 description = '' 25 Additional entries to be appended to <filename>/etc/hosts</filename>. 26 ''; 27 }; 28 29 networking.dnsSingleRequest = lib.mkOption { 30 type = types.bool; 31 default = false; 32 description = '' 33 Recent versions of glibc will issue both ipv4 (A) and ipv6 (AAAA) 34 address queries at the same time, from the same port. Sometimes upstream 35 routers will systemically drop the ipv4 queries. The symptom of this problem is 36 that 'getent hosts example.com' only returns ipv6 (or perhaps only ipv4) addresses. The 37 workaround for this is to specify the option 'single-request' in 38 /etc/resolv.conf. This option enables that. 39 ''; 40 }; 41 42 networking.extraResolvconfConf = lib.mkOption { 43 type = types.lines; 44 default = ""; 45 example = "libc=NO"; 46 description = '' 47 Extra configuration to append to <filename>resolvconf.conf</filename>. 48 ''; 49 }; 50 51 52 networking.proxy = { 53 54 default = lib.mkOption { 55 type = types.nullOr types.str; 56 default = null; 57 description = '' 58 This option specifies the default value for httpProxy, httpsProxy, ftpProxy and rsyncProxy. 59 ''; 60 example = "http://127.0.0.1:3128"; 61 }; 62 63 httpProxy = lib.mkOption { 64 type = types.nullOr types.str; 65 default = cfg.proxy.default; 66 description = '' 67 This option specifies the http_proxy environment variable. 68 ''; 69 example = "http://127.0.0.1:3128"; 70 }; 71 72 httpsProxy = lib.mkOption { 73 type = types.nullOr types.str; 74 default = cfg.proxy.default; 75 description = '' 76 This option specifies the https_proxy environment variable. 77 ''; 78 example = "http://127.0.0.1:3128"; 79 }; 80 81 ftpProxy = lib.mkOption { 82 type = types.nullOr types.str; 83 default = cfg.proxy.default; 84 description = '' 85 This option specifies the ftp_proxy environment variable. 86 ''; 87 example = "http://127.0.0.1:3128"; 88 }; 89 90 rsyncProxy = lib.mkOption { 91 type = types.nullOr types.str; 92 default = cfg.proxy.default; 93 description = '' 94 This option specifies the rsync_proxy environment variable. 95 ''; 96 example = "http://127.0.0.1:3128"; 97 }; 98 99 noProxy = lib.mkOption { 100 type = types.nullOr types.str; 101 default = null; 102 description = '' 103 This option specifies the no_proxy environment variable. 104 If a default proxy is used and noProxy is null, 105 then noProxy will be set to 127.0.0.1,localhost. 106 ''; 107 example = "127.0.0.1,localhost,.localdomain"; 108 }; 109 110 envVars = lib.mkOption { 111 type = types.attrs; 112 internal = true; 113 default = {}; 114 description = '' 115 Environment variables used for the network proxy. 116 ''; 117 }; 118 }; 119 }; 120 121 config = { 122 123 environment.etc = 124 { # /etc/services: TCP/UDP port assignments. 125 "services".source = pkgs.iana_etc + "/etc/services"; 126 127 # /etc/protocols: IP protocol numbers. 128 "protocols".source = pkgs.iana_etc + "/etc/protocols"; 129 130 # /etc/rpc: RPC program numbers. 131 "rpc".source = pkgs.glibc + "/etc/rpc"; 132 133 # /etc/hosts: Hostname-to-IP mappings. 134 "hosts".text = 135 '' 136 127.0.0.1 localhost 137 ${optionalString cfg.enableIPv6 '' 138 ::1 localhost 139 ''} 140 ${cfg.extraHosts} 141 ''; 142 143 # /etc/resolvconf.conf: Configuration for openresolv. 144 "resolvconf.conf".text = 145 '' 146 # This is the default, but we must set it here to prevent 147 # a collision with an apparently unrelated environment 148 # variable with the same name exported by dhcpcd. 149 interface_order='lo lo[0-9]*' 150 '' + optionalString config.services.nscd.enable '' 151 # Invalidate the nscd cache whenever resolv.conf is 152 # regenerated. 153 libc_restart='${pkgs.systemd}/bin/systemctl try-restart --no-block nscd.service 2> /dev/null' 154 '' + optionalString cfg.dnsSingleRequest '' 155 # only send one DNS request at a time 156 resolv_conf_options='single-request' 157 '' + optionalString hasLocalResolver '' 158 # This hosts runs a full-blown DNS resolver. 159 name_servers='127.0.0.1' 160 '' + optionalString dnsmasqResolve '' 161 dnsmasq_conf=/etc/dnsmasq-conf.conf 162 dnsmasq_resolv=/etc/dnsmasq-resolv.conf 163 '' + cfg.extraResolvconfConf + '' 164 ''; 165 166 } // (optionalAttrs config.services.resolved.enable ( 167 if dnsmasqResolve then { 168 "dnsmasq-resolv.conf".source = "/run/systemd/resolve/resolv.conf"; 169 } else { 170 "resolv.conf".source = "/run/systemd/resolve/resolv.conf"; 171 } 172 )); 173 174 networking.proxy.envVars = 175 optionalAttrs (cfg.proxy.default != null) { 176 # other options already fallback to proxy.default 177 no_proxy = "127.0.0.1,localhost"; 178 } // optionalAttrs (cfg.proxy.httpProxy != null) { 179 http_proxy = cfg.proxy.httpProxy; 180 } // optionalAttrs (cfg.proxy.httpsProxy != null) { 181 https_proxy = cfg.proxy.httpsProxy; 182 } // optionalAttrs (cfg.proxy.rsyncProxy != null) { 183 rsync_proxy = cfg.proxy.rsyncProxy; 184 } // optionalAttrs (cfg.proxy.ftpProxy != null) { 185 ftp_proxy = cfg.proxy.ftpProxy; 186 } // optionalAttrs (cfg.proxy.noProxy != null) { 187 no_proxy = cfg.proxy.noProxy; 188 }; 189 190 # Install the proxy environment variables 191 environment.sessionVariables = cfg.proxy.envVars; 192 193 # The ‘ip-up’ target is started when we have IP connectivity. So 194 # services that depend on IP connectivity (like ntpd) should be 195 # pulled in by this target. 196 systemd.targets.ip-up.description = "Services Requiring IP Connectivity"; 197 198 # This is needed when /etc/resolv.conf is being overriden by networkd 199 # and other configurations. If the file is destroyed by an environment 200 # activation then it must be rebuilt so that applications which interface 201 # with /etc/resolv.conf directly don't break. 202 system.activationScripts.resolvconf = stringAfter [ "etc" "tmpfs" "var" ] 203 '' 204 # Systemd resolved controls its own resolv.conf 205 rm -f /run/resolvconf/interfaces/systemd 206 ${optionalString config.services.resolved.enable '' 207 rm -rf /run/resolvconf/interfaces 208 mkdir -p /run/resolvconf/interfaces 209 ln -s /run/systemd/resolve/resolv.conf /run/resolvconf/interfaces/systemd 210 ''} 211 212 # Make sure resolv.conf is up to date if not managed by systemd 213 ${optionalString (!config.services.resolved.enable) '' 214 ${pkgs.openresolv}/bin/resolvconf -u 215 ''} 216 ''; 217 218 }; 219 220 }