Nixos GL options for application start # intel vs nvidia

how to start applications on nixos with e.g. intel egpu instead of nvidia gpu?


On a tower with nivia gpu it is not working (e.g. for rambox, zoom, sweet home 3d, …)
but e.g. blender cpu and gpu examples are working …


for rambox
[2:0617/135029.677237:FATAL:gpu_data_manager_impl_private.cc(445)] GPU process isn't usable. Goodbye.


traps: Chrome_IOThread[98952] trap int3 ip:55b893a87ae9 sp:7f844198f420 error:0 in rambox[55b891424000+6066000]

keeweb

(node:2) UnhandledPromiseRejectionWarning: Error: The name org.freedesktop.secrets was not provided by any .service files
(Use `keeweb --trace-warnings ...` to show where the warning was created)
(node:2) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To termin
ate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
libva error: /run/opengl-driver/lib/dri/nvidia_drv_video.so init failed

sweethome3d

com.jogamp.opengl.GLException: J3D-Renderer-1: Error making temp context(1) current: display 0x7f6bd8213de0, context 0x7f6bd801ec08, drawable X11OnscreenGLXDrawable[Realized true,
        Factory   jogamp.opengl.x11.glx.X11GLXDrawableFactory@4a9360b4,
        Handle    0x3400148,
        Surface   JAWTWindow[0x44eb943e][JVM version: 1.8.0_322 (1.8.0 update 322)
JAWT version: 0x10004, CA_LAYER: false, isLayeredSurface false, bounds [ 0 / 0  765 x 622 ], insets [ l 0, r 0 - t 0, b 0 - 0x0], pixelScale 1.0x1.0, shallUseOffscreenLayer false, isOffscreenLayerSurface false, attachedSurfaceLayer 0x0, windowHandle 0x3400148, surfaceHandle 0x3400148, bounds [ 0 / 0  765 x 622 ], insets [ l 0, r 0 - t 0, b 0 - 0x0], window [0/0 765x622], pixels[scale 1.0, 1.0 -> 765x622], visible true, lockedExt false,
        config AWTGraphicsConfiguration[AWTGraphicsScreen[AWTGraphicsDevice[type .awt, connection :0.0, unitID 0, awtDevice X11GraphicsDevice[screen=0], handle 0x0], idx 0],
        chosen    GLCaps[glx vid 0x28, fbc 0xdf: rgba 8/8/8/8, opaque, accum-rgba 16/16/16/16, dp/st/ms 24/0/0, dbl, mono  , hw, GLProfile[GL4bc/GL4bc.hw], on-scr[.]],
        requested GLCaps[rgba 5/5/5/1, opaque, accum-rgba 0/0/0/0, dp/st/ms 24/0/0, dbl, mono  , hw, GLProfile[GL4bc/GL4bc.hw], on-scr[.]],
        GLXGraphicsConfig[dev=X11GraphicsDevice[screen=0],vis=0x28],
        encapsulated X11GLXGraphicsConfiguration[X11GraphicsScreen[X11GraphicsDevice[type .x11, connection :0, unitID 0, handle 0x7f6bd8213de0, owner true, JAWTToolkitLock[obj 0x4e070203, isOwner true, <71cc0f13, 200b575e>[count 1, qsz 0, owner <J3D-Renderer-1>]]], idx 0], visualID 0x28, fbConfigID 0xdf,
        requested GLCaps[rgba 5/5/5/1, opaque, accum-rgba 0/0/0/0, dp/st/ms 24/0/0, dbl, mono  , hw, GLProfile[GL4bc/GL4bc.hw], on-scr[.]],
        chosen    GLCaps[glx vid 0x28, fbc 0xdf: rgba 8/8/8/8, opaque, accum-rgba 16/16/16/16, dp/st/ms 24/0/0, dbl, mono  , hw, GLProfile[GL4bc/GL4bc.hw], on-scr[.]]]],
        awtComponent com.eteks.sweethome3d.j3d.Component3DManager$ObservedCanvas3D[canvas2,0,0,765x622],
        surfaceLock <4ac1305d, 3cb25dd5>[count 1, qsz 0, owner <J3D-Renderer-1>]]]
        at jogamp.opengl.x11.glx.X11GLXContext.createImpl(X11GLXContext.java:393)
        at jogamp.opengl.GLContextImpl.makeCurrentWithinLock(GLContextImpl.java:770)
        at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:653)
        at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:591)
        at javax.media.j3d.JoglPipeline.createNewContext(JoglPipeline.java:6411)
        at javax.media.j3d.Canvas3D.createNewContext(Canvas3D.java:4612)
        at javax.media.j3d.Canvas3D.createNewContext(Canvas3D.java:2386)
        at javax.media.j3d.Renderer.doWork(Renderer.java:881)
        at javax.media.j3d.J3dThread.run(J3dThread.java:271)
