1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-12-25 03:17:13 +00:00
nixpkgs/nixos/tests/hostname.nix
Alyssa Ross e9eff47002 nixos/networking: don't add extra names to ::1
From hosts(5):

> For each host a single line should be present with the following
> information:
>
>               IP_address canonical_hostname [aliases...]

With lines like "::1 localhost ahost.adomin ahost", we were saying
that the canonical name for "ahost" was "localhost", the opposite of a
canonical name.  This is why a second loopback address (127.0.0.2) is
used for hostnames with IPv4 — if they were put after "localhost" on
the 127.0.0.1 line, the same thing would happen.  With IPv6 we can't
do the same thing as there's only a single loopback address, so
instead the right thing to do is to simply not list the hostnames in
/etc/hosts, and rely on the myhostname NSS plugin, which will handle
this correctly.

(Note that the examples in hosts(5) also do not include IPv6 FQDN or
hostname entries.)
2024-12-11 11:09:40 +01:00

76 lines
2.4 KiB
Nix

{ system ? builtins.currentSystem
, config ? { }
, pkgs ? import ../.. { inherit system config; }
}:
with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib;
let
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}";
meta = with pkgs.lib.maintainers; {
maintainers = [ primeos blitz ];
};
nodes.machine = { lib, ... }: {
networking.hostName = hostName;
networking.domain = domain;
environment.systemPackages = with pkgs; [
inetutils
];
};
testScript = { nodes, ... }: ''
start_all()
machine = ${hostName}
machine.systemctl("start network-online.target")
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.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()
assert (
"${hostName}"
== machine.succeed(
'hostnamectl status | grep "Static hostname" | cut -d: -f2'
).strip()
)
# 127.0.0.1 and ::1 should resolve back to "localhost":
assert (
"localhost" == machine.succeed("getent hosts 127.0.0.1 | awk '{print $2}'").strip()
)
assert "localhost" == machine.succeed("getent hosts ::1 | awk '{print $2}'").strip()
# 127.0.0.2 should resolve back to the FQDN and hostname:
fqdn_and_host_name = "${optionalString (domain != null) "${hostName}.${domain} "}${hostName}"
assert (
fqdn_and_host_name
== machine.succeed("getent hosts 127.0.0.2 | awk '{print $2,$3}'").strip()
)
assert "${fqdn}" == machine.succeed("getent hosts ${hostName} | awk '{print $2}'").strip()
'';
};
in
{
noExplicitDomain = makeHostNameTest "ahost" null null;
explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain";
}