Merge pull request #100155 from primeos/nixos-add-fqdn-option

nixos/networking: Add a read-only option for the FQDN
This commit is contained in:
Florian Klink 2021-01-25 16:45:45 +01:00 committed by GitHub
commit b2f3bd4d79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 17 deletions

View File

@ -124,7 +124,8 @@ in
}; };
hostName = mkOption { hostName = mkOption {
type = types.str; type = types.str;
default = config.networking.hostName; default = config.networking.fqdn;
defaultText = "\${config.networking.fqdn}";
example = "somewhere.example.com"; example = "somewhere.example.com";
description = "DNS name for the urls generated in the cgi."; description = "DNS name for the urls generated in the cgi.";
}; };
@ -156,6 +157,7 @@ in
ownerEmail = mkOption { ownerEmail = mkOption {
type = types.str; type = types.str;
default = "no-reply@${cfg.hostName}"; default = "no-reply@${cfg.hostName}";
defaultText = "no-reply@\${hostName}";
example = "no-reply@yourdomain.com"; example = "no-reply@yourdomain.com";
description = "Email contact for owner"; description = "Email contact for owner";
}; };
@ -239,18 +241,18 @@ in
targetConfig = mkOption { targetConfig = mkOption {
type = types.lines; type = types.lines;
default = '' default = ''
probe = FPing probe = FPing
menu = Top menu = Top
title = Network Latency Grapher title = Network Latency Grapher
remark = Welcome to the SmokePing website of xxx Company. \ remark = Welcome to the SmokePing website of xxx Company. \
Here you will learn all about the latency of our network. Here you will learn all about the latency of our network.
+ Local + Local
menu = Local menu = Local
title = Local Network title = Local Network
++ LocalMachine ++ LocalMachine
menu = Local Machine menu = Local Machine
title = This host title = This host
host = localhost host = localhost
''; '';
description = "Target configuration"; description = "Target configuration";
}; };

View File

@ -398,6 +398,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 { networking.hostId = mkOption {
default = null; default = null;
example = "4e98920d"; example = "4e98920d";

View File

@ -7,9 +7,12 @@ with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib; with pkgs.lib;
let let
makeHostNameTest = hostName: domain: makeHostNameTest = hostName: domain: fqdnOrNull:
let let
fqdn = hostName + (optionalString (domain != null) ".${domain}"); 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 in
makeTest { makeTest {
name = "hostname-${fqdn}"; name = "hostname-${fqdn}";
@ -26,13 +29,16 @@ let
]; ];
}; };
testScript = '' testScript = { nodes, ... }: ''
start_all() start_all()
machine = ${hostName} machine = ${hostName}
machine.wait_for_unit("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.config.networking.fqdn}" == "${getStr fqdnOrNull}"
# The FQDN, domain name, and hostname detection should work as expected: # The FQDN, domain name, and hostname detection should work as expected:
assert "${fqdn}" == machine.succeed("hostname --fqdn").strip() assert "${fqdn}" == machine.succeed("hostname --fqdn").strip()
assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip() assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip()
@ -60,7 +66,7 @@ let
in in
{ {
noExplicitDomain = makeHostNameTest "ahost" null; noExplicitDomain = makeHostNameTest "ahost" null null;
explicitDomain = makeHostNameTest "ahost" "adomain"; explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain";
} }

View File

@ -8,6 +8,7 @@ import ./make-test-python.nix ({ pkgs, ...} : {
sm = sm =
{ ... }: { ... }:
{ {
networking.domain = "example.com"; # FQDN: sm.example.com
services.smokeping = { services.smokeping = {
enable = true; enable = true;
port = 8081; port = 8081;