about summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--templates/rust/flake.nix59
1 files changed, 32 insertions, 27 deletions
diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix
index b6bc262..fdce743 100644
--- a/templates/rust/flake.nix
+++ b/templates/rust/flake.nix
@@ -11,6 +11,10 @@
         nixpkgs.follows = "nixpkgs";
       };
     };
+    crane = {
+      url = "github:ipetkov/crane";
+      inputs.nixpkgs.follows = "nixpkgs";
+    };
     pre-commit-hooks = {
       url = "github:cachix/pre-commit-hooks.nix";
       inputs = {
@@ -25,43 +29,45 @@
     , flake-utils
     , nixpkgs
     , rust-overlay
+    , crane
     , pre-commit-hooks
     }:
-    let
-      # Borrow project metadata from the Rust config
-      meta = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package;
-      inherit (meta) name version;
 
-      overlays = [
-        # Rust helpers
-        (import rust-overlay)
-        # Build Rust toolchain using helpers from rust-overlay
-        (self: super: {
-          # This supplies cargo, rustc, rustfmt, etc.
-          rustToolchain = super.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
-        })
-      ];
-    in
     flake-utils.lib.eachDefaultSystem
       (system:
       let
-        pkgs = import nixpkgs { inherit system overlays; };
+        pkgs = import nixpkgs {
+          inherit system;
+          overlays = [ (import rust-overlay) ];
+        };
+        rust-toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
+
+        craneLib = (crane.mkLib pkgs).overrideScope' (_: _: {
+          cargo = rust-toolchain;
+          clippy = rust-toolchain;
+          rustc = rust-toolchain;
+          rustfmt = rust-toolchain;
+        });
+
+        src = ./.;
+
+        cargoArtifacts = craneLib.buildDepsOnly {
+          inherit src;
+        };
+
+        my-crate = craneLib.buildPackage {
+          inherit cargoArtifacts src;
+        };
       in
       {
-        packages = rec {
-          default = XXX;
-          x509-info = pkgs.rustPlatform.buildRustPackage {
-            pname = name;
-            inherit version;
-            src = ./.;
-            release = true;
-            cargoLock.lockFile = ./Cargo.lock;
-          };
+        packages.default = my-crate;
+        apps.default = flake-utils.lib.mkApp {
+          drv = my-crate;
         };
 
         checks = {
           pre-commit = pre-commit-hooks.lib.${system}.run {
-            src = ./.;
+            inherit src;
             hooks = {
               clippy = {
                 enable = true;
@@ -80,8 +86,7 @@
 
         devShell = pkgs.mkShell {
           nativeBuildInputs = with pkgs; [
-            rustToolchain
-            cargo-audit
+            rust-toolchain
             cargo-deny
             rust-analyzer
           ];