build(nix): use crane to build packages

this enables faster incremental rebuilds in nix. it also allows us to
add clippy and cargo test checks more easily. aside from that, i have
tried to carry over things that i think are sensible from the previous
`mkDerivation` call.

the `checkPhase` may as well rather be done in a `installCheckPhase` and
we can set `enableParallelBuilding` to speed up the compilation of
`libbcachefs.a`.
This commit is contained in:
Thomas Mühlbacher 2024-05-28 18:56:08 +02:00
parent ce01c61ba5
commit bf2c0c8852
3 changed files with 95 additions and 79 deletions

View File

@ -1,77 +0,0 @@
{
lib,
stdenv,
pkg-config,
attr,
libuuid,
libsodium,
keyutils,
liburcu,
zlib,
libaio,
udev,
zstd,
lz4,
nix-gitignore,
rustPlatform,
rustc,
cargo,
fuse3,
fuseSupport ? false,
}:
let
src = nix-gitignore.gitignoreSource [ ] ./.;
commit = lib.strings.substring 0 7 (builtins.readFile ./.bcachefs_revision);
version = "git-${commit}";
in
stdenv.mkDerivation {
inherit src version;
pname = "bcachefs-tools";
nativeBuildInputs = [
pkg-config
cargo
rustc
rustPlatform.cargoSetupHook
rustPlatform.bindgenHook
];
buildInputs = [
libaio
keyutils # libkeyutils
lz4 # liblz4
libsodium
liburcu
libuuid
zstd # libzstd
zlib # zlib1g
attr
udev
] ++ lib.optional fuseSupport fuse3;
${if fuseSupport then "BCACHEFS_FUSE" else null} = "1";
cargoRoot = ".";
# when git-based crates are updated, run:
# nix run github:Mic92/nix-update -- --version=skip --flake default
# to update the hashes
cargoDeps = rustPlatform.importCargoLock { lockFile = "${src}/Cargo.lock"; };
makeFlags = [
"DESTDIR=${placeholder "out"}"
"PREFIX="
"VERSION=${commit}"
];
dontStrip = true;
checkPhase = "./target/release/bcachefs version";
doCheck = true;
meta = {
mainProgram = "bcachefs";
license = lib.licenses.gpl2Only;
};
}

21
flake.lock generated
View File

@ -1,5 +1,25 @@
{
"nodes": {
"crane": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1716745752,
"narHash": "sha256-8K1R9Yg4r08rYk86Yq+lu3E9L3uRUb4xMqYHgl0VGS0=",
"owner": "ipetkov",
"repo": "crane",
"rev": "19ca94ec2d288de334ae932107816b4a97736cd8",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": [
@ -85,6 +105,7 @@
},
"root": {
"inputs": {
"crane": "crane",
"fenix": "fenix",
"flake-compat": "flake-compat",
"flake-parts": "flake-parts",

View File

@ -11,6 +11,11 @@
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
@ -29,6 +34,7 @@
flake-parts,
treefmt-nix,
fenix,
crane,
flake-compat,
...
}:
@ -46,18 +52,84 @@
{
self',
config,
lib,
pkgs,
system,
...
}:
let
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
rustfmtToml = builtins.fromTOML (builtins.readFile ./rustfmt.toml);
craneLib = crane.mkLib pkgs;
commit = lib.strings.substring 0 7 (builtins.readFile ./.bcachefs_revision);
commonArgs = {
version = "git-${commit}";
src = self;
makeFlags = [
"DESTDIR=${placeholder "out"}"
"PREFIX="
"VERSION=${commit}"
];
dontStrip = true;
nativeBuildInputs = with pkgs; [
pkg-config
rustPlatform.bindgenHook
];
buildInputs = with pkgs; [
attr
keyutils
libaio
libsodium
liburcu
libuuid
lz4
udev
zlib
zstd
];
};
cargoArtifacts = craneLib.buildDepsOnly (commonArgs // { pname = cargoToml.package.name; });
in
{
packages.default = config.packages.bcachefs-tools;
packages.bcachefs-tools = pkgs.callPackage ./build.nix { };
packages.bcachefs-tools = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
packages.bcachefs-tools-fuse = config.packages.bcachefs-tools.override { fuseSupport = true; };
enableParallelBuilding = true;
buildPhaseCargoCommand = ''
make ''${enableParallelBuilding:+-j''${NIX_BUILD_CORES}} $makeFlags
'';
installPhaseCommand = ''
make ''${enableParallelBuilding:+-j''${NIX_BUILD_CORES}} $makeFlags install
'';
doInstallCheck = true;
installCheckPhase = ''
runHook preInstallCheck
test "$($out/bin/bcachefs version)" = "${commit}"
runHook postInstallCheck
'';
}
);
packages.bcachefs-tools-fuse = config.packages.bcachefs-tools.overrideAttrs (
final: prev: {
makeFlags = prev.makeFlags ++ [ "BCACHEFS_FUSE=1" ];
buildInputs = prev.buildInputs ++ [ pkgs.fuse3 ];
}
);
devShells.default = pkgs.mkShell {
inputsFrom = [