Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/qemu-vm: add opengl hardware acceleration option #279009

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions nixos/modules/virtualisation/qemu-vm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ in
'';
};

virtualisation.opengl =
mkOption {
type = types.bool;
default = false;
description =
lib.mdDoc ''
Whether or not to run QEMU with OpenGL hardware acceleration.
'';
};

virtualisation.cores =
mkOption {
type = types.ints.positive;
Expand Down Expand Up @@ -1153,12 +1163,17 @@ in
(mkIf cfg.qemu.virtioKeyboard [
"-device virtio-keyboard"
])
(mkIf pkgs.stdenv.hostPlatform.isx86 [
"-usb" "-device usb-tablet,bus=usb-bus.0"
])
(mkIf pkgs.stdenv.hostPlatform.isAarch [
"-device virtio-gpu-pci" "-device usb-ehci,id=usb0" "-device usb-kbd" "-device usb-tablet"
])
(mkIf pkgs.stdenv.hostPlatform.isx86 (mkMerge [
[ "-usb" "-device usb-tablet,bus=usb-bus.0" ]
(mkIf cfg.opengl [ "-vga none" "-device virtio-gpu-gl-pci" ])
# Use software rendering when opengl is required, but hardware acceleration is disabled
(mkIf (!cfg.opengl && config.hardware.opengl.enable) [ "-vga none" "-device virtio-gpu-pci" ])
]))
(mkIf pkgs.stdenv.hostPlatform.isAarch (mkMerge [
[ "-device usb-ehci,id=usb0" "-device usb-kbd" "-device usb-tablet" ]
(mkIf cfg.opengl [ "-device virtio-gpu-gl-pci" ])
(mkIf (!cfg.opengl) [ "-device virtio-gpu-pci" ])
]))
(let
alphaNumericChars = lowerChars ++ upperChars ++ (map toString (range 0 9));
# Replace all non-alphanumeric characters with underscores
Expand All @@ -1175,9 +1190,13 @@ in
(mkIf (cfg.bios != null) [
"-bios ${cfg.bios}/bios.bin"
])
(mkIf (!cfg.graphics) [
"-nographic"
(mkIf cfg.graphics [
"-display default${lib.optionalString cfg.opengl ",gl=on"}"
])
(mkIf (!cfg.graphics) (mkMerge [
[ "-nographic" ]
(mkIf cfg.opengl [ "-display egl-headless" ])
]))
(mkIf (cfg.tpm.enable) [
"-chardev socket,id=chrtpm,path=\"$NIX_SWTPM_DIR\"/socket"
"-tpmdev emulator,id=tpm_dev_0,chardev=chrtpm"
Expand Down
5 changes: 3 additions & 2 deletions nixos/tests/cage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import ./make-test-python.nix ({ pkgs, ...} :
user = "alice";
program = "${pkgs.xterm}/bin/xterm";
};
};

# Need to switch to a different GPU driver than the default one (-vga std) so that Cage can launch:
virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
interactive.nodes.machine = {
virtualisation.opengl = true;
};

enableOCR = true;
Expand Down
5 changes: 3 additions & 2 deletions nixos/tests/cagebreak.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ in
programs.xwayland.enable = true;
security.polkit.enable = true;
environment.systemPackages = [ pkgs.cagebreak pkgs.wayland-utils ];
};

# Need to switch to a different GPU driver than the default one (-vga std) so that Cagebreak can launch:
virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
interactive.nodes.machine = {
virtualisation.opengl = true;
};

enableOCR = true;
Expand Down
5 changes: 4 additions & 1 deletion nixos/tests/gnome-extensions.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ import ./make-test-python.nix (
};
};
};

};

interactive.nodes.machine = {
virtualisation.opengl = true;
};

testScript = { nodes, ... }: let
# Keep line widths somewhat manageable
user = nodes.machine.users.users.alice;
Expand Down
6 changes: 4 additions & 2 deletions nixos/tests/gnome-xorg.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ./make-test-python.nix ({ pkgs, lib, ...} : {
nodes.machine = { nodes, ... }: let
user = nodes.machine.users.users.alice;
in

{ imports = [ ./common/user-account.nix ];

services.xserver.enable = true;
Expand Down Expand Up @@ -39,9 +38,12 @@ import ./make-test-python.nix ({ pkgs, lib, ...} : {
};
};
};

};

interactive.nodes.machine = {
virtualisation.opengl = true;
};

testScript = { nodes, ... }: let
user = nodes.machine.users.users.alice;
uid = toString user.uid;
Expand Down
5 changes: 4 additions & 1 deletion nixos/tests/gnome.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ import ./make-test-python.nix ({ pkgs, lib, ...} : {
};
};
};

};

