Skip to content

linuxPackages.nvidia_x11.libXNVCtrl: make the shared library available#312811

Merged
SomeoneSerge merged 3 commits intoNixOS:masterfrom
aidalgol:libxnvctrl-shared
May 24, 2024
Merged

linuxPackages.nvidia_x11.libXNVCtrl: make the shared library available#312811
SomeoneSerge merged 3 commits intoNixOS:masterfrom
aidalgol:libxnvctrl-shared

Conversation

@aidalgol
Copy link
Copy Markdown
Contributor

Description of changes

The libXNVCtrl library is used by several programs in nixpkgs to get Nvidia GPU stats, and they all load the library dynamically, but the libXNVCtrl package only builds a static library. There is no build flag to enable this, so we have to patch the Makefile to produce a shared library. We then add the package to hardware.opengl.extraPackages in the NixOS module hardware/video/nvidia.nix so that it is available on Nvidia-enabled systems.

Based on a patch by @negativo17 for Fedora: https://github.com/negativo17/nvidia-settings/blob/master/nvidia-settings-libXNVCtrl.patch

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions Bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels May 19, 2024
@aidalgol aidalgol requested a review from a team May 19, 2024 06:06
@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. labels May 19, 2024
Copy link
Copy Markdown
Contributor

@ConnorBaker ConnorBaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor nit; I can try the builds later to verify they build.

Do you have an easy way to verify this is working as intended?

Also, would you mind adding yourself to the list of maintainers for pkgs/os-specific/linux/nvidia-x11/settings.nix?

Comment thread pkgs/os-specific/linux/nvidia-x11/settings.nix Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at https://github.com/NVIDIA/nvidia-settings/blob/4e4bb3fecbbdd902d6f4bc6bbfb9902043c5c7f6/src/libXNVCtrl/Makefile#L39-L53, it seems that we could build the .so just by invoking make with appropriate arguments. I think that'd be more maintainable than patching Makefile per-release.

If we wish to build both the static and the shared object in a single derivation we'd still have to define a custom buildPhase, but I think that's OK.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no make rule for a .so file in xnvctrl.mk (or anywhere that I can see), so I don't think we can do this via make arguments. We could possibly just bypass make entirely and invoke the commands directly in a custom buildPhase, but I'm not sure whether that would be more maintainable.

Copy link
Copy Markdown
Contributor

@SomeoneSerge SomeoneSerge May 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't notice the $(AR) part, I was naive to think they had some kind of a generic rule for linkage...

Copy link
Copy Markdown
Contributor

@SomeoneSerge SomeoneSerge May 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still like to get rid of the conditional patches. A part of the diff doesn't seem to be essential to the derivation, e.g. the clean (no-op for nix) and all (we have makeFlags/makeTargets for that) rules. Maybe omitting those we could have a single patch that successfully applies to all releases: adding nothing but the .so rule.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to simplify down to two patches, because xnvctrl.mk does not exist in the 3xx versions.

@aidalgol
Copy link
Copy Markdown
Contributor Author

Do you have an easy way to verify this is working as intended?

The program I was trying to get to work with my nvidia GPU was MangoHUD, so running vkcube with that was how I was testing my fix.

Before changes:

mangohud vkcube
Selected GPU 0: NVIDIA GeForce RTX 3080, type: DiscreteGpu
[2024-05-20 06:44:07.254] [MANGOHUD] [error] [loader_nvctrl.cpp:39] Failed to open 64bit libXNVCtrl.so.0: libXNVCtrl.so.0: cannot open shared object file: No such file or directory
[2024-05-20 06:44:07.254] [MANGOHUD] [error] [nvctrl.cpp:48] XNVCtrl loader failed to load

After changes:

mangohud vkcube
Selected GPU 0: NVIDIA GeForce RTX 3080, type: DiscreteGpu

(With gpu_stats in my ~/.config/MangoHud/MangoHud.conf.)

Copy link
Copy Markdown
Contributor

@SomeoneSerge SomeoneSerge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've some general comments left, in the line of "are we solving the right problem":

  1. It seems that patching Makefiles takes approximately the same amount of code as shipping our own ad hoc CMakeLists.txt would have...
  2. It seems that upstream does not distribute shared libraries, so it feels kind of arbitrary that MangoHUD should use it as one.
  3. Further on MangoHUD, these probably should be in buildInputs:
    # Only the headers are used from these packages
    # The corresponding libraries are loaded at runtime from the app's runpath
    libXNVCtrl
    wayland
    libX11

That said, if you @aidalgol are convinced this is the way to address your use-case, I'll merge

@aidalgol
Copy link
Copy Markdown
Contributor Author

  1. It seems that patching Makefiles takes approximately the same amount of code as shipping our own ad hoc CMakeLists.txt would have...

Do you mean for all of nvidia_x11, or just libXNVCtrl?

  1. It seems that upstream does not distribute shared libraries, so it feels kind of arbitrary that MangoHUD should use it as one.

