nixos/templates/rust/flake.nix

131 lines
3.4 KiB
Nix
Raw Normal View History

2025-05-03 23:23:02 +08:00
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils = {
url = "github:numtide/flake-utils";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
2025-05-14 03:37:58 +08:00
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
crane = {
url = "github:ipetkov/crane";
};
2025-05-03 23:23:02 +08:00
};
outputs = {
2025-05-14 03:37:58 +08:00
self,
2025-05-03 23:23:02 +08:00
flake-utils,
nixpkgs,
rust-overlay,
2025-05-14 03:37:58 +08:00
advisory-db,
crane,
2025-05-03 23:23:02 +08:00
...
}:
flake-utils.lib.eachDefaultSystem (system: let
2025-05-14 03:37:58 +08:00
inherit (pkgs) lib;
2025-05-03 23:23:02 +08:00
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
2025-05-14 03:37:58 +08:00
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
srcFilters = path: type:
builtins.any (suffix: lib.hasSuffix suffix path) [
".sql"
".diff"
".md"
".adoc"
".json"
]
|| (craneLib.filterCargoSources path type);
src = lib.cleanSourceWith {
src = ./.;
filter = srcFilters;
2025-05-03 23:23:02 +08:00
};
2025-05-14 03:37:58 +08:00
basicArgs = {
inherit src;
strictDeps = true;
};
2025-05-25 15:38:17 +08:00
nativeBuildInputs = with pkgs; [];
buildInputs =
(with pkgs; [])
++ lib.optional pkgs.stdenv.buildPlatform.isDarwin (with pkgs; [
darwin.apple_sdk.frameworks.Security
]);
cargoArtifacts = craneLib.buildDepsOnly (basicArgs
// {
inherit buildInputs nativeBuildInputs;
});
2025-05-14 03:37:58 +08:00
commonArgs =
basicArgs
// {
2025-05-25 15:38:17 +08:00
inherit cargoArtifacts buildInputs nativeBuildInputs;
2025-05-14 03:37:58 +08:00
};
2025-05-03 23:23:02 +08:00
in {
2025-05-14 03:37:58 +08:00
formatter = pkgs.alejandra;
checks = {
2025-05-25 15:38:17 +08:00
package = self.packages.${system}.default.overrideAttrs {
doCheck = true;
};
2025-05-14 03:37:58 +08:00
clippy = craneLib.cargoClippy (commonArgs
// {
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
doc = craneLib.cargoDoc commonArgs;
deny = craneLib.cargoDeny commonArgs;
fmt = craneLib.cargoFmt basicArgs;
audit = craneLib.cargoAudit {
inherit src advisory-db;
};
nextest = craneLib.cargoNextest (commonArgs
// {
partitions = 1;
partitionType = "count";
cargoNextestExtraArgs = "--no-tests pass";
env = {
CARGO_PROFILE = "dev";
};
});
};
packages = rec {
rust-demo = craneLib.buildPackage (commonArgs
// {
inherit
(craneLib.crateNameFromCargoToml {
cargoToml = "${toString src}/Cargo.toml";
})
2025-05-25 15:38:17 +08:00
pname
version
2025-05-14 03:37:58 +08:00
;
doCheck = false;
});
default = rust-demo;
};
apps.default = flake-utils.lib.mkApp {
drv = self.packages."${system}".default;
};
devShells.default = craneLib.devShell {
2025-05-25 15:38:17 +08:00
checks = self.checks.${system};
2025-05-03 23:23:02 +08:00
packages = with pkgs; [
rust-analyzer
2025-05-14 03:37:58 +08:00
cargo-audit
cargo-deny
cargo-watch
cargo-nextest
2025-05-03 23:23:02 +08:00
];
2025-05-14 03:37:58 +08:00
env = {
RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";
};
2025-05-03 23:23:02 +08:00
};
});
2025-05-14 03:37:58 +08:00
nixConfig = {
keepOutputs = true;
};
2025-05-03 23:23:02 +08:00
}