1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-01 08:49:44 +01:00
home-manager/modules/misc/nixgl.nix
Mel Bourgeois bbd4254d00
nixgl: make desktop files point to wrapped exe
Some desktop files will refer to the absolute path of the original
derivation, which would bypass nixGL wrapping. So we need to replace the
path with the path to the wrapper derivation to ensure the wrapped
version is always launched.
2024-10-25 10:20:37 +02:00

75 lines
2.6 KiB
Nix

{ 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}";
# 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
# If .desktop files refer to the old package, replace the references
for dsk in "$out/share/applications"/*.desktop ; do
if ! grep "${pkg.out}" "$dsk" > /dev/null; then
continue
fi
src="$(readlink "$dsk")"
rm "$dsk"
sed "s|${pkg.out}|$out|g" "$src" > "$dsk"
done
shopt -u nullglob # Revert nullglob back to its normal default state
'';
}));
};
}