1 # /etc files related to networking, such as /etc/services.
3 { config, lib, options, pkgs, ... }:
9 cfg = config.networking;
10 opt = options.networking;
12 localhostMultiple = any (elem "localhost") (attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]));
18 (mkRemovedOptionModule [ "networking" "hostConf" ] "Use environment.etc.\"host.conf\" instead.")
23 networking.hosts = lib.mkOption {
24 type = types.attrsOf (types.listOf types.str);
25 example = literalExpression ''
27 "127.0.0.1" = [ "foo.bar.baz" ];
28 "192.168.0.2" = [ "fileserver.local" "nameserver.local" ];
31 description = lib.mdDoc ''
32 Locally defined maps of hostnames to IP addresses.
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`.
45 networking.extraHosts = lib.mkOption {
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.
55 networking.timeServers = mkOption {
57 "0.nixos.pool.ntp.org"
58 "1.nixos.pool.ntp.org"
59 "2.nixos.pool.ntp.org"
60 "3.nixos.pool.ntp.org"
62 type = types.listOf types.str;
63 description = lib.mdDoc ''
64 The set of NTP servers from which to synchronise.
70 default = lib.mkOption {
71 type = types.nullOr types.str;
73 description = lib.mdDoc ''
74 This option specifies the default value for httpProxy, httpsProxy, ftpProxy and rsyncProxy.
76 example = "http://127.0.0.1:3128";
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.
86 example = "http://127.0.0.1:3128";
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.
96 example = "http://127.0.0.1:3128";
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.
106 example = "http://127.0.0.1:3128";
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.
116 example = "http://127.0.0.1:3128";
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.
126 example = "http://127.0.0.1:3128";
129 noProxy = lib.mkOption {
130 type = types.nullOr types.str;
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.
137 example = "127.0.0.1,localhost,.localdomain";
140 envVars = lib.mkOption {
144 description = lib.mdDoc ''
145 Environment variables used for the network proxy.
154 assertion = !localhostMultiple;
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.
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)
169 "127.0.0.2" = hostnames;
170 } // optionalAttrs cfg.enableIPv6 {
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" ''
181 ${optionalString cfg.enableIPv6 "::1 localhost"}
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 ];
192 { # /etc/services: TCP/UDP port assignments.
193 services.source = pkgs.iana-etc + "/etc/services";
195 # /etc/protocols: IP protocol numbers.
196 protocols.source = pkgs.iana-etc + "/etc/protocols";
198 # /etc/hosts: Hostname-to-IP mappings.
199 hosts.source = pkgs.concatText "hosts" cfg.hostFiles;
201 # /etc/netgroup: Network-wide groups.
202 netgroup.text = mkDefault "";
204 # /etc/host.conf: resolver configuration file
205 "host.conf".text = ''
209 } // optionalAttrs (pkgs.stdenv.hostPlatform.libc == "glibc") {
210 # /etc/rpc: RPC program numbers.
211 rpc.source = pkgs.stdenv.cc.libc.out + "/etc/rpc";
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;
232 # Install the proxy environment variables
233 environment.sessionVariables = cfg.proxy.envVars;