What else can I try to make my optimus NVidia card work well with X11?

I have a Lenovo Laptop with NVidia + AMD optimus card. I have Manjaro installed on it with optimus-manager and everything works very well in hybrid mode.

Last month I decided to try NixOS and created my configuration files (GitHub - bratfizyk/dotFiles: My config files - ignore the “_old” folder). I even managed to come up with a configuration that works well on Wayland and got my PR merged to nixos-hardware (Lenovo Legion 16achg6 support (#796) · NixOS/nixos-hardware@7763c6f · GitHub).

As I said, I can’t see a problem with Wayland, but I’m wondering what can be the reason for problems with X11?

The symptoms are: after I log to my Plasma desktop, only the build in laptop works and the external screen stays black. Typing xradr --listproviders shows only the AMD card, so I guess that HDMI must be connected to NVidia.

I’ve tried the sync/reverse sync methods described here (Nvidia - NixOS Wiki), but there was only little progress: after logging in I saw the spinning KDE cog on both monitors, but my session was loaded only on the built-in monitor, while the other one was left with frozen cog.

Surprisingly, when I choose “Log out” from the menu, the available options (e.g. “Shutdown”, “Reboot”, etc) show up on both screens as well, but nothing else happens on the external display. In particular: I can’t move my mouse onto the external display.

Is there anything else I could give a go to?

Does your laptop have a hardware mux chip? Can you disable hybrid graphics from the BIOS?
Mine (a Dell G15/G5 15 [marketing is unclear]) has a multiplexer chip that allows the iGPU to output over HDMI. Moreover, it can boot without hybrid graphics by booting only from the Nvidia GPU, which is necessary to prevent flickering on 4K screens. I guess yours probably does not have that, since it fails to output over HDMI with just the iGPU.

However, I would suggest (from inexperience) that it’s an X11 config problem when using sync/reverse-sync. Mine has an issue when booting from the Nvidia graphics that causes the monitor to render at 60Hz and at the wrong scaling/resolution because I haven’t gotten round to configuring X11 fully on it. I mention this because I haven’t needed to configure Xorg at all for my iGPU (with hybrid enabled) to render at 1080p 165Hz on my internal display, so I was surprised when I needed to do anything to get it working right on the dGPU alone.

The fact that your 2nd monitor showed anything at all is promising.

Hi, thanks for the response.

I can change my card settings in BIOS. it’s called “Discrete” and “Dynamic”, which I believe means “NVidia only” and “Hybrid” respectively.

Thanks for the hint with the configuration. My built-in screen also uses 165Hz by default, while my external display can’t handle that and uses the more standard 60 Hz.

However, when I choose “Display Settings” (using Plasma Desktop) in sync / reverse sync mode, it doesn’t show the external display, but only the built-in one.

Anyway, thanks for the hint. I’ll definitely give it a go.
Maybe there’s something about Plasma I don’t understand as well.

Also, I found out that while using sddm display manager, it failed to discover the external display, but gdm deals with both just fine.

Hmm. I just took another look at my config, and I only have:

services.xserver.videoDrivers = ["nvidia"];

rather than:

  services.xserver.videoDrivers = [ "nvidia" "amdgpu" ];

Although I am on Intel, I would have thought that the rules for setting up prime would be the same for both Intel and AMD integrated graphics.

From the option description of hardware.nvidia.prime.sync.enable:
“Note that this option only has any effect if the “nvidia” driver is specified in services.xserver.videoDrivers, and it should preferably be the only driver there.”

This would seem to support my hypothesis. I think X11 is not sure what to do with the two drivers. I load the Intel iGPU driver as an initrd module but don’t mention it again in my config:

boot.initrd.kernelModules = [ "i915" ];

Can the same be done with the amdgpu driver?

Edit

I made a typo, where instead of “hardware.nvidia.prime.sync.enable”, I simply wrote “services.xserver.videoDrivers” again. Oops.

1 Like

Sounds like a good idea, thanks. I’ll experiment a bit with this over the next few days.

Sorry, I made a typo which may have caused a misunderstanding. The option description that I quoted from was that of hardware.nvidia.prime.sync.enable, which makes a lot more sense than services.xserver.videoDrivers requiring itself!

Yeah, no worries, I understood it.
I think it works! Thanks!

As it’s quite late and I might be doing something wrong, I’ll review it once again tomorrow. But it looks promising.

I basically followed your hints and changed the following lines:

services.xserver.videoDrivers = ["nvidia"];
boot.initrd.kernelModules = [ "amdgpu" ];

Too simple to be true, but if it works I’ll update my nixos-hardware config.
Everything seems to work fine both in sync and prime-offload mode. In the latter case I even checked the output of glxinfo before and after running nvidia-offload and the results were as expected.

I’m glad I could provide some kind of aid despite being a newcomer to NixOS. (I installed it only about three weeks ago.) I remembered the specific aspect of only having the nvidia driver loaded into X11 because I found it very strange (and I think I got it wrong at first).

Hopefully that’s it sorted. And good on you for making your own nixos-hardware config. I would consider doing so, but there’s one for an older generation that sort of works for my laptop. I used it as inspiration, and added the ability to boot in Discrete mode, which causes the issues I described earlier with the wrong refresh rate and resolution. Not sure if nixos-hardware submissions are allowed to create specialisations…

On that note, the fact that you have the options in your BIOS to choose “Discrete” or “Dynamic” indicates to me that your laptop probably does have a mux. Apparently latency is lower in discrete mode, and at least for me it eliminated some flickering problems with driving a 4K60Hz TV over HDMI 2.1 (on another Linux distro that needed less set-up).

I confirm it works, thanks a lot for the hint!
I’ve already created a PR: Lenovo Legion 16achg6: Fix gpu configuration to work with both x11/wayland by bratfizyk · Pull Request #802 · NixOS/nixos-hardware · GitHub.

Not sure if nixos-hardware submissions are allowed to create specialisations…

If by “specialisations” you mean “create more specific versions for the same model” than the answer is “yes, they do”. You can e.g. check here the configuration I submitted. It has two versions: hybrid and nvidia (meaning: nvidia-only).

While referencing it in flake.nix I can decide which one I want to use, e.g. I currently use this one:

      nixosConfigurations = {
          # ......
          modules = [ 
            nixos-hardware.nixosModules.lenovo-legion-16achg6-hybrid
            ./legion/configuration.nix
          ];
        };
      };

But there’s no problem with going nvidia-only by using

      nixosConfigurations = {
          # ......
          modules = [ 
            nixos-hardware.nixosModules.lenovo-legion-16achg6-nvidia
            ./legion/configuration.nix
          ];
        };
      };