git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #100155 from primeos/nixos-add-fqdn-option

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

commitb2f3bd4d79398246a5ce1ea7dfc906d092d1def4 authorFlorian Klink <flokli@flokli.de> dateMon 25 Jan 2021, 16:45 (6 years ago) parent68b6d6c237c20a
4 files changed+44−17
nixos/modules/services/networking/smokeping.nix+15−13 nixos/modules/tasks/network-interfaces.nix+18 nixos/tests/hostname.nix+10−4 nixos/tests/smokeping.nix+1
nixos/modules/services/networking/smokeping.nix+15−13
@@ -124,7 +124,8 @@ in
124124 };
125125 hostName = mkOption {
126126 type = types.str;
127− default = config.networking.hostName;
127+ default = config.networking.fqdn;
128+ defaultText = "\${config.networking.fqdn}";
128129 example = "somewhere.example.com";
129130 description = "DNS name for the urls generated in the cgi.";
130131 };
@@ -156,6 +157,7 @@ in
156157 ownerEmail = mkOption {
157158 type = types.str;
158159 default = "no-reply@${cfg.hostName}";
160+ defaultText = "no-reply@\${hostName}";
159161 example = "no-reply@yourdomain.com";
160162 description = "Email contact for owner";
161163 };
@@ -239,18 +241,18 @@ in
239241 targetConfig = mkOption {
240242 type = types.lines;
241243 default = ''
242− probe = FPing
243− menu = Top
244− title = Network Latency Grapher
245− remark = Welcome to the SmokePing website of xxx Company. \
246− Here you will learn all about the latency of our network.
247− + Local
248− menu = Local
249− title = Local Network
250− ++ LocalMachine
251− menu = Local Machine
252− title = This host
253− host = localhost
244+ probe = FPing
245+ menu = Top
246+ title = Network Latency Grapher
247+ remark = Welcome to the SmokePing website of xxx Company. \
248+ Here you will learn all about the latency of our network.
249+ + Local
250+ menu = Local
251+ title = Local Network
252+ ++ LocalMachine
253+ menu = Local Machine
254+ title = This host
255+ host = localhost
254256 '';
255257 description = "Target configuration";
256258 };
nixos/modules/tasks/network-interfaces.nix+18
@@ -398,6 +398,24 @@ in
398398 '';
399399 };
400400
401+ networking.fqdn = mkOption {
402+ readOnly = true;
403+ type = types.str;
404+ default = if (cfg.hostName != "" && cfg.domain != null)
405+ then "${cfg.hostName}.${cfg.domain}"
406+ else throw ''
407+ The FQDN is required but cannot be determined. Please make sure that
408+ both networking.hostName and networking.domain are set properly.
409+ '';
410+ defaultText = literalExample ''''${networking.hostName}.''${networking.domain}'';
411+ description = ''
412+ The fully qualified domain name (FQDN) of this host. It is the result
413+ of combining networking.hostName and networking.domain. Using this
414+ option will result in an evaluation error if the hostname is empty or
415+ no domain is specified.
416+ '';
417+ };
418+
401419 networking.hostId = mkOption {
402420 default = null;
403421 example = "4e98920d";
nixos/tests/hostname.nix+10−4
@@ -7,9 +7,12 @@ with import ../lib/testing-python.nix { inherit system pkgs; };
77with pkgs.lib;
88
99let
10− makeHostNameTest = hostName: domain:
10+ makeHostNameTest = hostName: domain: fqdnOrNull:
1111 let
1212 fqdn = hostName + (optionalString (domain != null) ".${domain}");
13+ getStr = str: # maybeString2String
14+ let res = builtins.tryEval str;
15+ in if (res.success && res.value != null) then res.value else "null";
1316 in
1417 makeTest {
1518 name = "hostname-${fqdn}";
@@ -26,13 +29,16 @@ let
2629 ];
2730 };
2831
29− testScript = ''
32+ testScript = { nodes, ... }: ''
3033 start_all()
3134
3235 machine = ${hostName}
3336
3437 machine.wait_for_unit("network-online.target")
3538
39+ # Test if NixOS computes the correct FQDN (either a FQDN or an error/null):
40+ assert "${getStr nodes.machine.config.networking.fqdn}" == "${getStr fqdnOrNull}"
41+
3642 # The FQDN, domain name, and hostname detection should work as expected:
3743 assert "${fqdn}" == machine.succeed("hostname --fqdn").strip()
3844 assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip()
@@ -60,7 +66,7 @@ let
6066
6167in
6268{
63− noExplicitDomain = makeHostNameTest "ahost" null;
69+ noExplicitDomain = makeHostNameTest "ahost" null null;
6470
65− explicitDomain = makeHostNameTest "ahost" "adomain";
71+ explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain";
6672}
nixos/tests/smokeping.nix+1
@@ -8,6 +8,7 @@ import ./make-test-python.nix ({ pkgs, ...} : {
88 sm =
99 { ... }:
1010 {
11+ networking.domain = "example.com"; # FQDN: sm.example.com
1112 services.smokeping = {
1213 enable = true;
1314 port = 8081;