build(flake): Wrap mold so that it works with the nix supplied libraries

This commit is contained in:
Benedikt Peetz 2023-09-03 22:11:41 +02:00
parent a8112d554e
commit eb63cb6247
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
1 changed files with 147 additions and 125 deletions

272
flake.nix
View File

@ -52,139 +52,161 @@
...
}:
flake-utils.lib.eachDefaultSystem (system: let
c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain";
rcb = pkgs.stdenv.mkDerivation {
pname = "rustc_cranelift_backend";
version = "1.0";
buildInputs = [c_rust];
nativeBuildInputs = with pkgs; [gawk fd];
srcs = ["${rustc_cranelift_backend_src}" "${c_rust}"];
sourceRoot = ".";
postUnpack = ''
rust_dir="$(fd . --max-depth 1 | awk '!/source/ && !/env-vars/')"
inherit (pkgs) lib;
bintools-wrapper = "${nixpkgs}/pkgs/build-support/bintools-wrapper";
mold' = pkgs.symlinkJoin {
name = "mold";
paths = [pkgs.mold];
nativeBuildInputs = [pkgs.makeWrapper];
suffixSalt = lib.replaceStrings ["-" "."] ["_" "_"] pkgs.targetPlatform.config;
postBuild = ''
for bin in ${pkgs.mold}/bin/*; do
rm $out/bin/"$(basename "$bin")"
# remove unneeded stuff
rm -r "$rust_dir"/{bin,nix-support,share};
rm -r "$rust_dir"/lib/rustlib/{etc,src,rustc-src};
export prog="$bin"
substituteAll "${bintools-wrapper}/ld-wrapper.sh" $out/bin/"$(basename "$bin")"
chmod +x $out/bin/"$(basename "$bin")"
cp -r "$rust_dir"/. source/extra_dependencies
rm -r "$rust_dir"
cd source
'';
postPatch = ''
# patch bins
for file in $(fd . --type file);do
file="$(file --mime "$file" | awk 'BEGIN{FS=":"}/application\/x-pie-executable/{print $1}')";
if [ "$file" ]; then
echo "file: '$file' matches";
correct_interpreter_path="$(ldd "$file" | tail -n1 | awk 'BEGIN{FS="=> "} {print $2}' | awk 'BEGIN{FS=" "}{print $1}')"
echo "correct interpreter path is: '$correct_interpreter_path'"
patchelf --set-interpreter "$correct_interpreter_path" "$file"
mkdir -p $out/nix-support
substituteAll "${bintools-wrapper}/add-flags.sh" $out/nix-support/add-flags.sh
substituteAll "${bintools-wrapper}/add-hardening.sh" $out/nix-support/add-hardening.sh
substituteAll "${bintools-wrapper}/../wrapper-common/utils.bash" $out/nix-support/utils.bash
done
'';
};
c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain";
rcb = pkgs.stdenv.mkDerivation {
pname = "rustc_cranelift_backend";
version = "1.0";
buildInputs = [c_rust];
nativeBuildInputs = with pkgs; [gawk fd];
srcs = ["${rustc_cranelift_backend_src}" "${c_rust}"];
sourceRoot = ".";
postUnpack = ''
rust_dir="$(fd . --max-depth 1 | awk '!/source/ && !/env-vars/')"
if [ "$(patchelf --print-interpreter "$file")" = "$correct_interpreter_path" ];then
echo "Set interpreter";
else
echo "Failed to set interprter, the interpreter still is $(patchelf --print-interpreter )";
exit 1
fi
fi
done
# remove unneeded stuff
rm -r "$rust_dir"/{bin,nix-support,share};
rm -r "$rust_dir"/lib/rustlib/{etc,src,rustc-src};
# patch libs
all_files=$(mktemp);
for file in $(fd .);do
canonical_path="$(readlink -f "$file")"
file="$(file --mime "$canonical_path" | awk 'BEGIN{FS=":"}/application\/x-sharedlib/{print $1}')";
if [ "$file" ]; then
echo "$file" >> $all_files;
fi
done
while read -r file; do
echo "___________________";
echo "Checking file: '$file'";
is_missing="$(ldd "$file" | awk 'BEGIN{FS="=>"}{if (/not found/){print $1}}')"
if [ "$is_missing" ];then
echo "Warning: The following things are missing:";
for line in $is_missing; do
echo " $line";
done
echo
for line in $is_missing; do
echo "Searching for a substitute for '$line'"
substitute="$(grep "$line" "$all_files" | awk 'BEGIN{FS=" "}{print $1}' | tail -n1)";
if [ "$substitute" ]; then
echo "Found '$substitute', which can substitute '$line'"
echo "Patching.."
patchelf --replace-needed "$line" "$(readlink -f "$substitute")" "$file";
else
echo "Error: Failed to find a substitute"
fi
done
else
echo "This file is not missing anything"
fi
done < $all_files
rm $all_files
'';
installPhase = ''
install -d $out/
cp -r ./. $out/
'';
};
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
cp -r "$rust_dir"/. source/extra_dependencies
rm -r "$rust_dir"
cd source
'';
postPatch = ''
# patch bins
for file in $(fd . --type file);do
file="$(file --mime "$file" | awk 'BEGIN{FS=":"}/application\/x-pie-executable/{print $1}')";
if [ "$file" ]; then
echo "file: '$file' matches";
correct_interpreter_path="$(ldd "$file" | tail -n1 | awk 'BEGIN{FS="=> "} {print $2}' | awk 'BEGIN{FS=" "}{print $1}')"
echo "correct interpreter path is: '$correct_interpreter_path'"
patchelf --set-interpreter "$correct_interpreter_path" "$file"
nightly = true;
rust =
if nightly
then pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)
else pkgs.rust-bin.stable.latest.default;
if [ "$(patchelf --print-interpreter "$file")" = "$correct_interpreter_path" ];then
echo "Set interpreter";
else
echo "Failed to set interprter, the interpreter still is $(patchelf --print-interpreter )";
exit 1
fi
fi
done
craneLib = (crane.mkLib pkgs).overrideToolchain rust;
# patch libs
all_files=$(mktemp);
for file in $(fd .);do
canonical_path="$(readlink -f "$file")"
file="$(file --mime "$canonical_path" | awk 'BEGIN{FS=":"}/application\/x-sharedlib/{print $1}')";
if [ "$file" ]; then
echo "$file" >> $all_files;
fi
done
while read -r file; do
echo "___________________";
echo "Checking file: '$file'";
is_missing="$(ldd "$file" | awk 'BEGIN{FS="=>"}{if (/not found/){print $1}}')"
if [ "$is_missing" ];then
echo "Warning: The following things are missing:";
for line in $is_missing; do
echo " $line";
done
echo
for line in $is_missing; do
echo "Searching for a substitute for '$line'"
substitute="$(grep "$line" "$all_files" | awk 'BEGIN{FS=" "}{print $1}' | tail -n1)";
if [ "$substitute" ]; then
echo "Found '$substitute', which can substitute '$line'"
echo "Patching.."
patchelf --replace-needed "$line" "$(readlink -f "$substitute")" "$file";
else
echo "Error: Failed to find a substitute"
fi
done
else
echo "This file is not missing anything"
fi
done < $all_files
rm $all_files
'';
installPhase = ''
install -d $out/
cp -r ./. $out/
'';
};
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
nativeBuildInputs = with pkgs; [
pkg-config
mold
rcb
nightly = true;
rust =
if nightly
then pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)
else pkgs.rust-bin.stable.latest.default;
craneLib = (crane.mkLib pkgs).overrideToolchain rust;
nativeBuildInputs = with pkgs; [
pkg-config
mold'
rcb
];
buildInputs = with pkgs; [
openssl
lua54Packages.lua
];
craneBuild = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
doCheck = true;
inherit nativeBuildInputs buildInputs;
};
in {
packages = {
default = craneBuild;
};
app.default = {
type = "app";
program = "${self.packages.${system}.default}/bin/trinitix";
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nil
alejandra
statix
ltex-ls
rust
rust-analyzer
cargo-edit
cargo-expand
];
buildInputs = with pkgs; [
openssl
lua54Packages.lua
];
craneBuild = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
doCheck = true;
inherit nativeBuildInputs buildInputs;
};
in {
packages = {
default = craneBuild;
};
app.default = {
type = "app";
program = "${self.packages.${system}.default}/bin/trinitix";
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nil
alejandra
statix
ltex-ls
rust
rust-analyzer
cargo-edit
cargo-expand
];
inherit nativeBuildInputs buildInputs;
};
});
inherit nativeBuildInputs buildInputs;
};
});
}
# vim: ts=2