Getting wgpu and winit to work on NixOs

I've played around with graphics programming off and on for a while without ever getting too far (but always having fun!) The itch presented itself once again so off I went to scratch it.

I decided to give wgpu a spin. I ran into issues trying to get the hello_triangle example to work on NixOS and noticed that others also run into this issue, so I figured it'd be worth writing down.

The first problem I encountered is illustrated below:
called `Result::unwrap()` on an `Err` value: Os(OsError { line: 80, file: "/home/stefan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.11/src/platform_impl/linux/wayland/event_loop/mod.rs", error: WaylandError(Connection(NoWaylandLib)) })
After creating a nix-shell with the wayland build depenency then a second, related, error occurs:
thread 'main' panicked at /home/stefan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.19.0/src/instance.rs:521:39: called `Option::unwrap()` on a `None` value
Finally, we have another error:
thread 'main' panicked at /home/stefan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.11/src/platform_impl/linux/wayland/seat/keyboard/mod.rs:315:41: called `Result::unwrap()` on an `Err` value: XKBNotFound

I was able to finally display the glorious triangle with the following nix-shell:
{ pkgs ? import {} }: let libPath = with pkgs; lib.makeLibraryPath [ libGL libxkbcommon wayland ]; in { devShell = with pkgs; mkShell { buildInputs = [ cargo rustc rust-analyzer ]; RUST_LOG = "debug"; RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; LD_LIBRARY_PATH = libPath; }; }

So why does this happen at all? Why do we need to do this? It seems like the problem is that some programs try to dynamically open a shared library (dlopen), but can't find it on NixOS. We need to set LD_LIBRARY_PATH or use the patchelf utility.