From 446293584f10d56b91368f500c022f7a93edbe2c Mon Sep 17 00:00:00 2001 From: Mel Bourgeois Date: Mon, 29 Apr 2024 19:37:24 -0500 Subject: [PATCH] nixgl: add module This module enables wrapping programs which require access to libGL with nixGL on non-NixOS systems. --- modules/misc/news.nix | 12 ++++++++ modules/misc/nixgl.nix | 64 ++++++++++++++++++++++++++++++++++++++++++ modules/modules.nix | 1 + 3 files changed, 77 insertions(+) create mode 100644 modules/misc/nixgl.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 8f9a77cfa..801391cc2 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1801,6 +1801,18 @@ in { itself. ''; } + + { + time = "2024-10-25T08:18:30+00:00"; + condition = hostPlatform.isLinux; + message = '' + A new module is available: 'nixGL'. + + NixGL solve the "OpenGL" problem with nix. The 'nixGL' module provides + integration of NixGL into Home Manager. See the "GPU on non-NixOS + systems" section in the Home Manager mantual for more. + ''; + } ]; }; } diff --git a/modules/misc/nixgl.nix b/modules/misc/nixgl.nix new file mode 100644 index 000000000..b6cff7cc0 --- /dev/null +++ b/modules/misc/nixgl.nix @@ -0,0 +1,64 @@ +{ config, lib, pkgs, ... }: + +let cfg = config.nixGL; +in { + meta.maintainers = [ lib.maintainers.smona ]; + + options.nixGL.prefix = lib.mkOption { + type = lib.types.str; + default = ""; + example = lib.literalExpression + ''"''${inputs.nixGL.packages.x86_64-linux.nixGLIntel}/bin/nixGLIntel"''; + description = '' + The nixGL command that `lib.nixGL.wrap` should wrap packages with. + This can be used to provide libGL access to applications on non-NixOS systems. + + Some packages are wrapped by default (e.g. kitty, firefox), but you can wrap other packages + as well, with `(config.lib.nixGL.wrap )`. If this option is empty (the default), + then `lib.nixGL.wrap` is a no-op. + ''; + }; + + config = { + lib.nixGL.wrap = # Wrap a single package with the configured nixGL wrapper + pkg: + + if cfg.prefix == "" then + pkg + else + # Wrap the package's binaries with nixGL, while preserving the rest of + # the outputs and derivation attributes. + (pkg.overrideAttrs (old: { + name = "nixGL-${pkg.name}"; + + # Make sure this is false for the wrapper derivation, so nix doesn't expect + # a new debug output to be produced. We won't be producing any debug info + # for the original package. + separateDebugInfo = false; + + buildCommand = '' + set -eo pipefail + + ${ + # Heavily inspired by https://stackoverflow.com/a/68523368/6259505 + pkgs.lib.concatStringsSep "\n" (map (outputName: '' + echo "Copying output ${outputName}" + set -x + cp -rs --no-preserve=mode "${ + pkg.${outputName} + }" "''$${outputName}" + set +x + '') (old.outputs or [ "out" ]))} + + rm -rf $out/bin/* + shopt -s nullglob # Prevent loop from running if no files + for file in ${pkg.out}/bin/*; do + echo "#!${pkgs.bash}/bin/bash" > "$out/bin/$(basename $file)" + echo "exec -a \"\$0\" ${cfg.prefix} $file \"\$@\"" >> "$out/bin/$(basename $file)" + chmod +x "$out/bin/$(basename $file)" + done + shopt -u nullglob # Revert nullglob back to its normal default state + ''; + })); + }; +} diff --git a/modules/modules.nix b/modules/modules.nix index ba8414721..3b4985083 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -31,6 +31,7 @@ let ./misc/gtk.nix ./misc/lib.nix ./misc/news.nix + ./misc/nixgl.nix ./misc/numlock.nix ./misc/pam.nix ./misc/qt.nix