From 971f0b45ef7129073a6e996f1c63a1f2b470a7e0 Mon Sep 17 00:00:00 2001 From: Michael Weiss Date: Sat, 10 Oct 2020 11:30:47 +0000 Subject: [PATCH 1/3] 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. --- nixos/modules/tasks/network-interfaces.nix | 18 ++++++++++++++++++ nixos/tests/hostname.nix | 14 ++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/nixos/modules/tasks/network-interfaces.nix b/nixos/modules/tasks/network-interfaces.nix index d369aab5457..63a704e31f4 100644 --- a/nixos/modules/tasks/network-interfaces.nix +++ b/nixos/modules/tasks/network-interfaces.nix @@ -394,6 +394,24 @@ in ''; }; + 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"; diff --git a/nixos/tests/hostname.nix b/nixos/tests/hostname.nix index 3b87303d73e..8a77afc2173 100644 --- a/nixos/tests/hostname.nix +++ b/nixos/tests/hostname.nix @@ -7,9 +7,12 @@ with import ../lib/testing-python.nix { inherit system pkgs; }; 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}"; @@ -26,13 +29,16 @@ let ]; }; - 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() @@ -60,7 +66,7 @@ let in { - noExplicitDomain = makeHostNameTest "ahost" null; + noExplicitDomain = makeHostNameTest "ahost" null null; - explicitDomain = makeHostNameTest "ahost" "adomain"; + explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain"; } From 87fb5d381f13f2e7cce281d8f494cd1fb44ba5b5 Mon Sep 17 00:00:00 2001 From: Michael Weiss Date: Sun, 17 Jan 2021 20:42:20 +0100 Subject: [PATCH 2/3] nixos/smokeping: Add a PoC for using networking.fqdn --- nixos/modules/services/networking/smokeping.nix | 4 +++- nixos/tests/smokeping.nix | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/networking/smokeping.nix b/nixos/modules/services/networking/smokeping.nix index 37ee2a80389..35506e0395a 100644 --- a/nixos/modules/services/networking/smokeping.nix +++ b/nixos/modules/services/networking/smokeping.nix @@ -124,7 +124,8 @@ in }; hostName = mkOption { type = types.str; - default = config.networking.hostName; + default = config.networking.fqdn; + defaultText = "\${config.networking.fqdn}"; example = "somewhere.example.com"; description = "DNS name for the urls generated in the cgi."; }; @@ -156,6 +157,7 @@ in ownerEmail = mkOption { type = types.str; default = "no-reply@${cfg.hostName}"; + defaultText = "no-reply@\${hostName}"; example = "no-reply@yourdomain.com"; description = "Email contact for owner"; }; diff --git a/nixos/tests/smokeping.nix b/nixos/tests/smokeping.nix index 4f8f0fcc9fe..77a980153ad 100644 --- a/nixos/tests/smokeping.nix +++ b/nixos/tests/smokeping.nix @@ -8,6 +8,7 @@ import ./make-test-python.nix ({ pkgs, ...} : { sm = { ... }: { + networking.domain = "example.com"; # FQDN: sm.example.com services.smokeping = { enable = true; port = 8081; From 237c20ac61d0e65df7cbbe7957499895b3f82c23 Mon Sep 17 00:00:00 2001 From: Michael Weiss Date: Sat, 23 Jan 2021 12:26:34 +0100 Subject: [PATCH 3/3] nixos/smokeping: Replace the tabs in cfg.targetConfig This was inconsistent with the rest of the module. --- .../modules/services/networking/smokeping.nix | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/nixos/modules/services/networking/smokeping.nix b/nixos/modules/services/networking/smokeping.nix index 35506e0395a..d32f96f500b 100644 --- a/nixos/modules/services/networking/smokeping.nix +++ b/nixos/modules/services/networking/smokeping.nix @@ -241,18 +241,18 @@ in targetConfig = mkOption { type = types.lines; default = '' - probe = FPing - menu = Top - title = Network Latency Grapher - remark = Welcome to the SmokePing website of xxx Company. \ - Here you will learn all about the latency of our network. - + Local - menu = Local - title = Local Network - ++ LocalMachine - menu = Local Machine - title = This host - host = localhost + probe = FPing + menu = Top + title = Network Latency Grapher + remark = Welcome to the SmokePing website of xxx Company. \ + Here you will learn all about the latency of our network. + + Local + menu = Local + title = Local Network + ++ LocalMachine + menu = Local Machine + title = This host + host = localhost ''; description = "Target configuration"; };