This commit is contained in:
Mel Bourgeois 2024-04-30 22:25:12 -07:00 committed by GitHub
commit 727025bc62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 67 additions and 5 deletions

58
modules/misc/nixgl.nix Normal file
View File

@ -0,0 +1,58 @@
{ 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 <package>)`. 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}";
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
'';
}));
};
}

View File

@ -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

View File

@ -238,10 +238,12 @@ in {
package = mkOption {
type = with types; nullOr package;
default = if versionAtLeast config.home.stateVersion "19.09" then
pkgs.firefox
else
pkgs.firefox-unwrapped;
# Wrap with nixGL by default to provide hardware-accelerated WebRender
default = (config.lib.nixGL.wrap
(if versionAtLeast config.home.stateVersion "19.09" then
pkgs.firefox
else
pkgs.firefox-unwrapped));
defaultText = literalExpression "pkgs.firefox";
example = literalExpression ''
pkgs.firefox.override {

View File

@ -62,7 +62,8 @@ in {
package = mkOption {
type = types.package;
default = pkgs.kitty;
# Kitty fails to run without nixGL on genericLinux
default = (config.lib.nixGL.wrap pkgs.kitty);
defaultText = literalExpression "pkgs.kitty";
description = ''
Kitty package to install.