|
| 1 | +{ |
| 2 | + description = "Samply"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; |
| 6 | + |
| 7 | + crane = { |
| 8 | + url = "github:ipetkov/crane"; |
| 9 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 10 | + }; |
| 11 | + |
| 12 | + flake-utils.url = "github:numtide/flake-utils"; |
| 13 | + |
| 14 | + advisory-db = { |
| 15 | + url = "github:rustsec/advisory-db"; |
| 16 | + flake = false; |
| 17 | + }; |
| 18 | + |
| 19 | + rust-overlay = { |
| 20 | + url = "github:oxalica/rust-overlay"; |
| 21 | + inputs = { |
| 22 | + nixpkgs.follows = "nixpkgs"; |
| 23 | + flake-utils.follows = "flake-utils"; |
| 24 | + }; |
| 25 | + }; |
| 26 | + |
| 27 | + }; |
| 28 | + |
| 29 | + outputs = { self, nixpkgs, crane, flake-utils, advisory-db, ... }@inputs: |
| 30 | + flake-utils.lib.eachSystem [ "aarch64-darwin" "x86_64-darwin" "aarch64-linux" "x86_64-linux" ] (system: |
| 31 | + let |
| 32 | + pkgs = import nixpkgs { |
| 33 | + inherit system; |
| 34 | + overlays = [ |
| 35 | + (import inputs.rust-overlay) |
| 36 | + ]; |
| 37 | + }; |
| 38 | + |
| 39 | + inherit (pkgs) lib; |
| 40 | + |
| 41 | + |
| 42 | + toolchain-settings = { |
| 43 | + extensions = [ "rust-src" ]; |
| 44 | + targets = [ "aarch64-apple-darwin" "x86_64-apple-darwin" "aarch64-unknown-linux-gnu" "x86_64-unknown-linux-gnu" ]; |
| 45 | + }; |
| 46 | + # stable |
| 47 | + rust-toolchain = pkgs.rust-bin.stable.latest.default.override toolchain-settings; |
| 48 | + # nightly |
| 49 | + # rust-toolchain = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override toolchain-settings); |
| 50 | + |
| 51 | + craneLib = (crane.mkLib pkgs).overrideToolchain rust-toolchain; |
| 52 | + |
| 53 | + src = craneLib.cleanCargoSource ./.; |
| 54 | + |
| 55 | + buildInputs = with pkgs; [ |
| 56 | + # Add additional build inputs here |
| 57 | + openssl |
| 58 | + pkg-config |
| 59 | + ]; |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + # Build *just* the cargo dependencies, so we can reuse |
| 64 | + # all of that work (e.g. via cachix) when running in CI |
| 65 | + cargoArtifacts = craneLib.buildDepsOnly { |
| 66 | + inherit src buildInputs; |
| 67 | + }; |
| 68 | + |
| 69 | + # Build the actual crate itself, reusing the dependency |
| 70 | + # artifacts from above. |
| 71 | + samply = craneLib.buildPackage { |
| 72 | + inherit cargoArtifacts src buildInputs; |
| 73 | + pname = "samply"; |
| 74 | + cargoExtraArgs = "--bin samply"; |
| 75 | + }; |
| 76 | + in |
| 77 | + { |
| 78 | + checks = { |
| 79 | + # Build the crate as part of `nix flake check` for convenience |
| 80 | + inherit samply; |
| 81 | + |
| 82 | + # Run clippy (and deny all warnings) on the crate source, |
| 83 | + # again, resuing the dependency artifacts from above. |
| 84 | + # |
| 85 | + # Note that this is done as a separate derivation so that |
| 86 | + # we can block the CI if there are issues here, but not |
| 87 | + # prevent downstream consumers from building our crate by itself. |
| 88 | + cargo-clippy = craneLib.cargoClippy { |
| 89 | + inherit cargoArtifacts src buildInputs; |
| 90 | + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; |
| 91 | + }; |
| 92 | + |
| 93 | + cargo-doc = craneLib.cargoDoc { |
| 94 | + inherit cargoArtifacts src buildInputs; |
| 95 | + }; |
| 96 | + |
| 97 | + # Check formatting |
| 98 | + cargo-fmt = craneLib.cargoFmt { |
| 99 | + inherit src; |
| 100 | + }; |
| 101 | + |
| 102 | + # Audit dependencies |
| 103 | + cargo-audit = craneLib.cargoAudit { |
| 104 | + inherit src advisory-db; |
| 105 | + }; |
| 106 | + |
| 107 | + # Run tests with cargo-nextest |
| 108 | + # Consider setting `doCheck = false` on `samply` if you do not want |
| 109 | + # the tests to run twice |
| 110 | + cargo-nextest = craneLib.cargoNextest { |
| 111 | + inherit cargoArtifacts src buildInputs; |
| 112 | + partitions = 1; |
| 113 | + partitionType = "count"; |
| 114 | + }; |
| 115 | + } // lib.optionalAttrs (system == "x86_64-linux") { |
| 116 | + # NB: cargo-tarpaulin only supports x86_64 systems |
| 117 | + # Check code coverage (note: this will not upload coverage anywhere) |
| 118 | + cargo-coverage = craneLib.cargoTarpaulin { |
| 119 | + inherit cargoArtifacts src; |
| 120 | + }; |
| 121 | + }; |
| 122 | + |
| 123 | + packages.default = samply; |
| 124 | + |
| 125 | + apps.default = flake-utils.lib.mkApp { |
| 126 | + drv = samply; |
| 127 | + }; |
| 128 | + |
| 129 | + overlays = final: prev: { |
| 130 | + inherit samply; |
| 131 | + }; |
| 132 | + |
| 133 | + devShells.default = pkgs.mkShell { |
| 134 | + inputsFrom = builtins.attrValues self.checks; |
| 135 | + |
| 136 | + packages = with pkgs; [ |
| 137 | + rust-toolchain |
| 138 | + rust-analyzer |
| 139 | + ] ++ buildInputs; |
| 140 | + |
| 141 | + }; |
| 142 | + } |
| 143 | + ); |
| 144 | +} |
0 commit comments