add ip and mac arithmatic functions

calculate vm mac addresses
This commit is contained in:
Pim Kunis 2024-02-01 22:57:18 +01:00
parent beb1c384ec
commit 885cba2f30
4 changed files with 1344 additions and 6 deletions

View file

@ -94,5 +94,9 @@
minicom minicom
socat socat
]; ];
nixpkgs.overlays = [
(final: prev: { lib = prev.lib // (import ./net.nix prev); })
];
}; };
} }

View file

@ -64,7 +64,7 @@
nixosModule = { pkgs, ... }: { nixosModule = { pkgs, ... }: {
# TODO: would be cool to create a check that a mac address is only ever assigned to one VM. # TODO: would be cool to create a check that a mac address is only ever assigned to one VM.
lab.vm.macAddress = "BA:DB:EE:F0:00:00"; lab.vm.id = 0;
programs.bash.interactiveShellInit = '' programs.bash.interactiveShellInit = ''
echo "Hello world from inside a virtual machine!!" | ${pkgs.lolcat}/bin/lolcat echo "Hello world from inside a virtual machine!!" | ${pkgs.lolcat}/bin/lolcat
@ -81,7 +81,7 @@
networking.dmz.services.enable = true; networking.dmz.services.enable = true;
vm = { vm = {
macAddress = "BA:DB:EE:F0:00:07"; id = 7;
staticNetworking = true; staticNetworking = true;
staticIPv4 = config.lab.networking.dmz.ipv4.services; staticIPv4 = config.lab.networking.dmz.ipv4.services;
staticIPv6 = config.lab.networking.dmz.ipv6.services; staticIPv6 = config.lab.networking.dmz.ipv6.services;

1326
nixos/net.nix Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,17 @@
{ lib, config, hypervisorConfig, ... }: { { pkgs, lib, config, hypervisorConfig, ... }: {
options.lab.vm = { options.lab.vm = {
macAddress = lib.mkOption { baseMACAddress = lib.mkOption {
default = "BA:DB:EE:F0:00:00";
type = lib.types.str; type = lib.types.str;
description = '' description = ''
The MAC address of the VM's main NIC. Base MAC address for VMs in the DMZ.
'';
};
id = lib.mkOption {
type = lib.types.int;
description = ''
Unique identifier of this VM from wich the MAC address is derived.
''; '';
}; };
@ -45,7 +53,7 @@
interfaces = [{ interfaces = [{
type = "tap"; type = "tap";
id = "vm-${config.networking.hostName}"; id = "vm-${config.networking.hostName}";
mac = config.lab.vm.macAddress; mac = pkgs.lib.net.mac.add config.lab.vm.id config.lab.vm.baseMACAddress;
}]; }];
}; };