nix-community/nixGL

Easy automagic patching of applications

piegamesde opened this issue · 2 comments

I want to patch all my applications that require OpenGL to automatically start within the NixGL wrapper. I think that applying it should be as easy as providing a list of packages to be patched. This could for example be done by simply injecting a hook into the nativeBuildInputs via overrideAttrs which then does all of the work.

This is similar to #16 (comment), but deserves its own issue IMO.

I'm using the following overlay:

final: prev:
let
  wrapGeneric = wrapper: pkg: { additionalBinaries ? [] }:
    let
      mainPrograms = if builtins.hasAttr "mainProgram" pkg.meta then [ pkg.meta.mainProgram ] else [];
      bins = mainPrograms ++ additionalBinaries;
    in if builtins.length bins == 0
      then builtins.throw "No mainProgram defined and no additionalBinaries for ${pkg.name}"
      else final.symlinkJoin {
        name = "${pkg.name}-with-nixgl";
        paths = let
          wrapBin = bin: final.writeShellScriptBin bin ''
            exec ${wrapper} ${pkg}/bin/${bin} "$@"
          ''; in (map wrapBin bins) ++ [ pkg ];
        inherit (pkg) meta;
      };
  wrapNixGL = wrapGeneric "${final.nixgl.nixGLIntel}/bin/nixGLIntel";
  wrapVulkanGL = wrapGeneric "${final.nixgl.nixVulkanIntel}/bin/nixVulkanIntel ${final.nixgl.nixGLIntel}/bin/nixGLIntel";
  go = builtins.mapAttrs (n: wrapNixGL prev.${n});
  goVulkan = builtins.mapAttrs (n: wrapVulkanGL prev.${n});
in go {
  # things work fine when meta.mainProgram is specified
  firefox = {};
  # when it isn't, need to specify the binaries to wrap
  anydesk.additionalBinaries = [ "anydesk" ];
} // goVulkan { # adds vulkan env in addition to gl (but I didn't test this extensively)
  steam = {};
}

If you wanted always wrap all binaries, you could try getting the list of files in ${pkg}/bin/, but it doesn't seem like the best idea necessarily.

You'll need to tweak wrapper paths if you're targeting Nvidia proprietary drivers.

In theory, you could overrideAttrs to add wrapProgram to postFixup directly, but nixGL only exposes wrapper scripts, so I went with wrapper scripts and symlinkJoin here. Also, cache is invalidated by overrideAttrs, so you'd end up rebuilding things like firefox and chromium, which isn't fun.