Skip to content

Instantly share code, notes, and snippets.

@SuzanneSoy
Last active September 17, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SuzanneSoy/f56e431e096991029de1f154326986ea to your computer and use it in GitHub Desktop.
Save SuzanneSoy/f56e431e096991029de1f154326986ea to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
# Call with:
#
# nixpatch.sh file_1 file_2 … file_n
#
# where each file_i is either an ELF executable or a shell script.
#
# This script will break badly if any of the arguments contain spaces.
# Don't use spaces in the file_i names!
#
# This script will replace each file with a symlink to a wrapepd version.
#
# This script will create a backup of each file as .file_i-original, and will
# skip files for which a backup already exists.
#
# This script creates a .result-file_i symlink which points to NixOS's copy
# of the wrapper file. Removing or moving that hidden symlink will get
# the wrapper file garbage-collected by nix-collect-garbage.
for toPatch in $*; do
if test -f "$toPatch" && ! test -e ".$toPatch-original"; then
cat > .$toPatch-patch.nix <<-EOF
{ stdenv, runCommand, fetchurl, makeFontsConf, makeWrapper
, cairo, coreutils, fontconfig, freefont_ttf
, glib, gmp, gtk2, libffi, libjpeg, libpng
, libtool, mpfr, openssl, pango, poppler
, readline, sqlite
}:
let
fontsConf = makeFontsConf {
fontDirectories = [ freefont_ttf ];
};
libPath = stdenv.lib.makeLibraryPath [
cairo
fontconfig
glib
gmp
gtk2
libjpeg
libpng
mpfr
openssl
pango
poppler
readline
sqlite
];
in
runCommand "$toPatch" { nativeBuildInputs = [ makeWrapper ]; } ''
echo "Date is now (to get something unique): $(date)"
mkdir -p \$out/bin
cp -vi \${./$toPatch} \$out/bin/$toPatch
makeWrapper \$out/bin/$toPatch \$out/bin/$toPatch-wrapper --set LD_LIBRARY_PATH "\${libPath}"
chmod +x \$out/bin/$toPatch-wrapper
''
EOF
nix-build -E "with import <nixpkgs>{}; callPackage ./.$toPatch-patch.nix {}" -o .$toPatch-result
mv -i $toPatch .$toPatch-original
ln -s .$toPatch-result/bin/$toPatch-wrapper $toPatch
echo "Success: $toPatch."
else
echo "Skipping $toPatch, it is already patched."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment