From 4d1d37a1ed5593470045f74778a1a2a30538d4b2 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Mon, 12 Apr 2021 20:45:47 +0200 Subject: [PATCH] nixos: set QEMU network options directly Configure QEMU devices as drivers are configuration. --- nixos-modules/qemu-vm.nix | 62 +++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/nixos-modules/qemu-vm.nix b/nixos-modules/qemu-vm.nix index f08a2a3..ce364f1 100644 --- a/nixos-modules/qemu-vm.nix +++ b/nixos-modules/qemu-vm.nix @@ -48,7 +48,6 @@ let -name ${config.system.name} \ -m ${toString config.virtualisation.memorySize} \ -smp ${toString config.virtualisation.cores} \ - ${concatStringsSep " " config.virtualisation.qemu.networkingOptions} \ ${toString config.virtualisation.qemu.options} \ ${ if config.hardware.genode.usb.storage.enable then @@ -128,7 +127,7 @@ in { }; virtualisation.vlans = mkOption { - default = [ 1 ]; + default = [ ]; example = [ 1 2 ]; description = '' Virtual networks to which the VM is connected. Each @@ -189,20 +188,42 @@ in { ''; }; - networkingOptions = mkOption { - default = [ - "-net nic,netdev=user.0,model=virtio" - "-netdev user,id=user.0\${QEMU_NET_OPTS:+,$QEMU_NET_OPTS}" - ]; - type = types.listOf types.str; - description = '' - Networking-related command-line options that should be passed to qemu. - The default is to use userspace networking (slirp). - - If you override this option, be advised to keep - ''${QEMU_NET_OPTS:+,$QEMU_NET_OPTS} (as seen in the default) - to keep the default runtime behaviour. - ''; + nics = mkOption { + description = "QEMU network devices."; + default = { }; + type = with lib.types; + attrsOf (submodule { + options = { + netdev = mkOption { + type = submodule { + options = { + kind = mkOption { + type = str; + default = "user"; + }; + settings = mkOption { + type = attrsOf str; + default = { }; + }; + }; + }; + }; + device = mkOption { + type = submodule { + options = { + kind = mkOption { + type = str; + default = "virtio-net-pci"; + }; + settings = mkOption { + type = attrsOf str; + default = { }; + }; + }; + }; + }; + }; + }); }; diskInterface = mkOption { @@ -280,6 +301,15 @@ in { ]) (mkIf (cfg.bios != null) [ "-bios ${cfg.bios}/bios.bin" ]) (mkIf (!cfg.graphics) [ "-nographic" ]) + (let + toFlags = bind: + { kind, settings }: + lib.strings.concatStringsSep "," ([ "${kind},${bind}" ] + ++ (lib.attrsets.mapAttrsToList (k: v: "${k}=${v}") settings)); + in lib.attrsets.mapAttrsToList (id: nic: [ + "-netdev ${toFlags "id=${id}" nic.netdev}" + "-device ${toFlags "netdev=${id}" nic.device}" + ]) cfg.qemu.nics) ]; system.build.vm = pkgs.runCommand "nixos-vm" { preferLocalBuild = true; } ''