template-nix-rust/flake.nix

137 lines
3.7 KiB
Nix
Raw Normal View History

2024-10-21 23:02:58 +02:00
{
inputs = {
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils";
nix-filter.url = "github:numtide/nix-filter";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
rust-overlay.url = "github:oxalica/rust-overlay";
2024-10-21 23:41:27 +02:00
treefmt-nix.url = "github:numtide/treefmt-nix";
2024-10-21 23:02:58 +02:00
};
outputs =
{
self,
advisory-db,
crane,
flake-utils,
nix-filter,
nixpkgs,
rust-overlay,
2024-10-21 23:41:27 +02:00
treefmt-nix,
2024-10-21 23:02:58 +02:00
}:
flake-utils.lib.eachDefaultSystem (
system:
let
overlays = [
(import rust-overlay)
(final: prev: {
nix-filter = nix-filter.lib;
rust-toolchain = pkgs.rust-bin.stable.latest.default;
rust-dev-toolchain = pkgs.rust-toolchain.override {
extensions = [
"rust-analyzer"
"rust-src"
];
};
})
];
pkgs = import nixpkgs { inherit overlays system; };
craneLib = (crane.mkLib pkgs).overrideToolchain pkgs.rust-toolchain;
stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv;
nativeBuildInputs = with pkgs; [
clang_18
mold
];
sourceAndFixtures = path: type: (craneLib.filterCargoSources path type);
src = pkgs.lib.cleanSourceWith {
src = ./.;
filter = sourceAndFixtures;
};
commonArgs = {
2024-10-21 23:41:27 +02:00
inherit src stdenv nativeBuildInputs;
2024-10-21 23:02:58 +02:00
strictDeps = true;
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
2024-10-21 23:41:27 +02:00
treefmtEval = treefmt-nix.lib.evalModule pkgs {
projectRootFile = "flake.nix";
programs = {
nixfmt.enable = true;
nixfmt.package = pkgs.nixfmt-rfc-style;
shfmt.enable = true;
rustfmt.enable = true;
};
settings.formatter.rustfmt.command = pkgs.lib.mkForce "${pkgs.rust-toolchain}/bin/rustfmt";
};
my-project = craneLib.buildPackage (commonArgs // { inherit cargoArtifacts; });
2024-10-21 23:02:58 +02:00
in
rec {
# `nix build`
packages.default = my-project;
# Tests
checks = {
clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
fmt = craneLib.cargoFmt (commonArgs // { inherit src; });
audit = craneLib.cargoAudit (commonArgs // { inherit src advisory-db; });
nextest = craneLib.cargoNextest (
commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
}
);
};
2024-10-21 23:41:27 +02:00
# For `nix fmt`
formatter = treefmtEval.config.build.wrapper;
2024-10-21 23:02:58 +02:00
# `nix develop`
devShells.default = pkgs.mkShell {
inherit nativeBuildInputs;
buildInputs = with pkgs; [
bacon
cargo-edit
cargo-features-manager
cargo-machete
cargo-nextest
cargo-watch
hyperfine
just
rust-dev-toolchain
watchexec
];
shellHook = ''
export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc} # Required for rust-analyzer
echo
echo " Welcome to the My Project development environment! "
echo "Run 'just' to see available commands."
echo
'';
};
}
);
}