From 2faffd92fc81a6b850ac5df108e5c74b8e9c2551 Mon Sep 17 00:00:00 2001 From: Peter Esselius Date: Sun, 5 Oct 2025 16:22:14 +0200 Subject: [PATCH 1/2] Set qemu gui backend to cocoa on darwin --- lib/runners/qemu.nix | 28 +++++++++++++--------- nixos-modules/microvm/options.nix | 39 +++++++++++++++++++------------ 2 files changed, 41 insertions(+), 26 deletions(-) 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..73330e39 100644 --- a/nixos-modules/microvm/options.nix +++ b/nixos-modules/microvm/options.nix @@ -475,21 +475,30 @@ 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"; + 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 = '' From 964848b15a601a8cd297d9ca4805f05aeddfc39b Mon Sep 17 00:00:00 2001 From: Peter Esselius Date: Wed, 22 Oct 2025 23:48:14 +0200 Subject: [PATCH 2/2] Add defaultText for microvm.graphics.backend option Co-authored-by: Sandro --- nixos-modules/microvm/options.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nixos-modules/microvm/options.nix b/nixos-modules/microvm/options.nix index 73330e39..baa382be 100644 --- a/nixos-modules/microvm/options.nix +++ b/nixos-modules/microvm/options.nix @@ -491,6 +491,7 @@ in 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.