Xorg picks wrong libglx.so with Nvidia Legacy 390 driver

Hello, I’m running a laptop with dual graphics (Lenovo Thinkpad T530, intel and nvidia) and I’m unable to get the nvidia driver working.

I had it working for a while, however latest unstable channel update broke it. It also doesn’t work on 23.11 channel.

I followed the guide here Nvidia - NixOS Wiki to setup legacy 390 driver (I have nvidia NVS5400M and 390 is the last driver that supports it). My setup used PRIME in sync mode and it worked fine (I can confirm my system indeed used it and not the intel driver, I checked loaded module in lspci, glxinfo vendor, performance difference, etc).

After the last unstable update and rebuilt nixos, however, my system no longer can use graphical acceleration: everything is software rasterized and my glxinfo renderer is llvmpipe.

At this point I tried any options related to nvidia drivers and also reinstalled with bare minimum config to make sure nothing else interferes.

So far I tracked it down to this: my xorg no longer loads the correct libglx.so by nvidia:

[    16.977] (II) LoadModule: "glx"
[    16.977] (II) Loading /nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules/extensions/libglx.so
[    16.993] (II) Module glx: vendor="X.Org Foundation"
[    16.993] 	compiled for 1.21.1.11, module version = 1.0.0
[    16.993] 	ABI class: X.Org Server Extension, version 10.0

The loaded glx module is provided by xorg, while it should be picked from /nix/store/v8aivi2zl0igwvyr9bnvbn5z881357fn-nvidia-x11-390.157-6.1.82-bin/lib/xorg/modules/extensions.

After that, NVIDIA driver complains about the wrong glx module:

NVIDIA(0): Enabling 2D acceleration
[    17.041] (EE) NVIDIA(0): Failed to initialize the GLX module; please check in your X
[    17.041] (EE) NVIDIA(0):     log file that the GLX module has been loaded in your X
[    17.041] (EE) NVIDIA(0):     server, and that the module is the NVIDIA GLX module.  If
[    17.041] (EE) NVIDIA(0):     you continue to encounter problems, Please try
[    17.041] (EE) NVIDIA(0):     reinstalling the NVIDIA driver.

Inspecting further, I see that in my generated xorg.conf the ModulePath for xorg’s own modules take precedence over nvidia:

Section "Files"

  FontPath "/nix/store/pldlazgzngdppdn39z92w572fzs4q8px-font-cursor-misc-1.0.4/lib/X11/fonts/misc"
  FontPath "/nix/store/2n3gp57bb21vq93ca0817b5g2ip5n2fy-font-misc-misc-1.1.3/lib/X11/fonts/misc"
  FontPath "/nix/store/ck9p96ckrambx53lp7d34chj3b0h0lff-unifont-15.1.04/share/fonts"
  FontPath "/nix/store/j73wmipp6hzqqdc7psbmy98gc29091gz-font-adobe-100dpi-1.0.4/lib/X11/fonts/100dpi"
  FontPath "/nix/store/qak6k67pcsp8xwkxskcqphvh2w2d0mjg-font-adobe-75dpi-1.0.4/lib/X11/fonts/75dpi"
  ModulePath "/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules"
  ModulePath "/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules/drivers"
  ModulePath "/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules/extensions"
  ModulePath "/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules/input"
  ModulePath "/nix/store/kvamicjvw3q031dlj474a6vjxic0jbjb-xf86-input-evdev-2.10.6/lib/xorg/modules/input"
  ModulePath "/nix/store/v8aivi2zl0igwvyr9bnvbn5z881357fn-nvidia-x11-390.157-6.1.82-bin/lib/xorg/modules/drivers"
  ModulePath "/nix/store/v8aivi2zl0igwvyr9bnvbn5z881357fn-nvidia-x11-390.157-6.1.82-bin/lib/xorg/modules/extensions"
  ModulePath "/nix/store/wmaai5v87wmnllh2l3h56wbaadfk9672-xf86-input-libinput-1.4.0/lib/xorg/modules/input"

