diff --git a/flake.lock b/flake.lock index e328700..ff69fb5 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "crane": { + "locked": { + "lastModified": 1785782307, + "narHash": "sha256-MPaRdVkf6zZP5fCPxYCi8Dr4pZzgmXzg8T9nVEbp3Mw=", + "owner": "ipetkov", + "repo": "crane", + "rev": "2c71e194474d13de031d729b729c968ddbe3507f", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" @@ -36,6 +51,7 @@ }, "root": { "inputs": { + "crane": "crane", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } diff --git a/flake.nix b/flake.nix index 5633e8c..827d996 100644 --- a/flake.nix +++ b/flake.nix @@ -2,6 +2,8 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + crane.url = "github:ipetkov/crane"; + flake-utils.url = "github:numtide/flake-utils"; }; @@ -12,6 +14,7 @@ let pkgs = import inputs.nixpkgs { inherit system; }; lib = pkgs.lib; + craneLib = inputs.crane.mkLib pkgs; cargoToml = fromTOML (builtins.readFile ./Cargo.toml); name = cargoToml.package.name; @@ -44,6 +47,12 @@ src = ./.; }; + # Build *just* the cargo dependencies, + # to reuse them for build and test derivations. + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + # Build the actual crate itself, + # reusing the dependency artifacts from above. crate = let desktopItem = pkgs.makeDesktopItem { @@ -53,11 +62,10 @@ exec = name; }; in - pkgs.rustPlatform.buildRustPackage ( + craneLib.buildPackage ( commonArgs // { - inherit name; - cargoLock.lockFile = ./Cargo.lock; + inherit cargoArtifacts; nativeBuildInputs = commonArgs.nativeBuildInputs ++ [ pkgs.autoPatchelfHook @@ -81,18 +89,22 @@ { packages.default = crate; - devShells.default = pkgs.mkShell { + checks = { + crate-clippy = craneLib.cargoClippy ( + commonArgs + // { + inherit cargoArtifacts; + + cargoClippyExtraArgs = "-- --deny warnings"; + } + ); + }; + + devShells.default = craneLib.devShell { LD_LIBRARY_PATH = lib.makeLibraryPath dlDeps; inputsFrom = [ crate ]; - - packages = with pkgs; [ - rustc - cargo - clippy - rustfmt - rust-analyzer - ]; + packages = [ pkgs.rust-analyzer ]; }; } );