nixos-config/template/rust/flake.nix

118 lines
3.7 KiB
Nix
Raw Normal View History

2023-10-09 10:44:07 -07:00
{
inputs = {
2023-11-23 22:34:06 -08:00
crane.url = "github:ipetkov/crane";
2023-10-09 10:44:07 -07:00
flake-utils.url = "github:numtide/flake-utils";
fenix.url = "github:nix-community/fenix";
2023-11-23 22:34:06 -08:00
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
advisory-db = {
flake = false;
url = "github:rustsec/advisory-db";
};
2023-10-09 10:44:07 -07:00
};
2023-11-23 22:34:06 -08:00
outputs = { self, crane, fenix, flake-utils, nixpkgs, advisory-db }:
2023-10-09 10:44:07 -07:00
flake-utils.lib.eachDefaultSystem (system:
2023-11-23 22:34:06 -08:00
let
pkgs = nixpkgs.legacyPackages.${system};
2024-01-13 16:57:25 +08:00
fenixPkgs = fenix.packages.${system};
2023-11-23 22:34:06 -08:00
craneLib = crane.lib.${system};
2024-02-08 20:39:53 -08:00
src = craneLib.path ./.;
2023-11-23 22:34:06 -08:00
# Common arguments can be set here to avoid repeating them later
2024-02-08 20:39:53 -08:00
commonArgs = with pkgs; {
2023-11-23 22:34:06 -08:00
inherit src;
strictDeps = true;
buildInputs = [
# Add additional build inputs here
2024-02-08 20:39:53 -08:00
] ++ lib.optionals stdenv.isDarwin [
2023-11-23 22:34:06 -08:00
# Additional darwin specific inputs can be set here
2024-02-08 20:39:53 -08:00
libiconv
2023-11-23 22:34:06 -08:00
];
# Additional environment variables can be set directly
# MY_CUSTOM_VAR = "some value";
};
2024-02-08 20:39:53 -08:00
craneLibLLvmTools = craneLib.overrideToolchain fenixPkgs.complete;
2023-11-23 22:34:06 -08:00
# 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.
crate = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
});
in
{
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit crate;
# Run clippy (and deny all warnings) on the crate source,
# again, resuing 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.
clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
doc = craneLib.cargoDoc (commonArgs // {
inherit cargoArtifacts;
});
# Check formatting
fmt = craneLib.cargoFmt {
inherit src;
};
# Audit dependencies
audit = craneLib.cargoAudit {
inherit src advisory-db;
};
# Audit licenses
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
nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
});
};
packages = {
default = crate;
llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
inherit cargoArtifacts;
});
};
apps.default = flake-utils.lib.mkApp {
drv = crate;
};
devShells.default = craneLib.devShell {
# Inherit inputs from checks.
# Enable after Cargo.toml and Cargo.lock are present
2024-01-25 10:29:38 -08:00
# Consider customizing deny.toml
2023-11-23 22:34:06 -08:00
# checks = self.checks.${system};
2024-01-25 10:29:38 -08:00
# Extra inputs can be added here; cargo and rustc are provided by default
2023-11-23 22:34:06 -08:00
packages = [
2024-01-13 16:57:25 +08:00
fenixPkgs.rust-analyzer
2023-11-23 22:34:06 -08:00
];
2024-02-08 20:39:53 -08:00
RUST_SRC_PATH = "${fenixPkgs.complete.rust-src}/lib/rustlib/src/rust/library";
2023-10-09 10:44:07 -07:00
};
});
}