From de00301c399c3c7dbc18ead8979704595614587a Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Tue, 4 Jun 2024 22:30:18 +0200 Subject: [PATCH] Enhance flake with more robust setup --- flake.nix | 56 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/flake.nix b/flake.nix index df6c83c..6043b12 100644 --- a/flake.nix +++ b/flake.nix @@ -67,39 +67,43 @@ ]; }; - my-crate = craneLib.buildPackage ( + # Build *just* the cargo dependencies, so we can reuse + # all of that work (e.g. via cachix) when running in CI + cargoArtifacts = craneLib.buildDepsOnly (commonArgs); + + # Run clippy (and deny all warnings) on the crate source, + # reusing the dependency artifacts (e.g. from build scripts or + # proc-macros) from above. + # + # Note that this is done as a separate derivation so it + # does not impact building just the crate by itself. + myCrateClippy = craneLib.cargoClippy ( commonArgs // { - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - - # Additional environment variables or build phases/hooks can be set - # here *without* rebuilding all dependency crates - # MY_CUSTOM_VAR = "some value"; + # Again we apply some extra arguments only to this derivation + # and not every where else. In this case we add some clippy flags + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; } ); + + # Build the actual crate itself, reusing the dependency + # artifacts from above. + myCrate = craneLib.buildPackage (commonArgs // { inherit cargoArtifacts; }); + + # Also run the crate tests under cargo-tarpaulin so that we can keep + # track of code coverage + myCrateCoverage = craneLib.cargoTarpaulin (commonArgs // { inherit cargoArtifacts; }); in { + packages.default = myCrate; checks = { - inherit my-crate; - }; - - packages.default = my-crate; - - apps.default = flake-utils.lib.mkApp { drv = my-crate; }; - - devShells.default = craneLib.devShell { - # Inherit inputs from checks. - checks = self.checks.${system}; - - # Additional dev-shell environment variables can be set directly - # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; - - # Extra inputs can be added here; cargo and rustc are provided by default. - packages = with pkgs; [ - # rustPackages.clippy - # rustfmt - # pre-commit - ]; + inherit + # Build the crate as part of `nix flake check` for convenience + myCrate + myCrateClippy + myCrateCoverage + ; }; } );