nix-community / nixGL

A wrapper tool for nix OpenGL application [maintainer=@guibou]
628 stars 76 forks source link

Runtime selecting nixGL driver #132

Open cfhammill opened 1 year ago

cfhammill commented 1 year ago

I am currently using nixGL in a context where I will be provisioning docker containers that run machine learning code that may run on a variety of host systems. To accommodate this code I use to provision the container generates nixGLNvidia-<version> for a selection of different driver versions. I like the convenience of using nixGL instead of the full wrapper path, but becomes unwieldy if we don't know in advance which driver nixGL should point to. To work around it I have the following nix expression

{ pkgs }:

pkgs.writeTextFile rec {
  name = "nixGL";
  text = ''#!${pkgs.runtimeShell}

  vers=$(${pkgs.gnugrep}/bin/grep "Module" /proc/driver/nvidia/version |\
        ${pkgs.gnused}/bin/sed -E "s/.*Module  ([0-9.]+)  .*/\1/")
  if [ -z "$vers" ]; then
    echo "Failed to find your driver version"
    exit 1
  fi

  exec nixGLNvidia-"$vers" "$@"
  '';
  executable = true;
  destination = "/bin/${name}";
  checkPhase = ''
        ${pkgs.shellcheck}/bin/shellcheck "$out/bin/${name}"

        # Check that all the files listed in the output binary exists
        for i in $(${pkgs.pcre}/bin/pcregrep  -o0 '/nix/store/.*?/[^ ":]+' $out/bin/${name})
        do
          ls $i > /dev/null || (echo "File $i, referenced in $out/bin/${name} does not exists."; exit -1)
        done
      '';
}

which is essentially writeExecutable from nixGL with the text hard-coded to parse the module version and exec the correct nixGLNvidia wrapper.

Is there any interest in me contributing something like this to nixGL, if so any recommendations on how you would like it added?