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: # type:
# fileList :: Path -> String -> [Path] # fileList :: Path -> String -> [Path]
fileList = dir: map (file: dir + "/${file}") (ls dir); 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 ( lib.mapAttrs (
host: host:
{ {
ipv4, address,
gateway,
machine-id, machine-id,
server ? false, 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; ) vms;
genMicroVM = genMicroVM =
hostName: ipv4: _system: machine-id: vm-config: hostName: address: gateway: _system: machine-id: vm-config:
# microvm refers to microvm.nixosModules # microvm refers to microvm.nixosModules
# { # {
@ -44,7 +47,18 @@ rec {
text = machine-id + "\n"; text = machine-id + "\n";
}; };
microvm.shares = [ networking.hostName = hostName;
microvm = {
interfaces = [
{
type = "tap";
# bridge = "ztkubnet";
id = "vm-${hostName}";
mac = lib.rad-dev.strToMac hostName;
}
];
shares = [
{ {
source = "/nix/store"; source = "/nix/store";
mountPoint = "/nix/.ro-store"; mountPoint = "/nix/.ro-store";
@ -61,15 +75,19 @@ rec {
socket = "journal.sock"; socket = "journal.sock";
} }
]; ];
};
networking = { systemd.network.enable = true;
inherit hostName;
interfaces.ether.ipv4.addresses = [ systemd.network.networks."20-lan" = {
{ matchConfig.Type = "ether";
address = ipv4; networkConfig = {
prefixLength = 32; Address = address;
} Gateway = gateway;
]; DNS = [ "9.9.9.9" ];
IPv6AcceptRA = true;
DHCP = "no";
};
}; };
services.openssh = { services.openssh = {

View File

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

View File

@ -15,7 +15,8 @@ in
genK3SVM (src + "/modules/opt/k3s-server.nix") (src + "/modules/opt/k3s-agent.nix") genK3SVM (src + "/modules/opt/k3s-server.nix") (src + "/modules/opt/k3s-agent.nix")
{ {
"ph-server-1" = { "ph-server-1" = {
ipv4 = "192.168.69.10"; address = [ "192.168.69.10/24" ];
gateway = "192.168.69.1";
machine-id = "d694ad1e88b356887bb204ac665263f7"; machine-id = "d694ad1e88b356887bb204ac665263f7";
server = true; server = true;
}; };