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";
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
# will be more reproducible and more efficient.
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
# This is one by default, you can switch it to off if you want to reduce a
# bit the size of nixGL closure.
@ -15,5 +20,10 @@
}
}:
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
# will be more reproducible and more efficient.
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
# This is one by default, you can switch it to off if you want to reduce a
# bit the size of nixGL closure.
@ -14,27 +19,37 @@
}:
let
# The nvidia version. Either fixed by the `nvidiaVersion` argument, or
# auto-detected.
_nvidiaVersion = if nvidiaVersion != null
then nvidiaVersion
_nvidiaVersionFile =
if nvidiaVersionFile != null then
nvidiaVersionFile
else
# This is the auto-detection mecanism. This is ugly.
# We read /proc/driver/nvidia/version which is set by the Nvidia driver kernel module.
# 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
# then let nix invalidate the derivation if the content of this file
# changes, but that's not possible, see
# https://github.com/NixOS/nix/issues/3539
# But /proc is readable at build time! So runCommand works fine.
import (runCommand "auto-detect-nvidia" {
# HACK: Get the version from /proc. It turns out that /proc is mounted
# inside of the build sandbox and varies from machine to machine.
#
# builtins.readFile is not able to read /proc files. See
# https://github.com/NixOS/nix/issues/3539.
runCommand "impure-nvidia-version-file" {
# To avoid sharing the build result over time or between machine,
# Add an impure parameter to force the rebuild on each access.
time = builtins.currentTime;
}
''
# 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)"
echo "\"$VERSION\"" > $out
'');
"cp /proc/driver/nvidia/version $out || touch $out";
# The nvidia version. Either fixed by the `nvidiaVersion` argument, or
# 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: {
name = oldAttrs.name + "-${_nvidiaVersion}";
@ -57,7 +72,6 @@ let
done
'';
};
in
rec {
nvidia = (linuxPackages.nvidia_x11.override {
@ -175,8 +189,9 @@ in
# The output derivation contains nixGL which point either to
# nixGLNvidia or nixGLIntel using an heuristic.
nixGLDefault =
if builtins.pathExists "/proc/driver/nvidia/version"
then nixGLCommon nixGLNvidia
else nixGLCommon nixGLIntel;
if _nvidiaVersion != null then
nixGLCommon nixGLNvidia
else
nixGLCommon nixGLIntel
;
}