How to run QEMU/KVM with the GPU on an Optimus laptop

Hello, I’m trying to run QEMU or KVM (or both, not really sure how any of this works) with the GPU.

I’m on an Optimus laptop (intel CPU + nvidia GPU), which uses the intel iGPU by default, and I normally provide a couple of environment variables to the things I want to run with the nvidia GPU. For example, I run steam with __NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME="nvidia" __VK_LAYER_NV_optimus="NVIDIA_only".

Issue is, I have no idea what to run with those variables. I run virt-manager, but running the manager on the GPU is not interesting, I as I want the virtual machines to be run with the GPU.

What could I try ? I imagine I could maybe try to run libvirt with the environment variables, but systemd takes care of that, so I don’t know what I could change for that.

Thanks for any help ^^

You may be interested in QEMU/Guest graphics acceleration - ArchWiki.

I’ve read through that, with the current configuration I have, I can’t do any of the passthrough or similar methods. I’m using the QXL/Spice method, but it runs on the CPU, not on the GPU. I don’t know how I would run it on the GPU though, where I should put the environment variables I mention.