Copy over the rust tools and start on flake.nix

This commit is contained in:
Savanni D'Gerinel 2023-07-27 22:40:11 -04:00
parent ea75073147
commit 8024cd0e4a
3 changed files with 61 additions and 14 deletions

View File

@ -33,19 +33,6 @@
}
}
},
{
"id": "4d0f931be6c172b5",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Nix For Development.md",
"mode": "source",
"backlinks": true,
"source": false
}
}
},
{
"id": "bd24ffd92b4cc242",
"type": "leaf",
@ -192,11 +179,11 @@
"active": "f8131571839b6055",
"lastOpenFiles": [
"Nix For Development.md",
"cargo2nix.md",
"Index.md",
"Nix Monorepos.md",
"Rust build tools in Nix.md",
"Nix should immediately make a person's time better.md",
"cargo2nix.md",
"Nix Flakes.md",
"buildRustPackage.md",
"buildRustCrate.md",

16
flake.nix Normal file
View File

@ -0,0 +1,16 @@
{
description = "Monorepo build tools for Nix";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, ... }:
rust-tools = import ./rust-tools.nix {
inherit (pkgs.stdenv) mkDerivation;
};
in
{
rust-tools = rust-tools;
};
}

44
rust-tools.nix Normal file
View File

@ -0,0 +1,44 @@
{ 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
'';
};
}