Replace mapHqHosts and mapPublicHosts with mergeHostsFile

This commit is contained in:
Ehmry - 2022-01-18 23:07:39 +01:00
parent 539cfb8d3b
commit 06f9033bdb
6 changed files with 29 additions and 49 deletions

View File

@ -187,7 +187,7 @@ in {
c3d2 = { c3d2 = {
isInHq = false; # not in HQ, this is the default. isInHq = false; # not in HQ, this is the default.
mapHqHosts = true; # Make entries in /etc/hosts for *.hq internal addresses. mergeHostsFile = true; # Make entries in /etc/hosts form hosts.nix
enableMotd = true; # Set the login shell message to the <<</>> logo. enableMotd = true; # Set the login shell message to the <<</>> logo.
}; };

View File

@ -41,12 +41,12 @@ in {
message = "Root passwords not allowed in HQ"; message = "Root passwords not allowed in HQ";
} }
{ {
assertion = cfg.hq.enableBinaryCache -> cfg.mapHqHosts; assertion = cfg.hq.enableBinaryCache -> cfg.mergeHostsFile;
message = "mapHqHosts must be enabled for enableBinaryCache"; message = "mergeHostsFile must be enabled for enableBinaryCache";
} }
{ {
assertion = cfg.hq.enableMpdProxy -> cfg.mapHqHosts; assertion = cfg.hq.enableMpdProxy -> cfg.mergeHostsFile;
message = "mapHqHosts must be enabled for enableMpdProxy"; message = "mergeHostsFile must be enabled for enableMpdProxy";
} }
{ {
assertion = cfg.isInHq -> builtins.hasAttr config.networking.hostName cfg.hosts; assertion = cfg.isInHq -> builtins.hasAttr config.networking.hostName cfg.hosts;
@ -96,29 +96,6 @@ in {
networking.domain = mkIfIsInHq "hq.c3d2.de"; networking.domain = mkIfIsInHq "hq.c3d2.de";
networking.hosts = let
getHost = hostName: builtins.getAttr hostName cfg.hosts;
hqLocalHosts = with builtins;
let
f = hostName:
let
host = getHost hostName;
ip6 = if host.ip6 != null then
host.ip6
else
toHqPrivateAddress hostName;
in [{
name = ip6;
value = [ "${hostName}.hq" hostName ];
}] ++ lib.optional (host.ip4 != null) {
name = host.ip4;
value = [ "${hostName}.hq" hostName ];
};
in listToAttrs (concatLists (map f (attrNames cfg.hosts)));
in if cfg.mapHqHosts then hqLocalHosts else { };
systemd.network.networks = systemd.network.networks =
if cfg.hq.interface != null && config.networking.useNetworkd if cfg.hq.interface != null && config.networking.useNetworkd
then { then {
@ -230,7 +207,7 @@ in {
services.mpd.extraConfig = lib.mkIf cfg.hq.enableMpdProxy '' services.mpd.extraConfig = lib.mkIf cfg.hq.enableMpdProxy ''
database { database {
plugin "proxy" plugin "proxy"
host "mpd-index.hq" host "mpd-index.c3d2"
} }
''; '';

View File

@ -68,7 +68,7 @@
c3d2 = { c3d2 = {
isInHq = true; isInHq = true;
mapHqHosts = true; mergeHostsFile = true;
hq.interface = "eth0"; hq.interface = "eth0";
hq.statistics.enable = true; hq.statistics.enable = true;
}; };

View File

@ -35,7 +35,7 @@ in
c3d2 = { c3d2 = {
isInHq = true; isInHq = true;
mapHqHosts = true; mergeHostsFile = true;
hq.interface = "eth0"; hq.interface = "eth0";
hq.statistics.enable = true; hq.statistics.enable = true;
}; };

View File

@ -15,7 +15,7 @@ in
c3d2 = { c3d2 = {
k-ot.enable = true; k-ot.enable = true;
isInHq = true; isInHq = true;
mapHqHosts = true; mergeHostsFile = true;
hq.interface = eth0; hq.interface = eth0;
mountCeph = "/mnt/cephfs"; mountCeph = "/mnt/cephfs";
}; };

View File

@ -48,19 +48,11 @@ in
defaultText = literalExample "config.c3d2.isInHq"; defaultText = literalExample "config.c3d2.isInHq";
}; };
mapPublicHosts = mkOption { mergeHostsFile = mkOption {
type = bool;
default = false;
description = ''
Whether to add all external HQ host mappings to /etc/hosts.
'';
};
mapHqHosts = mkOption {
type = bool; type = bool;
default = cfg.isInHq; default = cfg.isInHq;
description = '' description = ''
Whether to add all internal HQ host mappings to /etc/hosts. Whether to add <literal>c3d2.hosts</literal> to /etc/hosts.
''; '';
}; };
@ -141,14 +133,16 @@ in
users = users =
mkOption { mkOption {
type = attrsOf (submodule { type = attrsOf
options = { (submodule {
sshKeys = mkOption { options = {
type = listOf types.str; sshKeys = mkOption {
default = [ ]; type = with types;
listOf str;
default = [ ];
};
}; };
}; });
});
}; };
}; };
@ -161,6 +155,15 @@ in
)); ));
in in
{ {
networking.hosts = lib.mkIf cfg.mergeHostsFile
((
lib.attrsets.mapAttrs' (n: v: { name = v.ip4; value = [ "${n}.c3d2" ]; })
(lib.attrsets.filterAttrs (n: v: v.ip4 != null) cfg.hosts)
) // (
lib.attrsets.mapAttrs' (n: v: { name = v.ip6; value = [ "${n}.c3d2" ]; })
(lib.attrsets.filterAttrs (n: v: v.ip6 != null) cfg.hosts)
));
programs.nncp.settings = lib.mkIf cfg.mergeNncpSettings cfg.nncp; programs.nncp.settings = lib.mkIf cfg.mergeNncpSettings cfg.nncp;
users.motd = lib.mkIf cfg.enableMotd (builtins.readFile ./motd); users.motd = lib.mkIf cfg.enableMotd (builtins.readFile ./motd);