EndSection

This section is defined in services.xserver.modules:

Xorg log also reports ModulePath in this order, with xorg being higher than nvidia:

[    16.939] (**) ModulePath set to "/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules,/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules/drivers,/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules/extensions,/nix/store/2zkp7b0qd8msq4ishch4wrjyjdgx4pfk-xorg-server-21.1.11/lib/xorg/modules/input,/nix/store/kvamicjvw3q031dlj474a6vjxic0jbjb-xf86-input-evdev-2.10.6/lib/xorg/modules/input,/nix/store/v8aivi2zl0igwvyr9bnvbn5z881357fn-nvidia-x11-390.157-6.1.82-bin/lib/xorg/modules/drivers,/nix/store/v8aivi2zl0igwvyr9bnvbn5z881357fn-nvidia-x11-390.157-6.1.82-bin/lib/xorg/modules/extensions,/nix/store/wmaai5v87wmnllh2l3h56wbaadfk9672-xf86-input-libinput-1.4.0/lib/xorg/modules/input"

The code that generates xorg configuration seems to sort entries in modules option: nixpkgs/nixos/modules/services/x11/xserver.nix at 44d0940ea560dee511026a53f0e2e2cde489b4d4 · NixOS/nixpkgs · GitHub so it seems that depending on names of xorg-server and nvidia-x11 packages in nix store their order in files section in xorg.conf will be different.

What seems to have happened is that at some point between 23.11 and current unstable xorg-server and nvidia-x11 had different nix store paths so nvidia-x11 sorted higher in modulepath list than xorg-server and nvidia’s libglx.so loaded correctly.

Here’s an example of xorg log https://0x0.st/HEsQ.log with a very similar configuration to mine from this topic: Nvidia 390 driver not working. In this log, the correct nvidia libglx.so is loaded and the log reports the module path list with nvidia having precedence over xorg:

[    69.740] (II) LoadModule: "glx"
[    69.740] (II) Loading /nix/store/jz6jbz1zdnch5xbbbmja7i15a7l3rdm9-nvidia-x11-390.157-6.1.68-bin/lib/xorg/modules/extensions/libglx.so
[    69.744] (II) Module glx: vendor="NVIDIA Corporation"
[    69.744] 	compiled for 4.0.2, module version = 1.0.0
[    69.744] 	Module class: X.Org Server Extension
[    69.744] (II) NVIDIA GLX Module  390.157  Wed Oct 12 09:19:15 UTC 2022
[    69.716] (**) ModulePath set to "/nix/store/6r71kvqpza1din0vah4rgzmfg120q32k-xf86-input-libinput-1.4.0/lib/xorg/modules/input,/nix/store/jz6jbz1zdnch5xbbbmja7i15a7l3rdm9-nvidia-x11-390.157-6.1.68-bin/lib/xorg/modules/drivers,/nix/store/jz6jbz1zdnch5xbbbmja7i15a7l3rdm9-nvidia-x11-390.157-6.1.68-bin/lib/xorg/modules/extensions,/nix/store/p6lkx7hprrw2m3ilyra5bcrglkb871bg-xf86-input-evdev-2.10.6/lib/xorg/modules/input,/nix/store/w605mm513cbq3cgg6kmyyrd7i554q9hl-xorg-server-21.1.9/lib/xorg/modules,/nix/store/w605mm513cbq3cgg6kmyyrd7i554q9hl-xorg-server-21.1.9/lib/xorg/modules/drivers,/nix/store/w605mm513cbq3cgg6kmyyrd7i554q9hl-xorg-server-21.1.9/lib/xorg/modules/extensions,/nix/store/w605mm513cbq3cgg6kmyyrd7i554q9hl-xorg-server-21.1.9/lib/xorg/modules/input"

How can I enforce nvidia store path to take precedence over xorg paths, if elements are sorted? Or, what I can do in general to fix this issue more reliably?