From 8024cd0e4acc76f60b02980b6c6159e682c4a64f Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 27 Jul 2023 22:40:11 -0400 Subject: [PATCH] Copy over the rust tools and start on flake.nix --- Mononix/.obsidian/workspace.json | 15 +---------- flake.nix | 16 ++++++++++++ rust-tools.nix | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 flake.nix create mode 100644 rust-tools.nix diff --git a/Mononix/.obsidian/workspace.json b/Mononix/.obsidian/workspace.json index d81e83d..dd41dc7 100644 --- a/Mononix/.obsidian/workspace.json +++ b/Mononix/.obsidian/workspace.json @@ -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", diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2ad0c82 --- /dev/null +++ b/flake.nix @@ -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; + }; +} diff --git a/rust-tools.nix b/rust-tools.nix new file mode 100644 index 0000000..c05aea6 --- /dev/null +++ b/rust-tools.nix @@ -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 + ''; + }; +} +