interactive.nodes.machine = {
virtualisation.opengl = true;
};

testScript = { nodes, ... }: let
# Keep line widths somewhat manageable
user = nodes.machine.users.users.alice;
Expand Down
5 changes: 3 additions & 2 deletions nixos/tests/mate-wayland.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import ./make-test-python.nix ({ pkgs, lib, ... }: {
services.xserver.desktopManager.mate.enableWaylandSession = true;

hardware.pulseaudio.enable = true;
};

# Need to switch to a different GPU driver than the default one (-vga std) so that wayfire can launch:
virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
interactive.nodes.machine = {
virtualisation.opengl = true;
};

enableOCR = true;
Expand Down
4 changes: 4 additions & 0 deletions nixos/tests/mate.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import ./make-test-python.nix ({ pkgs, lib, ... }: {
hardware.pulseaudio.enable = true;
};

interactive.nodes.machine = {
virtualisation.opengl = true;
};

enableOCR = true;

testScript = { nodes, ... }:
Expand Down
14 changes: 5 additions & 9 deletions nixos/tests/phosh.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ./make-test-python.nix ({ pkgs, ...}: let
import ./make-test-python.nix ({ pkgs, ... }: let
pin = "1234";
in {
name = "phosh";
Expand All @@ -25,18 +25,14 @@ in {
};
};

systemd.services.phosh = {
environment = {
# Accelerated graphics fail on phoc 0.20 (wlroots 0.15)
"WLR_RENDERER" = "pixman";
};
};

virtualisation.resolution = { x = 720; y = 1440; };
virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci,xres=720,yres=1440" ];
};
};

interactive.nodes.phone = {
virtualisation.opengl = true;
};

enableOCR = true;

testScript = ''
Expand Down
5 changes: 4 additions & 1 deletion nixos/tests/plasma5.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ./make-test-python.nix ({ pkgs, ...} :
};

nodes.machine = { ... }:

{
imports = [ ./common/user-account.nix ];
services.xserver.enable = true;
Expand All @@ -22,6 +21,10 @@ import ./make-test-python.nix ({ pkgs, ...} :
hardware.pulseaudio.enable = true; # needed for the factl test, /dev/snd/* exists without them but udev doesn't care then
};

interactive.nodes.machine = {
virtualisation.opengl = true;
};

testScript = { nodes, ... }: let
user = nodes.machine.users.users.alice;
xdo = "${pkgs.xdotool}/bin/xdotool";
Expand Down
5 changes: 4 additions & 1 deletion nixos/tests/seatd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ in
'';

hardware.opengl.enable = true;
virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
services.seatd.enable = true;
};

interactive.nodes.machine = {
virtualisation.opengl = true;
};

testScript = ''
machine.wait_for_file("/tmp/foot_started")
machine.succeed("test $(seatd-client-pid) = $(pgrep dwl)")
Expand Down
15 changes: 4 additions & 11 deletions nixos/tests/sway.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ import ./make-test-python.nix ({ pkgs, lib, ... }: {
# For glinfo and wayland-info:
systemPackages = with pkgs; [ mesa-demos wayland-utils alacritty ];
# Use a fixed SWAYSOCK path (for swaymsg):
variables = {
"SWAYSOCK" = "/tmp/sway-ipc.sock";
# TODO: Investigate if we can get hardware acceleration to work (via
# virtio-gpu and Virgil). We currently have to use the Pixman software
# renderer since the GLES2 renderer doesn't work inside the VM (even
# with WLR_RENDERER_ALLOW_SOFTWARE):
# "WLR_RENDERER_ALLOW_SOFTWARE" = "1";
"WLR_RENDERER" = "pixman";
};
variables."SWAYSOCK" = "/tmp/sway-ipc.sock";
# For convenience:
shellAliases = {
test-x11 = "glinfo | tee /tmp/test-x11.out && touch /tmp/test-x11-exit-ok";
Expand Down Expand Up @@ -70,9 +62,10 @@ import ./make-test-python.nix ({ pkgs, lib, ... }: {

# To test pinentry via gpg-agent:
programs.gnupg.agent.enable = true;
};

# Need to switch to a different GPU driver than the default one (-vga std) so that Sway can launch:
virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
interactive.nodes.machine = {
virtualisation.opengl = true;
};

testScript = { nodes, ... }: ''
Expand Down
5 changes: 3 additions & 2 deletions nixos/tests/tinywl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import ./make-test-python.nix ({ pkgs, lib, ... }:
touch /tmp/tinywl-exit-ok
fi
'';
};

# Switch to a different GPU driver (default: -vga std), otherwise TinyWL segfaults:
virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
interactive.nodes.machine = {
virtualisation.opengl = true;
};

testScript = { nodes, ... }: ''
Expand Down