|
| 1 | +{ |
| 2 | + description = "Git repository summary on your terminal"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; |
| 6 | + |
| 7 | + crane.url = "github:ipetkov/crane"; |
| 8 | + |
| 9 | + flake-utils.url = "github:numtide/flake-utils"; |
| 10 | + |
| 11 | + advisory-db = { |
| 12 | + url = "github:rustsec/advisory-db"; |
| 13 | + flake = false; |
| 14 | + }; |
| 15 | + }; |
| 16 | + |
| 17 | + outputs = { self, nixpkgs, crane, flake-utils, advisory-db, ... }: |
| 18 | + flake-utils.lib.eachDefaultSystem (system: |
| 19 | + let |
| 20 | + pkgs = nixpkgs.legacyPackages.${system}; |
| 21 | + |
| 22 | + inherit (pkgs) lib; |
| 23 | + |
| 24 | + craneLib = crane.mkLib pkgs; |
| 25 | + src = craneLib.cleanCargoSource ./.; |
| 26 | + |
| 27 | + # Common arguments can be set here to avoid repeating them later |
| 28 | + commonArgs = { |
| 29 | + inherit src; |
| 30 | + strictDeps = true; |
| 31 | + |
| 32 | + buildInputs = [ |
| 33 | + # Add additional build inputs here |
| 34 | + ] ++ lib.optionals pkgs.stdenv.isDarwin [ |
| 35 | + # Additional darwin specific inputs can be set here |
| 36 | + pkgs.libiconv |
| 37 | + ]; |
| 38 | + |
| 39 | + # Additional environment variables can be set directly |
| 40 | + # MY_CUSTOM_VAR = "some value"; |
| 41 | + }; |
| 42 | + |
| 43 | + # Build *just* the cargo dependencies, so we can reuse |
| 44 | + # all of that work (e.g. via cachix) when running in CI |
| 45 | + cargoArtifacts = craneLib.buildDepsOnly commonArgs; |
| 46 | + |
| 47 | + # Build the actual crate itself, reusing the dependency |
| 48 | + # artifacts from above. |
| 49 | + my-crate = craneLib.buildPackage (commonArgs // { |
| 50 | + inherit cargoArtifacts; |
| 51 | + }); |
| 52 | + in |
| 53 | + { |
| 54 | + checks = { |
| 55 | + # Build the crate as part of `nix flake check` for convenience |
| 56 | + inherit my-crate; |
| 57 | + |
| 58 | + # Run clippy (and deny all warnings) on the crate source, |
| 59 | + # again, reusing the dependency artifacts from above. |
| 60 | + # |
| 61 | + # Note that this is done as a separate derivation so that |
| 62 | + # we can block the CI if there are issues here, but not |
| 63 | + # prevent downstream consumers from building our crate by itself. |
| 64 | + my-crate-clippy = craneLib.cargoClippy (commonArgs // { |
| 65 | + inherit cargoArtifacts; |
| 66 | + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; |
| 67 | + }); |
| 68 | + |
| 69 | + my-crate-doc = craneLib.cargoDoc (commonArgs // { |
| 70 | + inherit cargoArtifacts; |
| 71 | + }); |
| 72 | + |
| 73 | + # Check formatting |
| 74 | + my-crate-fmt = craneLib.cargoFmt { |
| 75 | + inherit src; |
| 76 | + }; |
| 77 | + |
| 78 | + my-crate-toml-fmt = craneLib.taploFmt { |
| 79 | + src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ]; |
| 80 | + # taplo arguments can be further customized below as needed |
| 81 | + # taploExtraArgs = "--config ./taplo.toml"; |
| 82 | + }; |
| 83 | + |
| 84 | + # Audit dependencies |
| 85 | + my-crate-audit = craneLib.cargoAudit { |
| 86 | + inherit src advisory-db; |
| 87 | + }; |
| 88 | + |
| 89 | + # Audit licenses |
| 90 | + my-crate-deny = craneLib.cargoDeny { |
| 91 | + inherit src; |
| 92 | + }; |
| 93 | + |
| 94 | + # Run tests with cargo-nextest |
| 95 | + # Consider setting `doCheck = false` on `my-crate` if you do not want |
| 96 | + # the tests to run twice |
| 97 | + my-crate-nextest = craneLib.cargoNextest (commonArgs // { |
| 98 | + inherit cargoArtifacts; |
| 99 | + partitions = 1; |
| 100 | + partitionType = "count"; |
| 101 | + cargoNextestPartitionsExtraArgs = "--no-tests=pass"; |
| 102 | + }); |
| 103 | + }; |
| 104 | + |
| 105 | + packages = { |
| 106 | + default = my-crate; |
| 107 | + }; |
| 108 | + |
| 109 | + apps.default = flake-utils.lib.mkApp { |
| 110 | + drv = my-crate; |
| 111 | + }; |
| 112 | + |
| 113 | + devShells.default = craneLib.devShell { |
| 114 | + # Inherit inputs from checks. |
| 115 | + checks = self.checks.${system}; |
| 116 | + |
| 117 | + # Additional dev-shell environment variables can be set directly |
| 118 | + # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; |
| 119 | + |
| 120 | + # Extra inputs can be added here; cargo and rustc are provided by default. |
| 121 | + packages = [ |
| 122 | + # pkgs.ripgrep |
| 123 | + ]; |
| 124 | + }; |
| 125 | + }); |
| 126 | +} |
0 commit comments