Use cargo crane in Nix flake and set up cachix action (#539)

This commit is contained in:
Benjamin Grosse
2025-10-25 23:44:19 +01:00
committed by GitHub
parent 0ddded3b8b
commit 1ec311590d
4 changed files with 154 additions and 61 deletions

108
flake.nix
View File

@@ -5,49 +5,99 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
crane.url = "github:ipetkov/crane";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
crane,
flake-utils,
rust-overlay,
fenix,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
# We only need the nightly overlay in the devShell because .rs files are formatted with nightly.
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
rustNightly = pkgs.rust-bin.nightly."2025-08-31".default;
in
with pkgs;
{
packages.default = rustPlatform.buildRustPackage {
pname = "iamb";
version = self.shortRev or self.dirtyShortRev;
src = ./.;
cargoLock = {
lockFile = ./Cargo.lock;
};
nativeBuildInputs = [ pkg-config ];
buildInputs = [ openssl ];
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
rustToolchain = fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
# When the file changes, this hash must be updated.
sha256 = "sha256-Hn2uaQzRLidAWpfmRwSRdImifGUCAb9HeAqTYFXWeQk=";
};
devShell = mkShell {
buildInputs = [
(rustNightly.override {
extensions = [
"rust-src"
"rust-analyzer-preview"
"rustfmt"
"clippy"
];
})
pkg-config
# Nightly toolchain for rustfmt (pinned to current flake lock)
# Note that the github CI uses "current nightly" for formatting, it 's not pinned.
rustNightly = fenix.packages.${system}.latest.toolchain;
rustNightlyFmt = fenix.packages.${system}.latest.rustfmt;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
craneLibNightly = (crane.mkLib pkgs).overrideToolchain rustNightly;
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
(craneLib.fileset.commonCargoSources ./.)
./src/windows/welcome.md
];
};
commonArgs = {
inherit src;
strictDeps = true;
pname = "iamb";
version = self.shortRev or self.dirtyShortRev;
};
# 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
iamb = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
});
in
{
checks = {
# Build the crate as part of `nix flake check`
inherit iamb;
iamb-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
iamb-fmt = craneLibNightly.cargoFmt {
inherit src;
};
iamb-nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
});
};
packages.default = iamb;
apps.default = flake-utils.lib.mkApp {
drv = iamb;
};
devShells.default = craneLib.devShell {
# Inherit inputs from checks
checks = self.checks.${system};
packages = with pkgs; [
rustNightlyFmt
cargo-tarpaulin
cargo-watch
sqlite