dotfiles/home-manager/utils/nixGLWrap.nix

37 lines
1.2 KiB
Nix
Raw Normal View History

2024-04-22 17:50:39 +02:00
# Call once on import to load global context
2024-05-28 14:38:22 +02:00
{ pkgs, config }:
2024-04-22 17:50:39 +02:00
# Wrap a single package
pkg:
2024-05-28 14:38:22 +02:00
if config.nixGLPrefix == "" then
pkg
2024-04-22 17:50:39 +02:00
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
2023-05-03 02:27:17 +02:00
2024-04-22 17:50:39 +02:00
${
# Heavily inspired by https://stackoverflow.com/a/68523368/6259505
2024-05-28 14:38:22 +02:00
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" ])
)
2024-04-22 17:50:39 +02:00
}
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\" ${config.nixGLPrefix} $file \"\$@\"" >> "$out/bin/$(basename $file)"
chmod +x "$out/bin/$(basename $file)"
done
shopt -u nullglob # Revert nullglob back to its normal default state
'';
2024-05-28 14:38:22 +02:00
}))