diff --git a/lib/default.nix b/lib/default.nix index 430a6a3..09a95a3 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -57,5 +57,30 @@ # type: # fileList :: Path -> String -> [Path] fileList = dir: map (file: dir + "/${file}") (ls dir); + + # constructs a mac address from a string's hash + # + # args: + # hashable: the string to hash + # + # type: + # strToMac :: String -> String + strToMac = + hashable: + let + # computes sha512 hash of input + hashStr = builtins.hashString "sha512" hashable; + # grabs first 12 letters of hash + hashSub = start: builtins.substring start 2 (builtins.substring 0 12 hashStr); + # joins list of strings with a delimiter between + joiner = + delim: arr: + builtins.foldl' ( + a: b: lib.concatStrings ([ a ] ++ (lib.optionals (a != "") [ delim ]) ++ [ b ]) + ) "" arr; + # generates a list of indexes for the hash + starts = builtins.genList (x: x * 2) 6; + in + joiner ":" (map hashSub starts); }; } diff --git a/lib/microvms.nix b/lib/microvms.nix index 4fcf46d..a3d5552 100644 --- a/lib/microvms.nix +++ b/lib/microvms.nix @@ -5,15 +5,18 @@ rec { lib.mapAttrs ( host: { - ipv4, + address, + gateway, machine-id, server ? false, }: - genMicroVM host ipv4 "x86_64-linux" machine-id (if server then server-config else agent-config) + genMicroVM host address gateway "x86_64-linux" machine-id ( + if server then server-config else agent-config + ) ) vms; genMicroVM = - hostName: ipv4: _system: machine-id: vm-config: + hostName: address: gateway: _system: machine-id: vm-config: # microvm refers to microvm.nixosModules # { @@ -44,32 +47,47 @@ rec { text = machine-id + "\n"; }; - microvm.shares = [ - { - source = "/nix/store"; - mountPoint = "/nix/.ro-store"; - tag = "ro-store"; - proto = "virtiofs"; - } - { - # On the host - source = "/var/lib/microvms/${hostName}/journal"; - # In the MicroVM - mountPoint = "/var/log/journal"; - tag = "journal"; - proto = "virtiofs"; - socket = "journal.sock"; - } - ]; + networking.hostName = hostName; - networking = { - inherit hostName; - interfaces.ether.ipv4.addresses = [ + microvm = { + interfaces = [ { - address = ipv4; - prefixLength = 32; + type = "tap"; + # bridge = "ztkubnet"; + id = "vm-${hostName}"; + mac = lib.rad-dev.strToMac hostName; } ]; + shares = [ + { + source = "/nix/store"; + mountPoint = "/nix/.ro-store"; + tag = "ro-store"; + proto = "virtiofs"; + } + { + # On the host + source = "/var/lib/microvms/${hostName}/journal"; + # In the MicroVM + mountPoint = "/var/log/journal"; + tag = "journal"; + proto = "virtiofs"; + socket = "journal.sock"; + } + ]; + }; + + systemd.network.enable = true; + + systemd.network.networks."20-lan" = { + matchConfig.Type = "ether"; + networkConfig = { + Address = address; + Gateway = gateway; + DNS = [ "9.9.9.9" ]; + IPv6AcceptRA = true; + DHCP = "no"; + }; }; services.openssh = { diff --git a/modules/kub_net.nix b/modules/kub_net.nix index e500bc8..82de679 100644 --- a/modules/kub_net.nix +++ b/modules/kub_net.nix @@ -38,7 +38,10 @@ in }; networks = { "30-ztkubnet" = { - matchConfig.Name = "ztkubnet"; + matchConfig.Name = [ + "ztkubnet" + "vm-*" + ]; networkConfig.Bridge = "brkubnet"; linkConfig.RequiredForOnline = "enslaved"; }; diff --git a/systems/palatine-hill/microvms.nix b/systems/palatine-hill/microvms.nix index a1ad488..e8298e1 100644 --- a/systems/palatine-hill/microvms.nix +++ b/systems/palatine-hill/microvms.nix @@ -15,7 +15,8 @@ in genK3SVM (src + "/modules/opt/k3s-server.nix") (src + "/modules/opt/k3s-agent.nix") { "ph-server-1" = { - ipv4 = "192.168.69.10"; + address = [ "192.168.69.10/24" ]; + gateway = "192.168.69.1"; machine-id = "d694ad1e88b356887bb204ac665263f7"; server = true; };