nixos/networking: Add a read-only option for the FQDN

This is a convenience option that can be used to quickly obtain the
configured FQDN.

Changed files
+28 -4
nixos
modules
tests
+18
nixos/modules/tasks/network-interfaces.nix
···
'';
};
+
networking.fqdn = mkOption {
+
readOnly = true;
+
type = types.str;
+
default = if (cfg.hostName != "" && cfg.domain != null)
+
then "${cfg.hostName}.${cfg.domain}"
+
else throw ''
+
The FQDN is required but cannot be determined. Please make sure that
+
both networking.hostName and networking.domain are set properly.
+
'';
+
defaultText = literalExample ''''${networking.hostName}.''${networking.domain}'';
+
description = ''
+
The fully qualified domain name (FQDN) of this host. It is the result
+
of combining networking.hostName and networking.domain. Using this
+
option will result in an evaluation error if the hostname is empty or
+
no domain is specified.
+
'';
+
};
+
networking.hostId = mkOption {
default = null;
example = "4e98920d";
+10 -4
nixos/tests/hostname.nix
···
with pkgs.lib;
let
-
makeHostNameTest = hostName: domain:
+
makeHostNameTest = hostName: domain: fqdnOrNull:
let
fqdn = hostName + (optionalString (domain != null) ".${domain}");
+
getStr = str: # maybeString2String
+
let res = builtins.tryEval str;
+
in if (res.success && res.value != null) then res.value else "null";
in
makeTest {
name = "hostname-${fqdn}";
···
];
};
-
testScript = ''
+
testScript = { nodes, ... }: ''
start_all()
machine = ${hostName}
machine.wait_for_unit("network-online.target")
+
# Test if NixOS computes the correct FQDN (either a FQDN or an error/null):
+
assert "${getStr nodes.machine.config.networking.fqdn}" == "${getStr fqdnOrNull}"
+
# The FQDN, domain name, and hostname detection should work as expected:
assert "${fqdn}" == machine.succeed("hostname --fqdn").strip()
assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip()
···
in
{
-
noExplicitDomain = makeHostNameTest "ahost" null;
+
noExplicitDomain = makeHostNameTest "ahost" null null;
-
explicitDomain = makeHostNameTest "ahost" "adomain";
+
explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain";
}