diff --git a/lib/runners/qemu.nix b/lib/runners/qemu.nix index 2a5e68ce..9f63425e 100644 --- a/lib/runners/qemu.nix +++ b/lib/runners/qemu.nix @@ -218,17 +218,23 @@ lib.warnIf (mem == 2048) '' "-drive" "id=store,format=raw,read-only=on,file=${storeDisk},if=none,aio=${aioEngine}" "-device" "virtio-blk-${devType},drive=store${lib.optionalString (devType == "pci") ",disable-legacy=on"}" ] ++ - (if graphics.enable - then [ - "-display" "gtk,gl=on" - "-device" "virtio-vga-gl" - "-device" "qemu-xhci" - "-device" "usb-tablet" - "-device" "usb-kbd" - ] - else [ - "-nographic" - ]) ++ + (if graphics.enable then ( + let + displayArgs = { + cocoa = [ + "-display" "cocoa" "-device" "virtio-gpu" + ]; + gtk = [ + "-display" "gtk,gl=on" "-device" "virtio-vga-gl" + ]; + }.${graphics.backend}; + in + displayArgs ++ [ + "-device" "qemu-xhci" + "-device" "usb-tablet" + "-device" "usb-kbd" + ] + ) else [ "-nographic" ]) ++ lib.optionals canSandbox [ "-sandbox" "on" ] ++ diff --git a/nixos-modules/microvm/options.nix b/nixos-modules/microvm/options.nix index 51703802..baa382be 100644 --- a/nixos-modules/microvm/options.nix +++ b/nixos-modules/microvm/options.nix @@ -475,21 +475,31 @@ in ''; }; - graphics.enable = mkOption { - type = types.bool; - default = false; - description = '' - Enable GUI support. - - MicroVMs with graphics are intended for the interactive - use-case. They cannot be started through systemd jobs. - - Support in Hypervisors: - - `qemu` starts a Gtk window with the framebuffer of the virtio-gpu - ''; - }; - - graphics.socket = mkOption { + graphics.enable = mkOption { + type = types.bool; + default = false; + description = '' + Enable GUI support. + + MicroVMs with graphics are intended for the interactive + use-case. They cannot be started through systemd jobs. + + The display backend is chosen by `microvm.graphics.backend`. + ''; + }; + + graphics.backend = mkOption { + type = types.enum [ "gtk" "cocoa" ]; + default = if pkgs.stdenv.hostPlatform.isDarwin then "cocoa" else "gtk"; + defaultText = lib.literalExpression ``if pkgs.stdenv.hostPlatform.isDarwin then "cocoa" else "gtk"``; + description = '' + QEMU display backend to use when `graphics.enable` is true. + + Defaults to `cocoa` on Darwin hosts and `gtk` otherwise. + ''; + }; + + graphics.socket = mkOption { type = types.str; default = "${hostName}-gpu.sock"; description = ''