Minimal example does not work on NixOS
Description
I tried running a minimal WGPU example, as well as the hello_triangle example under NixOS. both fails. I have an Nvidia gtx 3050 (driver version 535.113.01) as well as an integrated intel GPU. I tried on my own NixOS setup, as well as on a NixOS fresh install using the gnome / wayland ISO.
Repro steps
- get the NixOS gnome graphical ISO image and boot
- clone the WGPU repo
- try to run the hello_triangle example
Expected vs observed behavior
Expected was the hello_triangle (or my minimal app setup), observed is the following error
Extra materials
The error is the following :
thread 'main' panicked at <my home folder>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-hal-0.18.1/src/gles/egl.rs:751:18:
called `Result::unwrap()` on an `Err` value: BadAlloc
stack backtrace:
0: rust_begin_unwind
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
1: core::panicking::panic_fmt
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14
2: core::result::unwrap_failed
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1652:5
3: core::result::Result<T,E>::unwrap
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1077:23
4: <wgpu_hal::gles::egl::Instance as wgpu_hal::Instance<wgpu_hal::gles::Api>>::init
at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-hal-0.18.1/src/gles/egl.rs:744:31
5: wgpu_core::instance::Instance::new::init
at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.18.1/src/instance.rs:83:32
6: wgpu_core::instance::Instance::new
at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.18.1/src/instance.rs:114:17
7: wgpu_core::global::Global<G>::new
at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.18.1/src/global.rs:36:23
8: <wgpu::backend::direct::Context as wgpu::context::Context>::init
at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.18.0/src/backend/direct.rs:584:14
9: wgpu::Instance::new
at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.18.0/src/lib.rs:1772:32
10: morpheus::main
at ./src/main.rs:12:20
11: core::ops::function::FnOnce::call_once
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
Platform I'm running NixOS unstable, but the same have been experienced on NixOS 23.05 wayland / gnome ditribution. This was done with the latest version of wgpu.
I'm not really sure how to debug this, I know NixOS is generally pretty hard to get graphical stuff working.
Make sure you have the EGL available, the appropriate mesa drivers, etc.
This is probably EGL looking for something it can't find.
I managed to make a shell that expose the libs used by WGPU, it woks so far. I'm putting it here in case anyone needs it in the future :
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
libxkbcommon
libGL
# WINIT_UNIX_BACKEND=wayland
wayland
];
LD_LIBRARY_PATH = "${pkgs.libxkbcommon}/lib:${pkgs.libGL}/lib:${pkgs.wayland}/lib";
RUST_BACKTRACE=1;
}
I'll keep learning WGPU, and I'll update it if there are other needs. Cheers!
It would be worth documenting this in the wiki or on the readme. @cwfitzgerald thoughts?
This works on my current setup, but I thing I'll need to try it again on a plain NixOS install to check it is a sound solution. I can't do it right now, but I can take the time to do proper testing and creating the minimal nix shell for this.
However, I'm still learning NixOS, so I'm trying my best but I'm unsure I am the best person to do this.
Yeah this would be good to document in the wiki once we figure it out!
Struggling with NixOS + wgpu as well. Do you have a good understanding as to why LD_LIBRARY_PATH needs manual intervention?
Also, were you able to get a larger flake.nix that you can share? I'm still working on mine. Seems like this would be a great template for wgpu-based projects.
Can you try forcing X11? There are still plenty of issues with NVIDIA and Wayland, e.g., #4775.
Tangentially related but on an Intel Integrated GPU on NixOS, my problem was not having VK_ICD_FILENAMES set properly, I set it to /run/opengl-driver/share/vulkan/icd.d/intel_icd.x86_64.json which worked.
I tried each of the files in that folder to find the one I needed
currenly it seems to need (on my amdgpu device) also vulkan-loader. I am using this shell.nix:
{ pkgs ? import <nixpkgs> {} }:
let
libPath = with pkgs; lib.makeLibraryPath [
libGL
libxkbcommon
wayland
vulkan-loader
];
in {
devShell = with pkgs; mkShell {
LD_LIBRARY_PATH = libPath;
};
}