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