Merge pull request #40 from zimbatm/nvidia-version-file

Nvidia version file
This commit is contained in:
Guillaume Bouchard 2020-04-28 17:12:48 +02:00 committed by GitHub
commit 76196e5da3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 46 deletions

14
all.nix
View File

@ -1,4 +1,16 @@
import ./default.nix { let
pkgs = import <nixpkgs> { config = { allowUnfree = true; }; };
in
{
pure = pkgs.recurseIntoAttrs (pkgs.callPackage ./nixGL.nix {
nvidiaVersion = "440.82"; nvidiaVersion = "440.82";
nvidiaHash = "edd415acf2f75a659e0f3b4f27c1fab770cf21614e84a18152d94f0d004a758e"; nvidiaHash = "edd415acf2f75a659e0f3b4f27c1fab770cf21614e84a18152d94f0d004a758e";
});
versionFile = pkgs.recurseIntoAttrs (pkgs.callPackage ./nixGL.nix {
nvidiaVersionFile = pkgs.writeText "nvidia-version-440.82" ''
NVRM version: NVIDIA UNIX x86_64 Kernel Module 440.82 Wed Apr 1 20:04:33 UTC 2020
GCC version: gcc version 9.3.0 (Arch Linux 9.3.0-1)
'';
});
} }

View File

@ -4,6 +4,11 @@
# Hash of the Nvidia driver .run file. null is fine, but fixing a value here # Hash of the Nvidia driver .run file. null is fine, but fixing a value here
# will be more reproducible and more efficient. # will be more reproducible and more efficient.
nvidiaHash ? null, nvidiaHash ? null,
# Alternatively, you can pass a path that points to a nvidia version file
# and let nixGL extract the version from it. That file must be a copy of
# /proc/driver/nvidia/version. Nix doesn't like zero-sized files (see
# https://github.com/NixOS/nix/issues/3539 ).
nvidiaVersionFile ? null,
# Enable 32 bits driver # Enable 32 bits driver
# This is one by default, you can switch it to off if you want to reduce a # This is one by default, you can switch it to off if you want to reduce a
# bit the size of nixGL closure. # bit the size of nixGL closure.
@ -15,5 +20,10 @@
} }
}: }:
pkgs.callPackage ./nixGL.nix { pkgs.callPackage ./nixGL.nix {
inherit nvidiaVersion nvidiaHash enable32bits; inherit
nvidiaVersion
nvidiaVersionFile
nvidiaHash
enable32bits
;
} }

View File

@ -4,6 +4,11 @@
# Hash of the Nvidia driver .run file. null is fine, but fixing a value here # Hash of the Nvidia driver .run file. null is fine, but fixing a value here
# will be more reproducible and more efficient. # will be more reproducible and more efficient.
nvidiaHash ? null, nvidiaHash ? null,
# Alternatively, you can pass a path that points to a nvidia version file
# and let nixGL extract the version from it. That file must be a copy of
# /proc/driver/nvidia/version. Nix doesn't like zero-sized files (see
# https://github.com/NixOS/nix/issues/3539 ).
nvidiaVersionFile ? null,
# Enable 32 bits driver # Enable 32 bits driver
# This is one by default, you can switch it to off if you want to reduce a # This is one by default, you can switch it to off if you want to reduce a
# bit the size of nixGL closure. # bit the size of nixGL closure.
@ -14,27 +19,37 @@
}: }:
let let
# The nvidia version. Either fixed by the `nvidiaVersion` argument, or _nvidiaVersionFile =
# auto-detected. if nvidiaVersionFile != null then
_nvidiaVersion = if nvidiaVersion != null nvidiaVersionFile
then nvidiaVersion
else else
# This is the auto-detection mecanism. This is ugly. # HACK: Get the version from /proc. It turns out that /proc is mounted
# We read /proc/driver/nvidia/version which is set by the Nvidia driver kernel module. # inside of the build sandbox and varies from machine to machine.
# This fails if the nvidia driver kernel module is not loaded. #
# I'd like to just read the file using `${/proc/driver/nvidia/version}` and # builtins.readFile is not able to read /proc files. See
# then let nix invalidate the derivation if the content of this file # https://github.com/NixOS/nix/issues/3539.
# changes, but that's not possible, see runCommand "impure-nvidia-version-file" {
# https://github.com/NixOS/nix/issues/3539 # To avoid sharing the build result over time or between machine,
# But /proc is readable at build time! So runCommand works fine. # Add an impure parameter to force the rebuild on each access.
import (runCommand "auto-detect-nvidia" {
time = builtins.currentTime; time = builtins.currentTime;
} }
'' "cp /proc/driver/nvidia/version $out || touch $out";
# Written this way so if the version file does not exists, the script crashs
VERSION="$(${pcre}/bin/pcregrep -o1 'Module +([0-9]+\.[0-9]+)' /proc/driver/nvidia/version)" # The nvidia version. Either fixed by the `nvidiaVersion` argument, or
echo "\"$VERSION\"" > $out # auto-detected. Auto-detection is impure.
''); _nvidiaVersion =
if nvidiaVersion != null then
nvidiaVersion
else
# Get if from the nvidiaVersionFile
let
data = builtins.readFile _nvidiaVersionFile;
versionMatch = builtins.match ".*Module +([0-9]+\\.[0-9]+).*" data;
in
if versionMatch != null then
builtins.head versionMatch
else
null;
addNvidiaVersion = drv: drv.overrideAttrs(oldAttrs: { addNvidiaVersion = drv: drv.overrideAttrs(oldAttrs: {
name = oldAttrs.name + "-${_nvidiaVersion}"; name = oldAttrs.name + "-${_nvidiaVersion}";
@ -57,7 +72,6 @@ let
done done
''; '';
}; };
in in
rec { rec {
nvidia = (linuxPackages.nvidia_x11.override { nvidia = (linuxPackages.nvidia_x11.override {
@ -175,8 +189,9 @@ in
# The output derivation contains nixGL which point either to # The output derivation contains nixGL which point either to
# nixGLNvidia or nixGLIntel using an heuristic. # nixGLNvidia or nixGLIntel using an heuristic.
nixGLDefault = nixGLDefault =
if builtins.pathExists "/proc/driver/nvidia/version" if _nvidiaVersion != null then
then nixGLCommon nixGLNvidia nixGLCommon nixGLNvidia
else nixGLCommon nixGLIntel; else
nixGLCommon nixGLIntel
} ;
}