From d406be44b98f041b0de03ff7aa2c35c0baac63c0 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Fri, 17 Oct 2025 18:15:20 +0200 Subject: [PATCH] nixos/qemu-vm: add `enableSharedMemory` option This option configures a memfd backend for the VM's memory with the size of `virtualisation.memorySize` and uses that as default memory backend. This is required for e.g. vhost-device-vsock. The motivation for making this an option is that I decided to enable this by default for all NixOS tests to avoid changing essential QEMU options based on whether or not debugging is enabled. However, there should be an easy way to turn it off which is what this option provides. --- nixos/modules/virtualisation/qemu-vm.nix | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index 33bd759a1e3f..0a2646a3364e 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -716,6 +716,8 @@ in }; virtualisation.qemu = { + enableSharedMemory = mkEnableOption "shared memory"; + package = mkOption { type = types.package; default = @@ -1338,6 +1340,10 @@ in "-device usb-kbd" "-device usb-tablet" ]) + (mkIf cfg.qemu.enableSharedMemory [ + "-object memory-backend-memfd,id=mem0,size=${toString config.virtualisation.memorySize}M,share=on" + "-machine memory-backend=mem0" + ]) ( let alphaNumericChars = lowerChars ++ upperChars ++ (map toString (range 0 9));