ya-ar/support/rust.nix
2026-02-10 21:24:16 +09:00

100 lines
2.7 KiB
Nix

{ pkgs
, system ? "x86_64-linux"
, zig
, ...
}:
let
systemAsRust = {
x86_64-linux = "x86_64-unknown-linux-gnu";
aarch64-linux = "aarch64-unknown-linux-gnu";
}.${system};
manifest-src = builtins.fetchurl {
url = "https://static.rust-lang.org/dist/2025-05-15/channel-rust-stable.toml";
sha256 = "0594aavj08dw7ba9wndbjg31crkpqz7wcbzzfmgc9yg3j7mbaj99";
};
manifest = builtins.fromTOML (builtins.readFile manifest-src);
simpleComponent = x: builtins.fetchurl {
url = x.url;
sha256 = x.hash;
};
components = [
(simpleComponent manifest.pkg.cargo.target.${systemAsRust})
(simpleComponent manifest.pkg.rustc.target.${systemAsRust})
(simpleComponent manifest.pkg.rust-std.target.${systemAsRust})
(simpleComponent manifest.pkg.rust-std.target.x86_64-unknown-linux-musl)
(simpleComponent manifest.pkg.rust-std.target.aarch64-unknown-linux-musl)
];
in pkgs.stdenvNoCC.mkDerivation rec {
name = "yaar-rust";
srcs = components;
sourceRoot = ".";
dontConfigure = true;
dontBuild = true;
nativeBuildInputs = [ pkgs.makeWrapper ];
buildInputs = [
pkgs.glibc
pkgs.zlib
pkgs.bash
pkgs.gcc.cc.lib
zig
];
dontAutoPatchelf = true;
fixupPhase = ''
SEARCH_DIRS="$out/bin"
if [[ "${system}" == "x86_64-linux" ]]
then
SEARCH_DIRS="$SEARCH_DIRS $out/lib/rustlib/x86_64-unknown-linux-gnu/bin"
fi
if [[ "${system}" == "aarch64-linux" ]]
then
SEARCH_DIRS="$SEARCH_DIRS $out/lib/rustlib/aarch64-unknown-linux-gnu/bin"
fi
find $SEARCH_DIRS -type f -executable | while read binary; do
if file "$binary" | grep -q "ELF"
then
echo "Patching $binary"
patchelf \
--set-interpreter ${pkgs.glibc}/lib/ld-linux-x86-64.so.2 \
--set-rpath "${pkgs.lib.makeLibraryPath buildInputs}:"'$ORIGIN/../lib' \
"$binary"
fi
done
find $out/lib -type f -maxdepth 1 | while read binary; do
if file "$binary" | grep -q "ELF"
then
echo "Patching $binary"
patchelf \
--set-rpath "${pkgs.lib.makeLibraryPath buildInputs}:"'$ORIGIN/../lib' \
"$binary"
fi
done
runHook postInstall
'';
installPhase = ''
mkdir -p $out
for i in $(find . -type d -maxdepth 2 -mindepth 1) ;
do
cp -r "$i/." $out/.
done
wrapProgram $out/bin/cargo \
--set CC_x86_64_unknown_linux_musl zig-cc-amd64-linux \
--set CC_aarch64_unknown_linux_musl zig-cc-arm64-linux \
--set CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER zig-cc-amd64-linux \
--set CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER zig-cc-arm64-linux \
--prefix LD_LIBRARY_PATH : "${pkgs.lib.makeLibraryPath buildInputs}"
'';
}