template-nix-rust/flake.nix

133 lines
3.3 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";
};
outputs =
{
self,
advisory-db,
crane,
flake-utils,
nix-filter,
nixpkgs,
rust-overlay,
}:
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 = {
inherit
src
stdenv
nativeBuildInputs
;
strictDeps = true;
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
my-project = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
}
);
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";
}
);
};
# Formatter for the Nix code
formatter = pkgs.nixpkgs-fmt;
# `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
'';
};
}
);
}