mononix/rust-tools.nix

45 lines
1.3 KiB
Nix
Raw Normal View History

{ mkDerivation,
rust,
system,
target ? system,
}:
let
normalizeCrateName = name: builtins.replaceStrings ["-"] ["_"] name;
mkDeps = path: crates:
builtins.concatStringsSep " " ["-L ${path} " (builtins.concatStringsSep " " (builtins.map (crate: "--extern ${crate}") crates))];
rust-system = if system == "x86_64-linux" then "x86_64-unknown-linux-gnu" else system;
in {
buildRustLib = { name, src }:
let
stdDeps = mkDeps "${rust.rust-std}/lib/rustlib/${rust-system}/lib" [ "std" "core" ];
in mkDerivation {
name = name;
src = src;
dontUnpack = true;
buildPhase = ''
mkdir -p $out/lib
${rust.rustc}/bin/rustc -o $out/lib/lib${name}.rlib ${stdDeps} --crate-type lib ${src}/src/lib.rs
'';
};
buildRustApp = { name, src, buildDependencies }:
let
stdDeps = mkDeps "${rust.rust-std}/lib/rustlib/${rust-system}/lib" [ "std" "core" ];
rustDeps = builtins.concatStringsSep " " (builtins.map (dep: "--extern ${normalizeCrateName dep.name}=${dep.out}/lib/lib${dep.name}.rlib") buildDependencies);
in mkDerivation {
name = name;
src = src;
dontUnpack = true;
buildPhase = ''
mkdir -p $out/bin
${rust.rustc}/bin/rustc -o $out/bin/${name} ${stdDeps} ${rustDeps} ${src}/src/main.rs
'';
};
}