add guest network (now with custom mac address generators!)

Signed-off-by: ahuston-0 <aliceghuston@gmail.com>
This commit is contained in:
ahuston-0 2024-05-30 23:39:31 -04:00
parent cc702cb5fe
commit d29b2ecbab
No known key found for this signature in database
GPG Key ID: 1FACF4075E3212F7
4 changed files with 74 additions and 27 deletions

View File

@ -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);
};
}

View File

@ -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 = {

View File

@ -38,7 +38,10 @@ in
};
networks = {
"30-ztkubnet" = {
matchConfig.Name = "ztkubnet";
matchConfig.Name = [
"ztkubnet"
"vm-*"
];
networkConfig.Bridge = "brkubnet";
linkConfig.RequiredForOnline = "enslaved";
};

View File

@ -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;
};