nixos/networking: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-27 20:42:48 +02:00
parent dd7e20e16a
commit b5b0edaf37

View File

@ -1,28 +1,24 @@
# /etc files related to networking, such as /etc/services.
{ config, lib, options, pkgs, ... }:
with lib;
let
cfg = config.networking;
opt = options.networking;
localhostMultiple = any (elem "localhost") (attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]));
localhostMultiple = lib.any (lib.elem "localhost") (lib.attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]));
in
{
imports = [
(mkRemovedOptionModule [ "networking" "hostConf" ] "Use environment.etc.\"host.conf\" instead.")
(lib.mkRemovedOptionModule [ "networking" "hostConf" ] "Use environment.etc.\"host.conf\" instead.")
];
options = {
networking.hosts = lib.mkOption {
type = types.attrsOf (types.listOf types.str);
example = literalExpression ''
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
example = lib.literalExpression ''
{
"127.0.0.1" = [ "foo.bar.baz" ];
"192.168.0.2" = [ "fileserver.local" "nameserver.local" ];
@ -34,16 +30,16 @@ in
};
networking.hostFiles = lib.mkOption {
type = types.listOf types.path;
defaultText = literalMD "Hosts from {option}`networking.hosts` and {option}`networking.extraHosts`";
example = literalExpression ''[ "''${pkgs.my-blocklist-package}/share/my-blocklist/hosts" ]'';
type = lib.types.listOf lib.types.path;
defaultText = lib.literalMD "Hosts from {option}`networking.hosts` and {option}`networking.extraHosts`";
example = lib.literalExpression ''[ "''${pkgs.my-blocklist-package}/share/my-blocklist/hosts" ]'';
description = ''
Files that should be concatenated together to form {file}`/etc/hosts`.
'';
};
networking.extraHosts = lib.mkOption {
type = types.lines;
type = lib.types.lines;
default = "";
example = "192.168.0.1 lanlocalhost";
description = ''
@ -52,14 +48,14 @@ in
'';
};
networking.timeServers = mkOption {
networking.timeServers = lib.mkOption {
default = [
"0.nixos.pool.ntp.org"
"1.nixos.pool.ntp.org"
"2.nixos.pool.ntp.org"
"3.nixos.pool.ntp.org"
];
type = types.listOf types.str;
type = lib.types.listOf lib.types.str;
description = ''
The set of NTP servers from which to synchronise.
'';
@ -68,7 +64,7 @@ in
networking.proxy = {
default = lib.mkOption {
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
This option specifies the default value for httpProxy, httpsProxy, ftpProxy and rsyncProxy.
@ -77,9 +73,9 @@ in
};
httpProxy = lib.mkOption {
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = cfg.proxy.default;
defaultText = literalExpression "config.${opt.proxy.default}";
defaultText = lib.literalExpression "config.${opt.proxy.default}";
description = ''
This option specifies the http_proxy environment variable.
'';
@ -87,9 +83,9 @@ in
};
httpsProxy = lib.mkOption {
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = cfg.proxy.default;
defaultText = literalExpression "config.${opt.proxy.default}";
defaultText = lib.literalExpression "config.${opt.proxy.default}";
description = ''
This option specifies the https_proxy environment variable.
'';
@ -97,9 +93,9 @@ in
};
ftpProxy = lib.mkOption {
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = cfg.proxy.default;
defaultText = literalExpression "config.${opt.proxy.default}";
defaultText = lib.literalExpression "config.${opt.proxy.default}";
description = ''
This option specifies the ftp_proxy environment variable.
'';
@ -107,9 +103,9 @@ in
};
rsyncProxy = lib.mkOption {
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = cfg.proxy.default;
defaultText = literalExpression "config.${opt.proxy.default}";
defaultText = lib.literalExpression "config.${opt.proxy.default}";
description = ''
This option specifies the rsync_proxy environment variable.
'';
@ -117,9 +113,9 @@ in
};
allProxy = lib.mkOption {
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = cfg.proxy.default;
defaultText = literalExpression "config.${opt.proxy.default}";
defaultText = lib.literalExpression "config.${opt.proxy.default}";
description = ''
This option specifies the all_proxy environment variable.
'';
@ -127,7 +123,7 @@ in
};
noProxy = lib.mkOption {
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
This option specifies the no_proxy environment variable.
@ -138,7 +134,7 @@ in
};
envVars = lib.mkOption {
type = types.attrs;
type = lib.types.attrs;
internal = true;
default = {};
description = ''
@ -163,11 +159,11 @@ in
# hostname and FQDN correctly:
networking.hosts = let
hostnames = # Note: The FQDN (canonical hostname) has to come first:
optional (cfg.hostName != "" && cfg.domain != null) "${cfg.hostName}.${cfg.domain}"
++ optional (cfg.hostName != "") cfg.hostName; # Then the hostname (without the domain)
lib.optional (cfg.hostName != "" && cfg.domain != null) "${cfg.hostName}.${cfg.domain}"
++ lib.optional (cfg.hostName != "") cfg.hostName; # Then the hostname (without the domain)
in {
"127.0.0.2" = hostnames;
} // optionalAttrs cfg.enableIPv6 {
} // lib.optionalAttrs cfg.enableIPv6 {
"::1" = hostnames;
};
@ -178,15 +174,15 @@ in
# FQDN so that e.g. "hostname -f" works correctly.
localhostHosts = pkgs.writeText "localhost-hosts" ''
127.0.0.1 localhost
${optionalString cfg.enableIPv6 "::1 localhost"}
${lib.optionalString cfg.enableIPv6 "::1 localhost"}
'';
stringHosts =
let
oneToString = set: ip: ip + " " + concatStringsSep " " set.${ip} + "\n";
allToString = set: concatMapStrings (oneToString set) (attrNames set);
in pkgs.writeText "string-hosts" (allToString (filterAttrs (_: v: v != []) cfg.hosts));
oneToString = set: ip: ip + " " + lib.concatStringsSep " " set.${ip} + "\n";
allToString = set: lib.concatMapStrings (oneToString set) (lib.attrNames set);
in pkgs.writeText "string-hosts" (allToString (lib.filterAttrs (_: v: v != []) cfg.hosts));
extraHosts = pkgs.writeText "extra-hosts" cfg.extraHosts;
in mkBefore [ localhostHosts stringHosts extraHosts ];
in lib.mkBefore [ localhostHosts stringHosts extraHosts ];
environment.etc =
{ # /etc/services: TCP/UDP port assignments.
@ -199,33 +195,33 @@ in
hosts.source = pkgs.concatText "hosts" cfg.hostFiles;
# /etc/netgroup: Network-wide groups.
netgroup.text = mkDefault "";
netgroup.text = lib.mkDefault "";
# /etc/host.conf: resolver configuration file
"host.conf".text = ''
multi on
'';
} // optionalAttrs (pkgs.stdenv.hostPlatform.libc == "glibc") {
} // lib.optionalAttrs (pkgs.stdenv.hostPlatform.libc == "glibc") {
# /etc/rpc: RPC program numbers.
rpc.source = pkgs.stdenv.cc.libc.out + "/etc/rpc";
};
networking.proxy.envVars =
optionalAttrs (cfg.proxy.default != null) {
lib.optionalAttrs (cfg.proxy.default != null) {
# other options already fallback to proxy.default
no_proxy = "127.0.0.1,localhost";
} // optionalAttrs (cfg.proxy.httpProxy != null) {
} // lib.optionalAttrs (cfg.proxy.httpProxy != null) {
http_proxy = cfg.proxy.httpProxy;
} // optionalAttrs (cfg.proxy.httpsProxy != null) {
} // lib.optionalAttrs (cfg.proxy.httpsProxy != null) {
https_proxy = cfg.proxy.httpsProxy;
} // optionalAttrs (cfg.proxy.rsyncProxy != null) {
} // lib.optionalAttrs (cfg.proxy.rsyncProxy != null) {
rsync_proxy = cfg.proxy.rsyncProxy;
} // optionalAttrs (cfg.proxy.ftpProxy != null) {
} // lib.optionalAttrs (cfg.proxy.ftpProxy != null) {
ftp_proxy = cfg.proxy.ftpProxy;
} // optionalAttrs (cfg.proxy.allProxy != null) {
} // lib.optionalAttrs (cfg.proxy.allProxy != null) {
all_proxy = cfg.proxy.allProxy;
} // optionalAttrs (cfg.proxy.noProxy != null) {
} // lib.optionalAttrs (cfg.proxy.noProxy != null) {
no_proxy = cfg.proxy.noProxy;
};