From 5563d1be460f5f1d6a6395708e0e49560df34db7 Mon Sep 17 00:00:00 2001 From: Jack O'Sullivan Date: Thu, 12 May 2022 21:06:46 +0100 Subject: [PATCH] nixos/vms: Add UUID and MAC address options --- devshell/commands.nix | 6 ++++++ nixos/boxes/colony.nix | 3 ++- nixos/modules/vms.nix | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/devshell/commands.nix b/devshell/commands.nix index fc069f3..1284185 100644 --- a/devshell/commands.nix +++ b/devshell/commands.nix @@ -42,6 +42,12 @@ in help = pkgs.agenix.meta.description; command = ''exec ${pkgs.agenix}/bin/agenix --identity "$PRJ_ROOT/.keys/dev.key" "$@"''; } + { + name = "qemu-genmac"; + category = "utilities"; + help = "Generate MAC address suitable for QEMU"; + command = ''printf "52:54:00:ab:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256))''; + } { name = "fmt"; diff --git a/nixos/boxes/colony.nix b/nixos/boxes/colony.nix index 59f483b..3be6339 100644 --- a/nixos/boxes/colony.nix +++ b/nixos/boxes/colony.nix @@ -39,7 +39,8 @@ }; vms = { instances.test = { - networks.virtual = {}; + uuid = "59f51efb-7e6d-477b-a263-ed9620dbc87b"; + networks.virtual.mac = "52:54:00:ab:f1:52"; drives = { disk = { backend = { diff --git a/nixos/modules/vms.nix b/nixos/modules/vms.nix index 71c6c08..d375b47 100644 --- a/nixos/modules/vms.nix +++ b/nixos/modules/vms.nix @@ -44,6 +44,7 @@ let options = { bridge = mkOpt' str name "Network bridge to connect to."; model = mkOpt' str "virtio-net" "Device type for network interface."; + mac = mkOpt' str null "Guest MAC address."; extraOptions = mkOpt' qemuOpts { } "Extra QEMU options to set for the NIC."; }; }; @@ -70,6 +71,7 @@ let timeout = mkOpt' ints.unsigned 30 "Clean shutdown timeout (in seconds)."; }; + uuid = mkOpt' str null "QEMU machine UUID."; machine = mkOpt' str "q35" "QEMU machine type."; enableKVM = mkBoolOpt' true "Whether to enable KVM."; enableUEFI = mkBoolOpt' true "Whether to enable UEFI."; @@ -92,6 +94,7 @@ let i.qemuFlags ++ [ "name ${n}" + "uuid ${i.uuid}" "machine ${i.machine}" "cpu ${i.cpu}" "smp cpus=${toString i.smp.cpus},threads=${toString i.smp.threads}" @@ -113,7 +116,7 @@ let (optional i.spice.enable "spice unix=on,addr=/run/vms/${n}/spice.sock,disable-ticketing=on") ++ (flatten (mapAttrsToList (nn: c: [ "netdev bridge,id=${nn},br=${c.bridge}" - ("device ${c.model},netdev=${nn}" + (extraQEMUOpts c.extraOptions)) + ("device ${c.model},netdev=${nn},mac=${c.mac}" + (extraQEMUOpts c.extraOptions)) ]) i.networks)) ++ (flatten (mapAttrsToList (dn: c: [ "blockdev node-name=${dn}-backend,${c.backend}"