From 24670100f5e9ce66c679cdbb90bcfd5e295156dc Mon Sep 17 00:00:00 2001 From: Dennis Frieberg Date: Sun, 31 Mar 2024 21:47:33 +0200 Subject: [PATCH] Refactored existing network config --- nixos/machines/ghatanothoa/configuration.nix | 3 +- nixos/machines/ghatanothoa/network.nix | 16 ------- nixos/modules/vmNetwork.nix | 47 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 17 deletions(-) delete mode 100644 nixos/machines/ghatanothoa/network.nix create mode 100644 nixos/modules/vmNetwork.nix diff --git a/nixos/machines/ghatanothoa/configuration.nix b/nixos/machines/ghatanothoa/configuration.nix index 207e37f..8364bea 100644 --- a/nixos/machines/ghatanothoa/configuration.nix +++ b/nixos/machines/ghatanothoa/configuration.nix @@ -4,7 +4,7 @@ ../../modules/jitsi.nix ../../roles ../../roles/vm.nix - ./network.nix + ../../modules/vmNetwork.nix ]; services.mathebau-jitsi = { @@ -14,5 +14,6 @@ # System configuration here networking.hostName = "ghatanothoa"; + vmNetwork.ipv4 = "192.168.0.25"; system.stateVersion = "23.11"; } diff --git a/nixos/machines/ghatanothoa/network.nix b/nixos/machines/ghatanothoa/network.nix deleted file mode 100644 index 2a1f4ae..0000000 --- a/nixos/machines/ghatanothoa/network.nix +++ /dev/null @@ -1,16 +0,0 @@ -# We sohuld put that config somewhere in roles and give it a parameter or something, -# everyone gets the same nameserver and the same prefixLength and address vs defaultGateway alsways -# depend on the same thing -{ - imports = []; - networking = { - interfaces.enX0.ipv4.addresses = [ - { - address = "192.168.0.25"; - prefixLength = 16; - } - ]; - defaultGateway = "192.168.0.152"; - nameservers = ["130.83.2.22" "130.83.56.60" "130.83.22.60" "130.82.22.63"]; - }; -} diff --git a/nixos/modules/vmNetwork.nix b/nixos/modules/vmNetwork.nix new file mode 100644 index 0000000..4084ea8 --- /dev/null +++ b/nixos/modules/vmNetwork.nix @@ -0,0 +1,47 @@ +{ + lib, + config, + ... +}: let + inherit + (lib) + mkOption + types + last + init + ; + inherit + (lib.strings) + splitString + concatStringsSep + toInt + ; + cfg = config.vmNetwork; +in { + imports = []; + + options.vmNetwork = { + ipv4 = mkOption { + type = types.str; + description = "the ipv4 adress of this machine"; + }; + }; + + config = { + networking = { + interfaces.enX0.ipv4.addresses = [ + { + address = cfg.ipv4; + prefixLength = 16; + } + ]; + defaultGateway = let + addr = splitString "." cfg.ipv4; + addrInit = init addr; + addrLastInt = builtins.toString (toInt (last addr) + 127); + in + concatStringsSep "." (addrInit ++ [addrLastInt]); + nameservers = ["130.83.2.22" "130.83.56.60" "130.83.22.60" "130.82.22.63"]; + }; + }; +}