diff --git a/nixos/roles/hostmap.nix b/nixos/roles/hostmap.nix index 5240829..dac9fb5 100644 --- a/nixos/roles/hostmap.nix +++ b/nixos/roles/hostmap.nix @@ -1,54 +1,21 @@ -# This data is taken from /etc/hosts from azatoth { - bragi = { - ipv4 = "192.168.1.11"; - }; - tsathoggua = { - ipv4 = "192.168.0.13"; - }; - nyogtha = { - ipv4 = "192.168.0.14"; - }; - hastur = { - ipv4 = "192.168.0.15"; - }; - cthulhu = { - ipv4 = "192.168.0.16"; - }; - nyarlathotep = { - ipv4 = "192.168.0.17"; - }; - nodens = { - ipv4 = "192.168.0.18"; - }; - uvhash = { - ipv4 = "192.168.0.19"; - }; - aphoom-zhah = { - ipv4 = "192.168.0.20"; - }; - dagon = { - ipv4 = "192.168.0.21"; - }; - lobon = { - ipv4 = "192.168.0.22"; - }; - yibb-tstll = { - ipv4 = "192.168.0.23"; - }; - eihort = { - ipv4 = "192.168.0.24"; - }; - ghatanothoa = { - ipv4 = "192.168.0.25"; - }; - toth = { - ipv4 = "192.168.0.26"; - }; - ithaqua = { - ipv4 = "192.168.0.27"; - }; - cthugha = { - ipv4 = "192.168.0.30"; - }; + "192.168.1.11" = ["bragi"]; + "192.168.0.13" = ["tsathoggua"]; + "192.168.0.14" = ["nyogtha"]; + "192.168.0.15" = ["hastur"]; + "192.168.0.16" = ["cthulhu"]; + "192.168.0.17" = ["nyarlathotep"]; + "192.168.0.18" = ["nodens"]; + "192.168.0.19" = ["uvhash"]; + "192.168.0.20" = ["aphoom-zhah"]; + "192.168.0.21" = ["dagon"]; + "192.168.0.22" = ["lobon"]; + "192.168.0.23" = ["yibb-tstll"]; + "192.168.0.24" = ["eihort"]; + "192.168.0.25" = ["ghatanothoa"]; + "192.168.0.26" = ["toth"]; + "192.168.0.27" = ["ithaqua"]; + "192.168.0.28" = ["nyarlathotep"]; + "192.168.0.30" = ["cthugha"]; + "192.168.0.92" = ["sanctamariamaterdei"]; } diff --git a/nixos/roles/vmNetwork.nix b/nixos/roles/vmNetwork.nix index c17db14..e259d56 100644 --- a/nixos/roles/vmNetwork.nix +++ b/nixos/roles/vmNetwork.nix @@ -1,28 +1,26 @@ +# This module assumes that every hostName of a machine in this repo only appears in one of the ip addresses, +# to derive the ip adress based on the hostname this will need patching if we will ever have a vm with multiple +# outward facing network interfaces. +# (other hostnames can appear multiple times) { lib, config, ... }: let - inherit (lib) lists mapAttrsToList; - inherit (lib.attrsets) foldAttrs concatMapAttrs; - inherit (lib.asserts) assertMsg; + inherit (lib) lists; inherit (builtins) elem; + # taken from aza /etc/hosts 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: + myhosts = lib.attrsets.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; + hostmap; + myIp = (lists.findSingle (x: elem myhostName x.value) (throw "The machine has no ip address in nixos/roles/hostmap.nix") (throw "The ip for this machine is not unique in nixos/roles/hostmap.nix") (lib.attrsToList hostmap)).name; in { imports = [ ../modules/vmNetwork.nix