forked from Fachschaft/nixConfig
populate ip address based on hostname and populate the hostfile with other vm ips
This commit is contained in:
parent
a6db8ef50f
commit
d5302456bb
7 changed files with 87 additions and 8 deletions
32
nixos/roles/vmNetwork.nix
Normal file
32
nixos/roles/vmNetwork.nix
Normal file
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}: let
|
||||
inherit (lib) lists mapAttrsToList;
|
||||
inherit (lib.attrsets) foldAttrs concatMapAttrs;
|
||||
inherit (lib.asserts) assertMsg;
|
||||
inherit (builtins) elem;
|
||||
hostmap = import ./hostmap.nix;
|
||||
myhostName = config.networking.hostName;
|
||||
# We replace our own ip with 127.0.0.1 in /etc/hosts
|
||||
# To turn the hostmap around suitable for networking.hosts the following simple code almost works
|
||||
# concatMapAttrs (hostname: ipData: { ${ipData.ipv4} = [hostname]; }) hostmap
|
||||
# but breaks as soon as we want to map two different names to the same ip.
|
||||
# So the code looks uglier than one would expect.
|
||||
globalhosts = foldAttrs (a: b: a ++ b) [] (mapAttrsToList (hostname: ipData: {${ipData.ipv4} = [hostname];}) hostmap);
|
||||
myhosts = concatMapAttrs (ip: hosts:
|
||||
if (elem myhostName hosts)
|
||||
# nixos maps the hostname to the loopback 127.0.0.2 by default, so we exclude it here.
|
||||
# there is also a default localhost to 127.0.0.1 in place
|
||||
then {"127.0.0.1" = lists.filter (x: x != myhostName) hosts;}
|
||||
else {${ip} = hosts;})
|
||||
globalhosts;
|
||||
myIp = assert (assertMsg (hostmap ? ${myhostName}.ipv4) "${myhostName} has no ip configured in nixos/roles/hostmap.nix"); hostmap.${myhostName}.ipv4;
|
||||
in {
|
||||
imports = [
|
||||
../modules/vmNetwork.nix
|
||||
];
|
||||
networking.hosts = myhosts;
|
||||
vmNetwork.ipv4 = myIp;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue