From 389e8340e6d666530f354fc9c12f9f0a62f233f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Tue, 14 May 2024 15:44:04 +0200 Subject: [PATCH] feat: crane MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christina Sørensen --- flake.lock | 84 +++++++++++++++++++++++++++++ flake.nix | 154 ++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 220 insertions(+), 18 deletions(-) diff --git a/flake.lock b/flake.lock index a94ae13..44478c4 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,62 @@ { "nodes": { + "advisory-db": { + "flake": false, + "locked": { + "lastModified": 1714183630, + "narHash": "sha256-1BVft7ggSN2XXFeXQjazU3jN9wVECd9qp2mZx/8GDMk=", + "owner": "rustsec", + "repo": "advisory-db", + "rev": "35e7459a331d3e0c585e56dabd03006b9b354088", + "type": "github" + }, + "original": { + "owner": "rustsec", + "repo": "advisory-db", + "type": "github" + } + }, + "crane": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1715274763, + "narHash": "sha256-3Iv1PGHJn9sV3HO4FlOVaaztOxa9uGLfOmUWrH7v7+A=", + "owner": "ipetkov", + "repo": "crane", + "rev": "27025ab71bdca30e7ed0a16c88fd74c5970fc7f5", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": [] + }, + "locked": { + "lastModified": 1715581585, + "narHash": "sha256-/JjvIn1NXW3yOaDcD8Me987/QcXjo+rhg+uThasPAnI=", + "owner": "nix-community", + "repo": "fenix", + "rev": "2c4905096782e8e908205e7fa54ef987fba62793", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": [ @@ -38,11 +95,38 @@ }, "root": { "inputs": { + "advisory-db": "advisory-db", + "crane": "crane", + "fenix": "fenix", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay", "systems": "systems" } }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1715652909, + "narHash": "sha256-aCLEDvzL1j51Rf2mCFOqK1mieMO3pAn5ItCIdr5h2LA=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "1d8fcbbfcfd3476c2665384a46ee9d07ef2b4dd9", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, "systems": { "locked": { "lastModified": 1681028828, diff --git a/flake.nix b/flake.nix index 7c9d6f0..029f674 100644 --- a/flake.nix +++ b/flake.nix @@ -8,38 +8,156 @@ systems.follows = "systems"; }; }; + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; + }; + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.rust-analyzer-src.follows = ""; + }; + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; + }; }; outputs = { + self, flake-utils, nixpkgs, + crane, + rust-overlay, + advisory-db, + fenix, ... }: flake-utils.lib.eachDefaultSystem ( system: let - pkgs = (import nixpkgs) { - inherit system; - }; - in { - packages.default = pkgs.rustPlatform.buildRustPackage { - pname = "since"; - version = "0.0.1"; + pkgs = nixpkgs.legacyPackages.${system}; + inherit system; + inherit (pkgs) lib; + overlays = [ (import rust-overlay) ]; - cargoHash = "sha256-9sA6eHkJAFB4PRX7SG5RQXAGi5RRd8m3cPien/H/1fU="; + craneLib = crane.lib.${system}; + src = craneLib.cleanCargoSource (craneLib.path ./.); - checkFlags = [ - "--skip=utils::tests::parse_timestamp_h" - "--skip=utils::tests::parse_timestamp_hm" - "--skip=utils::tests::parse_timestamp_hms" - ]; + # Common arguments can be set here to avoid repeating them later + commonArgs = { + inherit src; + strictDeps = true; + + cargoTestExtraArgs = + "-- " + + " --skip=utils::tests::parse_timestamp_h" + + " --skip=utils::tests::parse_timestamp_hm" + + " --skip=utils::tests::parse_timestamp_hms"; cargoCheckFlags = ["--show-output"]; - TZ = "CET"; - RUST_BACKTRACE = 1; + buildInputs = [ + # Add additional build inputs here + ] ++ lib.optionals pkgs.stdenv.isDarwin [ + # Additional darwin specific inputs can be set here + pkgs.libiconv + ]; - src = ./.; + # Additional environment variables can be set directly + # MY_CUSTOM_VAR = "some value"; }; - } - ); + + craneLibLLvmTools = craneLib.overrideToolchain + (fenix.packages.${system}.complete.withComponents [ + "cargo" + "llvm-tools" + "rustc" + ]); + + # 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; + + # Build the actual crate itself, reusing the dependency + # artifacts from above. + since = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts; + }); + in { + checks = { + # Build the crate as part of `nix flake check` for convenience + inherit since; + + # Run clippy (and deny all warnings) on the crate source, + # again, reusing the dependency artifacts from above. + # + # Note that this is done as a separate derivation so that + # we can block the CI if there are issues here, but not + # prevent downstream consumers from building our crate by itself. + since-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + + since-doc = craneLib.cargoDoc (commonArgs // { + inherit cargoArtifacts; + }); + + # Check formatting + since-fmt = craneLib.cargoFmt { + inherit src; + }; + + # Audit dependencies + since-audit = craneLib.cargoAudit { + inherit src advisory-db; + }; + + # Audit licenses + since-deny = craneLib.cargoDeny { + inherit src; + }; + + # Run tests with cargo-nextest + # Consider setting `doCheck = false` on `my-crate` if you do not want + # the tests to run twice + since-nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + }); + }; + + packages = { + default = since; + } // lib.optionalAttrs (!pkgs.stdenv.isDarwin) { + since-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // { + inherit cargoArtifacts; + }); + }; + + apps.default = flake-utils.lib.mkApp { + drv = since; + }; + + 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 = [ + # pkgs.ripgrep + ]; + }; + }); }