Error in Java 3D : 3 Renderer: Error creating Canvas3D graphics contextX11Util.Display: Shutdown (JVM shutdown: true, open (no close attempt): 2/2, reusable (open, marked uncloseable): 0, pending (open in creation order): 2)
X11Util: Open X11 Display Connections: 2
X11Util: Open[0]: NamedX11Display[:0, 0x7f6bd802c890, refCount 1, unCloseable false]
X11Util: Open[1]: NamedX11Display[:0, 0x7f6bd8213de0, refCount 1, unCloseable false]


 - system: `"x86_64-linux"` 
 - host os: `Linux 5.15.43, NixOS, 22.05 (Quokka), 22.05.866.1f8d88087a3` 
 - multi-user?: `yes` 
 - sandbox: `yes` 
 - version: `nix-env (Nix) 2.8.1` 
 - channels(root): `"home-manager-22.05.tar.gz, nixos-22.05"` 
 - channels(usera): `""` 
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

let 
  nvidia-offload = pkgs.writeShellScriptBin "nvidia-offload" ''
    export __NV_PRIME_RENDER_OFFLOAD=1
    export __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0
    export __GLX_VENDOR_LIBRARY_NAME=nvidia
    export __VK_LAYER_NV_optimus=NVIDIA_only
    exec -a "$0" "$@"
  '';

  
  nvidia-status = pkgs.writeShellScriptBin "nvidia-offload-status" ''
      state="$(cat /sys/bus/pci/devices/0000:01:00.0/power/runtime_status)"
      echo "GPU state: $state"
      cat /proc/driver/nvidia/gpus/*/power
    '';

in 

{
   imports =
    [ 
    ];


   environment.systemPackages = [ 
    nvidia-offload 
    nvidia-status
    ];

   services.thermald.enable = lib.mkDefault true;

   programs.atop.atopgpu.enable = true ;


   hardware.cpu.intel.updateMicrocode = true; 


   hardware.nvidia = { 

     prime = {
         intelBusId  = "0@0:2:0"; 
         nvidiaBusId = "1@0:0:0";
       };
   };

   services.xserver = { 
         videoDrivers = [
                         "nvidia" 
         ] ;  
     };
  
   hardware = {

       video.hidpi.enable = lib.mkDefault true ;

       opengl = {
         enable = true ;             

         extraPackages = with pkgs; [
             intel-ocl

             intel-media-driver 

             vaapiIntel         
             vaapiVdpau
             libvdpau-va-gl
             ];

         driSupport = true;
         driSupport32Bit = true ;     
         extraPackages32 = with pkgs.pkgsi686Linux; [ vaapiIntel libva   libvdpau-va-gl vaapiVdpau ];
        } ;
  } ;

}

inxi -G

Graphics:  Device-1: Intel CometLake-S GT2 [UHD Graphics 630] driver: i915 v: kernel 
           Device-2: NVIDIA TU104GL [Quadro RTX 4000] driver: nvidia v: 515.48.07 
           Device-3: Logitech BRIO Ultra HD Webcam type: USB driver: snd-usb-audio,uvcvideo 
           Display: x11 server: X.Org 1.20.14 driver: loaded: nvidia note: n/a (using device driver) 
           resolution: 3840x2160~60Hz 
           OpenGL: renderer: Quadro RTX 4000/PCIe/SSE2 v: 4.6.0 NVIDIA 515.48.07

lsmod | grep "gpu"

i2c_nvidia_gpu         16384  0
i2c_core              102400  12 videodev,ucsi_ccg,i2c_nvidia_gpu,drm_kms_helper,i2c_algo_bit,ee1004,nvidia,i2c_smbus,i2c_i801,i915,i2c_dev,drm

nvidia-offload glxinfo | grep 'OpenGL render'
OpenGL renderer string: Quadro RTX 4000/PCIe/SSE2

glxinfo | grep 'version'

server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
OpenGL core profile version string: 4.6.0 NVIDIA 515.48.07
OpenGL core profile shading language version string: 4.60 NVIDIA
OpenGL version string: 4.6.0 NVIDIA 515.48.07
OpenGL shading language version string: 4.60 NVIDIA
OpenGL ES profile version string: OpenGL ES 3.2 NVIDIA 515.48.07
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
    GL_EXT_shader_group_vote, GL_EXT_shader_implicit_conversions,


nix-shell -p libva-utils --run vainfo

this path will be fetched (0.45 MiB download, 2.72 MiB unpacked):
  /nix/store/yvnh3j10b7smjn8pzyhydb0fxsni9ysl-libva-utils-2.14.0
copying path '/nix/store/yvnh3j10b7smjn8pzyhydb0fxsni9ysl-libva-utils-2.14.0' from 'https://cache.nixos.org'...
libva info: VA-API version 1.14.0
libva info: Trying to open /run/opengl-driver/lib/dri/nvidia_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva error: /run/opengl-driver/lib/dri/nvidia_drv_video.so init failed
libva info: va_openDriver() returns 1
vaInitialize failed with error code 1 (operation failed),exit

Afaik, the order of entries in services.xserver.videoDrivers is important.
So when you put "nvidia" before "intel" that gets prioritized.

Other than that you can either have primus or bumblebee for using the dGpu in case of nvidia.
https://nixos.wiki/wiki/Nvidia#Nvidia%20Prime

I am currently suffering from similar issues though. In my case nvidia-offload works, but the intel gpu does not. I found out why (mesa drivers are out of date) but I have no idea how to fix that.
More context here in case your issue is similar: Need help pinning opengl mesa package
Especially try running $ nix run -f channel:nixos-unstable glxinfo

I added some more context information

if different files get imported how to guarantee a sequence (e.g. in services.xserver.videoDrivers)?