MangoHUD is not the only package in nixpkgs that references libXNVCtrl.

  • psensor
  • nvfancontrol
  • monitor (yes, it's really called just "monitor")
  • vibrantlinux
  • conky

But I don't know whether any of those try to link dynamically. It sounds as if Conky does: Installing libxnvctrl (525.60.11-2) breaks dependency ‘libXNVCtrl.so=0-64’ required by conky.

Looking at other Linux distributions, at least Arch patches libXNVCtrl similarly.

  1. Further on MangoHUD, these probably should be in buildInputs:
    # Only the headers are used from these packages
    # The corresponding libraries are loaded at runtime from the app's runpath
    libXNVCtrl
    wayland
    libX11

That I'm not too sure about. If someone is using mangohud installed via "standalone" home-manager, and they update that more frequently than their system configuration, wouldn't mangohud have a newer version than the system's driver?

@SomeoneSerge
Copy link
Copy Markdown
Contributor

Do you mean for all of nvidia_x11, or just libXNVCtrl?

libXNVCtrl

Looking at other Linux distributions, at least Arch patches libXNVCtrl similarly.

Seems like the patch belongs in NVIDA/nvidia-settings then, let's aim to eventually move it there.

@aidalgol
Copy link
Copy Markdown
Contributor Author

Do you mean for all of nvidia_x11, or just libXNVCtrl?

libXNVCtrl

In that case, I would not be opposed to instead replacing the upstream Makefile, if you think that would be simpler or easier to maintain.

Looking at other Linux distributions, at least Arch patches libXNVCtrl similarly.

Seems like the patch belongs in NVIDA/nvidia-settings then, let's aim to eventually move it there.

You mean upstream it?

@SomeoneSerge
Copy link
Copy Markdown
Contributor

You mean upstream it?

Yes

In that case, I would not be opposed to instead replacing the upstream Makefile, if you think that would be simpler or easier to maintain.

I'd be in favour of that. But also if we're upstreaming a patch, we'll want to switch to fetchpatch, and it's safer to avoid trying to impose cmake (or anything) on people...

If you need time to choose but you're committed to following up on this, I can merge the current PR as a temporary fix

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one person. label May 20, 2024
@aidalgol
Copy link
Copy Markdown
Contributor Author

I have submitted a PR upstream and have changed the libXNVCtrl derivation to use the diff from the PR, except for the 3xx legacy version.

@aidalgol aidalgol requested a review from SomeoneSerge May 21, 2024 05:41
Copy link
Copy Markdown
Contributor

@SomeoneSerge SomeoneSerge May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Iirc the pull/xxxx/commits/yyyy URL may get invalidated after force-pushing or even rebase-merging (I may be wrong)
  • The fetchpatch now has an updated all rule, but the 3xx patch doesn't and the new commit omits makeTargets

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right. I thought that would make it robust against PR changes, but I think GitHub does invalidate commit URLs on a force push, at least after a while. I think I should back out this commit until the PR is merged.

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label May 22, 2024
Comment thread pkgs/os-specific/linux/nvidia-x11/settings.nix Outdated
Comment thread nixos/modules/hardware/video/nvidia.nix Outdated
aidalgol added 3 commits May 24, 2024 15:52
The libXNVCtrl library is used by several programs in nixpkgs to get
Nvidia GPU stats, and they all load the library dynamically, but the
libXNVCtrl package only builds a static library.  There is no build flag
to enable this, so we have to patch the Makefile to produce a shared
library.

Based on a patch by @negativo17 for Fedora.
https://github.com/negativo17/nvidia-settings/blob/master/nvidia-settings-libXNVCtrl.patch
@ofborg ofborg Bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label May 24, 2024
'';
hardware.opengl = {
extraPackages = [ nvidia_x11.out ];
extraPackages = [ nvidia_x11.out nvidia_x11.settings.libXNVCtrl ];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had another look at the derivation and it actually doesn't seem to depend on nvidia_x11, except through nvidia_x11.settingsVersion. This makes me question if we actually have to deploy this impurely rather than link directly

@SomeoneSerge SomeoneSerge changed the title Make libXNVCtrl shared library available linuxPackages.nvidia_x11.libXNVCtrl: make the shared library available May 24, 2024
@SomeoneSerge SomeoneSerge merged commit 67d54c2 into NixOS:master May 24, 2024
@github-actions
Copy link
Copy Markdown
Contributor

Backport failed for release-23.11, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release-23.11
git worktree add -d .worktree/backport-312811-to-release-23.11 origin/release-23.11
cd .worktree/backport-312811-to-release-23.11
git switch --create backport-312811-to-release-23.11
git cherry-pick -x b57aa88291910689aeaa83ff40dd02c50ab8199b 4e353b67f667766b1c6489f23c20b920ab0c82f5 e8c66dc41dd95c70718ba9f5ab34e9ebe23e49c4

@github-actions
Copy link
Copy Markdown
Contributor

Backport failed for release-24.05, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release-24.05
git worktree add -d .worktree/backport-312811-to-release-24.05 origin/release-24.05
cd .worktree/backport-312811-to-release-24.05
git switch --create backport-312811-to-release-24.05
git cherry-pick -x b57aa88291910689aeaa83ff40dd02c50ab8199b 4e353b67f667766b1c6489f23c20b920ab0c82f5 e8c66dc41dd95c70718ba9f5ab34e9ebe23e49c4

'';
hardware.opengl = {
extraPackages = [ nvidia_x11.out ];
extraPackages = [ nvidia_x11.out nvidia_x11.settings.libXNVCtrl ];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ This will break things. Depending on config ( useSettings or nvidiaSettings ) nvidia_x11.settings might be a empty set. See https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/os-specific/linux/nvidia-x11/generic.nix#L215
This should handle that case or be reverted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submitted #316516 to address this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@megheaiulian thanks for the heads up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants