NixOS / nixos-hardware

A collection of NixOS modules covering hardware quirks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for Lenovo Legion 7 16achg6

bratfizyk opened this issue · comments

I'm using Lenovo Legion 7 16achg6.
nixos-hardware repo already contains an entry for a very similar module 16ach6h here

I naively tried using it on my machine but after a reboot the X service failed to start.

After a quick investigation I realized that for 16ach6h AMD GPU is connected to PCI:6:0:0.

However, on my machine it's PCI:5:0:0.
Here's a link to my dot file:

https://github.com/bratfizyk/dotFiles/blob/6c1125614f0d3de3b747447f574c695820f610b2/legion/configuration.nix#L113

If I'd like to create a PR adding support for my machine, shall I copy all the files required for 16ach6h and change the PCI for AMD GPU, or shall I link the files for the existing configuration and only override the fields that differ?

commented

Should be pretty similar looking config wise, though you probably don't want the 16ach6h's edid part, as that's a workaround to get higher refresh rates working. And there's also some other bits that might not affect your device, such as https://github.com/NixOS/nixos-hardware/pull/550/files

OK, thanks. I'll experiment with it and will let you know.
I guess I can create a branch in this repo and then refer to it using e.g. a flake like this, right?

inputs.nixos-hardware.url = "github:NixOS/nixos-hardware/myNewBranch";
commented

I don't think you'd have repo perms to do so, would be better to just point to your own fork in the meantime. I'd also advise to first try with the simplest of modules, ie just "common" imports and hardware.nvidia.prime PCI ids before diving into having separate dedicated and hybrid setups.

Thanks, doing this now.

Ok, here it is:
https://github.com/bratfizyk/nixos-hardware/blob/master/lenovo/legion/16achg6/hybrid/default.nix

I copied hybrid config from the existing model and it almost worked. I had to comment out nvidia-prime common module, as I was unable to start X with it. Do you have an idea how can I check the exact error message my machien complained at?

Also, I added a few other bits and pieces that I had found somewhere else. Not sure it these are needed, so I also left them commented out.

Last thing: as I'm not too much used to this sort of low level stuff, can you recommend a tool which I could keep an eye on to see if changes to my nix config impacts machine's performance?

commented

as I was unable to start X with it

Probably related to amdgpu.loadInInitrd = lib.mkDefault false; which is part of making the EDID thing work. Need to try submit a PR to fix that at some point.

Do take into account that not including the prime thing means you're running off the nvidia GPU which will greatly affect battery life, try adding it again while leaving the "loadInInitrd" part commented out. Also check X's error log and search online as needed.

In my case since I need the initrd part I have https://github.com/4JX/nixos-config/blob/6f02677b27063d66a66d65a57ac2447a7b3e9a95/hosts/terra/xorg.nix#L25-L26, but you most likely don't if you leave that unset.

Also, I added a few other bits and pieces that I had found somewhere else. Not sure it these are needed, so I also left them commented out.

open defaults to false already which is probably how you want to have it. The other two options are probably already set elsewhere, but can't hurt to have them explicitly declared.

Do you have an idea how can I check the exact error message my machien complained at?

Check journalctl (ie. journalctl -b 0 -r) and look for X's error log from a TTY after a failed start.

as I'm not too much used to this sort of low level stuff, can you recommend a tool which I could keep an eye on to see if changes to my nix config impacts machine's performance?

No idea about those

I've found the error message. It says:

attempting starting display server on vt 7 failed
failed to read display number from pipe

(EE) Server terminated with error (1). Closing log file.
(II) Suspending AIGLX clients for VT switch.
(EE) failed to create screen resources.
(EE) modeset(G0): Failed to create pixmap

and then it refuses to start sddm.

Looking for a solution on the web now.

OK, I made it work. However, I was a bit experimenting without understanding too much, so whether or not I found a good solution, it's a different story :).

Here's the current state of my nixos-hardware fork: https://github.com/bratfizyk/nixos-hardware/blob/master/lenovo/legion/16achg6/hybrid/default.nix

In order to make it work I had to add the following line to my configuration.nix:

https://github.com/bratfizyk/dotFiles/blob/2c2bc2d9f6f742565bad59731b6075418e88a44b/legion/configuration.nix#L41

However, I'm a bit confused, as in nixos-hardware in nvidia-prime/disable.nix there is:

boot.blacklistedKernelModules = [ "nouveau" "nvidia" "nvidia_drm" "nvidia_modeset" ];

So I have no idea and I'm confused :).
If I understand the idea of prime correctly, all the processes should be run on my AMD GPU, unless explicitly run on NVidia, right? If so, how can I check which GPU is now used by deafault in my system?

I found a post here: https://askubuntu.com/a/236781
For me launching glxinfo returns:

OpenGL renderer string: AMD Radeon Graphics (renoir, LLVM 15.0.7, DRM 3.49, 6.1.63)
commented

So I have no idea and I'm confused :).

You're importing ../../../../common/gpu/nvidia/prime.nix, which doesn't import the disable.nix file with it, it is only when importing a folder path that you implicitly import /path/to/folder/default.nix which is not the case here.

You do however probably want to blacklist noveau in your config to use the nvidia driver though if you are not doing that already.

amdgpu.loadInInitrd = lib.mkDefault false;

Again, remove this, you do not need it to get a working config. This is only for the device specific EDID override.

opengl = {
enable = true;
driSupport = true;
driSupport32Bit = true;
};

These are already set in

hardware.opengl = {
driSupport = lib.mkDefault true;
driSupport32Bit = lib.mkDefault true;

If I understand the idea of prime correctly, all the processes should be run on my AMD GPU, unless explicitly run on NVidia, right? If so, how can I check which GPU is now used by deafault in my system?

Yes, you can read more about it in the nvidia docs. The command you ran should be enough to determine the current "main" GPU.

In order to make it work I had to add the following line to my configuration.nix:

You're probably running into the issue that nixpkgs and nixos-hardware likes to use modesetting by default for AMD GPUs, seeing as though you have the same incompatibility as me you can refer to #628 and:
https://github.com/4JX/nixos-config/blob/6f02677b27063d66a66d65a57ac2447a7b3e9a95/hosts/terra/xorg.nix#L28-L32

You can probably directly include that line in the hardware config.

To add a few interesting parts:

  • I currently use Plasma with Wayland. In this setup everything works as expected (though - as I already mentioned - I might be missing something under the hood)
  • I tried switching to X11 and then only my built-in laptop screen works. The external display is not even discovered (I can't see it in Plasma Display Settings).

It's getting more and more interesting :)

commented

Nvidia prime offload won't play well with external displays, as (assuming it is similarly configured to my model) all the ports are wired to the nvidia GPU.

If you want to use external displays with your laptop you would need to use prime.sync/prime.reverseSync

You're importing ../../../../common/gpu/nvidia/prime.nix, which doesn't import the disable.nix file with it, it is only when importing a folder path that you implicitly import /path/to/folder/default.nix which is not the case here.

OK, thanks. I misunderstood the meaning of the imports = [ ./. ]; line, but it seems only to load default.nix from this folder, right?

Again, remove this, you do not need it to get a working config. This is only for the device specific EDID override.
These are already set in

Now I understand, thanks.

You can probably directly include that line in the hardware config.
Yes, that's what I found a similar line in your configuration and that's why I decided to give it a go.

If you want to use external displays with your laptop you would need to use prime.sync/prime.reverseSync
I tried both but neither worked for me, though I read online that reverseSync is what I'm searching for.

However, after some more investigation, I found out this piece of config that did the job for X11:

  specialisation = {
    external-display.configuration = {
      system.nixos.tags = [ "external-display" ];
      hardware.nvidia.prime.offload.enable = lib.mkForce false;
      hardware.nvidia.powerManagement.enable = lib.mkForce false;
    };
  };

I guess that since this is a part of my personal setup, it should be put in my personal configuration, not in nixos-hardware.

As I said before, Wayland works fine even without these lines.

However, after some more investigation, I found out this piece of config that did the job for X11:

  specialisation = {
    external-display.configuration = {
      system.nixos.tags = [ "external-display" ];
      hardware.nvidia.prime.offload.enable = lib.mkForce false;
      hardware.nvidia.powerManagement.enable = lib.mkForce false;
    };
  };

Ignore me, now I understand what does it mean, sorry :).

I think the first version is good to go. I've created a PR for it (#796)
@4JX many thanks for all the help.

Implemented via #796

I'm sorry for waking up the Issue, I have the Legion 7 16achg6 as well and was not able to get it working, however when changing "amdgpuBusId = "PCI:6:0:0";" to "amdgpuBusId = "PCI:5:0:0";" it worked. Isn't it weird that the same hardware uses different busId or is this a common thing?

Hi there,
First thing - don't apologize as you're doing the right thing (i.e. introducing some new value to the discussion).

Regarding your question: amdgpuBusId for 16achg6 is already set to PCI:5:0:0 (see here). Are you sure you imported the right config? I know it's set to PCI:6:0:0 for 16ach6h (see here)

EDIT: By "Are you sure" I meant "Can you confirm that" ;)

Also, from my very recent experience: when I had my BIOS updated by Windows living side-by-side on my machine, it also changed BIOS NVidia settings to NVidia only, which in represented by Discrete in BIOS. NixOS does NOT work for me in this mode and I had to go to BIOS and switch it to hybrid mode (which - again - in BIOS is represented by Dynamic).

I mixed up the numbers, I changing from "amdgpuBusId = "PCI:5:0:0";" to "amdgpuBusId = "PCI:6:0:0";". I'm using hybrid in the bios. It's weird that we have different BusID. Here is what I did to make it work, ignore the commit message, I mixed up the numbers hehe. Commit