at 22.05-pre 7.6 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 11 localhostMultiple = any (elem "localhost") (attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ])); 12 13in 14 15{ 16 imports = [ 17 (mkRemovedOptionModule [ "networking" "hostConf" ] "Use environment.etc.\"host.conf\" instead.") 18 ]; 19 20 options = { 21 22 networking.hosts = lib.mkOption { 23 type = types.attrsOf (types.listOf types.str); 24 example = literalExpression '' 25 { 26 "127.0.0.1" = [ "foo.bar.baz" ]; 27 "192.168.0.2" = [ "fileserver.local" "nameserver.local" ]; 28 }; 29 ''; 30 description = '' 31 Locally defined maps of hostnames to IP addresses. 32 ''; 33 }; 34 35 networking.hostFiles = lib.mkOption { 36 type = types.listOf types.path; 37 defaultText = literalDocBook "Hosts from <option>networking.hosts</option> and <option>networking.extraHosts</option>"; 38 example = literalExpression ''[ "''${pkgs.my-blocklist-package}/share/my-blocklist/hosts" ]''; 39 description = '' 40 Files that should be concatenated together to form <filename>/etc/hosts</filename>. 41 ''; 42 }; 43 44 networking.extraHosts = lib.mkOption { 45 type = types.lines; 46 default = ""; 47 example = "192.168.0.1 lanlocalhost"; 48 description = '' 49 Additional verbatim entries to be appended to <filename>/etc/hosts</filename>. 50 For adding hosts from derivation results, use <option>networking.hostFiles</option> instead. 51 ''; 52 }; 53 54 networking.timeServers = mkOption { 55 default = [ 56 "0.nixos.pool.ntp.org" 57 "1.nixos.pool.ntp.org" 58 "2.nixos.pool.ntp.org" 59 "3.nixos.pool.ntp.org" 60 ]; 61 type = types.listOf types.str; 62 description = '' 63 The set of NTP servers from which to synchronise. 64 ''; 65 }; 66 67 networking.proxy = { 68 69 default = lib.mkOption { 70 type = types.nullOr types.str; 71 default = null; 72 description = '' 73 This option specifies the default value for httpProxy, httpsProxy, ftpProxy and rsyncProxy. 74 ''; 75 example = "http://127.0.0.1:3128"; 76 }; 77 78 httpProxy = lib.mkOption { 79 type = types.nullOr types.str; 80 default = cfg.proxy.default; 81 description = '' 82 This option specifies the http_proxy environment variable. 83 ''; 84 example = "http://127.0.0.1:3128"; 85 }; 86 87 httpsProxy = lib.mkOption { 88 type = types.nullOr types.str; 89 default = cfg.proxy.default; 90 description = '' 91 This option specifies the https_proxy environment variable. 92 ''; 93 example = "http://127.0.0.1:3128"; 94 }; 95 96 ftpProxy = lib.mkOption { 97 type = types.nullOr types.str; 98 default = cfg.proxy.default; 99 description = '' 100 This option specifies the ftp_proxy environment variable. 101 ''; 102 example = "http://127.0.0.1:3128"; 103 }; 104 105 rsyncProxy = lib.mkOption { 106 type = types.nullOr types.str; 107 default = cfg.proxy.default; 108 description = '' 109 This option specifies the rsync_proxy environment variable. 110 ''; 111 example = "http://127.0.0.1:3128"; 112 }; 113 114 allProxy = lib.mkOption { 115 type = types.nullOr types.str; 116 default = cfg.proxy.default; 117 description = '' 118 This option specifies the all_proxy environment variable. 119 ''; 120 example = "http://127.0.0.1:3128"; 121 }; 122 123 noProxy = lib.mkOption { 124 type = types.nullOr types.str; 125 default = null; 126 description = '' 127 This option specifies the no_proxy environment variable. 128 If a default proxy is used and noProxy is null, 129 then noProxy will be set to 127.0.0.1,localhost. 130 ''; 131 example = "127.0.0.1,localhost,.localdomain"; 132 }; 133 134 envVars = lib.mkOption { 135 type = types.attrs; 136 internal = true; 137 default = {}; 138 description = '' 139 Environment variables used for the network proxy. 140 ''; 141 }; 142 }; 143 }; 144 145 config = { 146 147 assertions = [{ 148 assertion = !localhostMultiple; 149 message = '' 150 `networking.hosts` maps "localhost" to something other than "127.0.0.1" 151 or "::1". This will break some applications. Please use 152 `networking.extraHosts` if you really want to add such a mapping. 153 ''; 154 }]; 155 156 # These entries are required for "hostname -f" and to resolve both the 157 # hostname and FQDN correctly: 158 networking.hosts = let 159 hostnames = # Note: The FQDN (canonical hostname) has to come first: 160 optional (cfg.hostName != "" && cfg.domain != null) "${cfg.hostName}.${cfg.domain}" 161 ++ optional (cfg.hostName != "") cfg.hostName; # Then the hostname (without the domain) 162 in { 163 "127.0.0.2" = hostnames; 164 } // optionalAttrs cfg.enableIPv6 { 165 "::1" = hostnames; 166 }; 167 168 networking.hostFiles = let 169 # Note: localhostHosts has to appear first in /etc/hosts so that 127.0.0.1 170 # resolves back to "localhost" (as some applications assume) instead of 171 # the FQDN! By default "networking.hosts" also contains entries for the 172 # FQDN so that e.g. "hostname -f" works correctly. 173 localhostHosts = pkgs.writeText "localhost-hosts" '' 174 127.0.0.1 localhost 175 ${optionalString cfg.enableIPv6 "::1 localhost"} 176 ''; 177 stringHosts = 178 let 179 oneToString = set: ip: ip + " " + concatStringsSep " " set.${ip} + "\n"; 180 allToString = set: concatMapStrings (oneToString set) (attrNames set); 181 in pkgs.writeText "string-hosts" (allToString (filterAttrs (_: v: v != []) cfg.hosts)); 182 extraHosts = pkgs.writeText "extra-hosts" cfg.extraHosts; 183 in mkBefore [ localhostHosts stringHosts extraHosts ]; 184 185 environment.etc = 186 { # /etc/services: TCP/UDP port assignments. 187 services.source = pkgs.iana-etc + "/etc/services"; 188 189 # /etc/protocols: IP protocol numbers. 190 protocols.source = pkgs.iana-etc + "/etc/protocols"; 191 192 # /etc/hosts: Hostname-to-IP mappings. 193 hosts.source = pkgs.runCommand "hosts" {} '' 194 cat ${escapeShellArgs cfg.hostFiles} > $out 195 ''; 196 197 # /etc/netgroup: Network-wide groups. 198 netgroup.text = mkDefault ""; 199 200 # /etc/host.conf: resolver configuration file 201 "host.conf".text = '' 202 multi on 203 ''; 204 205 } // optionalAttrs (pkgs.stdenv.hostPlatform.libc == "glibc") { 206 # /etc/rpc: RPC program numbers. 207 rpc.source = pkgs.stdenv.cc.libc.out + "/etc/rpc"; 208 }; 209 210 networking.proxy.envVars = 211 optionalAttrs (cfg.proxy.default != null) { 212 # other options already fallback to proxy.default 213 no_proxy = "127.0.0.1,localhost"; 214 } // optionalAttrs (cfg.proxy.httpProxy != null) { 215 http_proxy = cfg.proxy.httpProxy; 216 } // optionalAttrs (cfg.proxy.httpsProxy != null) { 217 https_proxy = cfg.proxy.httpsProxy; 218 } // optionalAttrs (cfg.proxy.rsyncProxy != null) { 219 rsync_proxy = cfg.proxy.rsyncProxy; 220 } // optionalAttrs (cfg.proxy.ftpProxy != null) { 221 ftp_proxy = cfg.proxy.ftpProxy; 222 } // optionalAttrs (cfg.proxy.allProxy != null) { 223 all_proxy = cfg.proxy.allProxy; 224 } // optionalAttrs (cfg.proxy.noProxy != null) { 225 no_proxy = cfg.proxy.noProxy; 226 }; 227 228 # Install the proxy environment variables 229 environment.sessionVariables = cfg.proxy.envVars; 230 231 }; 232 233}