Skip to content

Instantly share code, notes, and snippets.

@SuzanneSoy
Created October 20, 2016 00:24
Show Gist options
  • Save SuzanneSoy/ddee586750e39e05cb53477d29262df4 to your computer and use it in GitHub Desktop.
Save SuzanneSoy/ddee586750e39e05cb53477d29262df4 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 insert a wrapped version for each file replace
# each file in the ./wrapped/ folder.
#
# 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 original wrapper file garbage-collected by nix-collect-garbage.
# Since this script copies and adjusts the wrapper, it removes that directory anyway.
for toPatch in $*; do
if test -f "$toPatch" && ! test -e "./wrapped/$toPatch"; 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
mkdir -p ./wrapped/
#ln -sf ../.$toPatch-result/bin/$toPatch-wrapper ./wrapped/$toPatch
(head .$toPatch-result/bin/$toPatch-wrapper -n -1;
echo 'exec "$(dirname "$0")/../'$toPatch'" "${extraFlagsArray[@]}" "$@"') > ./wrapped/$toPatch
chmod +x ./wrapped/$toPatch
rm .$toPatch-result
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