Skip to content

Instantly share code, notes, and snippets.

@dtzWill
Created March 13, 2018 05:48
Show Gist options
  • Save dtzWill/321ab77bb5bcde6af74c277c8ad4cb4c to your computer and use it in GitHub Desktop.
Save dtzWill/321ab77bb5bcde6af74c277c8ad4cb4c to your computer and use it in GitHub Desktop.
{ lib
, wllvmPkgs
, allvm-scripts
, allvm-tools
, allexePkgs
#, alltogetherPkgs
, upstreamNixPkgs ? (fetchGit { url = https://github.com/NixOS/nixpkgs; rev = "e6b8eb0280be5b271a52c606e2365cb96b7bd9f1"; })
, muslPkgs ? import upstreamNixPkgs { localSystem = { config = "x86_64-unknown-linux-musl"; }; }
}:
with wllvmPkgs.runCommand;
with wllvmPkgs.makeWrapper;
wllvmPkgs.nixUnstable.overrideAttrs(o: {
name = "nix-mux-${o.version}";
# outputs = o.outputs ++ [ "orig" "bc" "allexe" "mux" "muxopt" ];
outputs = [ "out" "dev" /*"man"*/ /*"doc"*/ ] ++ [ "orig" "bc" "allexe" "mux" "muxopt" ];
preConfigure = (o.preConfigure or "") + ''
NIX_CFLAGS_COMPILE+=" -g0"
# Copy bash allexe into our output, will be our shell
# Also copy (most?) other deps
mkdir -p {$allexe,$out}/libexec/nix/
cp -t $allexe/libexec/nix \
${allexePkgs.bash44}/bin/bash \
${allexePkgs.coreutils_clean}/bin/* \
${allexePkgs.gnutar-no-nls}/bin/tar \
${allexePkgs.xz}/bin/xz \
${allexePkgs.gzip}/bin/gzip \
${allexePkgs.bzip2}/bin/bzip2 \
${allexePkgs.lsof}/bin/lsof \
${allexePkgs.unzip}/bin/unzip
rm $allexe/libexec/nix/stdbuf
ln -s $allexe/libexec/nix/* $out/libexec/nix/
export ALLVM_CACHE_DIR=$PWD/build-cache
PATH=$out/libexec/nix:$PATH
configureFlagsArray+=(--with-sandbox-shell=$out/libexec/nix/bash)
'';
# already applied in fork?
#patches = (o.patches or []) ++ [
# ../pkgs/tools/package-management/nix/0001-prepend-out-libexec-nix-to-PATH-searching.patch
#];
doInstallCheck = false; # during dev
postFixup = ''
export PATH=${lib.makeBinPath [ allvm-tools allvm-scripts muslPkgs.coreutils ]}:$PATH
mkdir -p $out $orig $bc $allexe
## Duplicate binaries that are symlinked
rm $out/bin/nix-{shell,hash}
cp $out/bin/{nix,nix-hash}
cp $out/bin/{nix,nix-hash}.bc
cp $out/bin/{nix,__run_in_chroot}
cp $out/bin/{nix,__run_in_chroot}.bc
cp $out/bin/nix-{build,shell}
cp $out/bin/nix-{build,shell}.bc
while IFS= read -r -d ''$'\0' i; do
# If it's not an ELF file, skip
if ! isELF "$i"; then continue; fi
# If it's a shared library, skip (this check could be better)
if [[ $i =~ ".so" ]]; then continue; fi
allexe_path="$allexe''${i#$out}"
echo "$i -> $allexe_path"
newdir=$(dirname $allexe_path)
mkdir -p $newdir
${allvm-scripts}/bin/copy-libs.sh $i $allexe_path
done < <(find $out -not -path "*/lib/crt/*crt*.o" -type f -print0|sort -zu)
mkdir -p {$orig,$bc}/{bin,lib,libexec/nix}
mv $out/bin/*.bc $bc/bin/
mv $out/lib/*.bc $bc/lib/
mv $out/libexec/nix/*.bc $bc/libexec/nix/
mv $out/bin/* $orig/bin/
mv $out/lib/*so $orig/lib/
mv $out/libexec/nix/* $orig/libexec/nix/
# mux
mkdir -p $mux $muxopt
allmux $allexe/bin/* $allexe/libexec/nix/* -o $mux/nix-mux
alltogether $mux/nix-mux -o $muxopt/nix-muxopt
# gen binary
export ALLVM_CACHE_DIR=$PWD/cache
mkdir -p $ALLVM_CACHE_DIR
allready $muxopt/nix-muxopt
mv $ALLVM_CACHE_DIR/static_binaries/* $out/bin/nix
# Remove dummy entries
# (mostly so we don't create symlink for it below)
rm $orig/bin/__run_in_chroot
rm $bc/bin/__run_in_chroot.bc
# Link allmux'd static binary to all needed entry points
for x in $orig/bin/* $orig/libexec/nix/*; do
if [ "$(basename $x)" = "nix" ]; then
continue
fi
ln -rs $out/bin/nix "$out''${x#$orig}"
done
# force-drop openssl ref, hopefully okay O:)
sed -re 's,/nix/store/[a-z0-9]{32}-openssl,/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-openssl,g' \
-i $out/bin/nix
'';
})
@tomberek
Copy link

Is there more detail or an update to this publicly available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment