From bf2c0c88528e6e6a2ffd47fe6804ec97c4708804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= Date: Tue, 28 May 2024 18:56:08 +0200 Subject: [PATCH 1/5] 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`. --- build.nix | 77 ------------------------------------------------------ flake.lock | 21 +++++++++++++++ flake.nix | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 79 deletions(-) delete mode 100644 build.nix diff --git a/build.nix b/build.nix deleted file mode 100644 index 4927519c..00000000 --- a/build.nix +++ /dev/null @@ -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; - }; -} diff --git a/flake.lock b/flake.lock index 6cd43325..47820499 100644 --- a/flake.lock +++ b/flake.lock @@ -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", diff --git a/flake.nix b/flake.nix index 6d946dbc..291d10f9 100644 --- a/flake.nix +++ b/flake.nix @@ -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 = [ From 422ae1f1a70c5c97bd26756d0f61ad011ccea87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= Date: Tue, 28 May 2024 19:03:36 +0200 Subject: [PATCH 2/5] build(nix): add cargo clippy check note that this only checks the top-level package, `bch_bindgen` is excluded because it has too many clippy lints. --- flake.nix | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/flake.nix b/flake.nix index 291d10f9..c6f5e6aa 100644 --- a/flake.nix +++ b/flake.nix @@ -131,6 +131,14 @@ } ); + checks.cargo-clippy = craneLib.cargoClippy ( + commonArgs + // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + } + ); + devShells.default = pkgs.mkShell { inputsFrom = [ config.packages.default From b362f12add2b7307cf404ead3b0c4fc75b43422e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= Date: Tue, 28 May 2024 19:04:47 +0200 Subject: [PATCH 3/5] build(nix): add `cargo test` check this is pretty much a no-op currently, but hopefully we can make some use of it in the future. --- flake.nix | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/flake.nix b/flake.nix index c6f5e6aa..fb1e420d 100644 --- a/flake.nix +++ b/flake.nix @@ -139,6 +139,24 @@ } ); + # we have to build our own `craneLib.cargoTest` + checks.cargo-test = craneLib.mkCargoDerivation ( + commonArgs + // { + inherit cargoArtifacts; + doCheck = true; + + enableParallelChecking = true; + + pnameSuffix = "-test"; + buildPhaseCargoCommand = ""; + checkPhaseCargoCommand = '' + make ''${enableParallelChecking:+-j''${NIX_BUILD_CORES}} $makeFlags libbcachefs.a + cargo test --profile release -- --nocapture + ''; + } + ); + devShells.default = pkgs.mkShell { inputsFrom = [ config.packages.default From 7f156855177800d89073f57a064ab368f78b5584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= Date: Tue, 28 May 2024 19:06:23 +0200 Subject: [PATCH 4/5] build(nix): minor tweaks - rm unused function argument - `LIBCLANG_PATH` is superfluous here, this is already handled by `rustPlatform.bindgenHook` for us. - rustc is not directly needed in the `$PATH` for the build but it's good to have in the dev shell. --- flake.nix | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/flake.nix b/flake.nix index fb1e420d..3cd44ec8 100644 --- a/flake.nix +++ b/flake.nix @@ -35,7 +35,6 @@ treefmt-nix, fenix, crane, - flake-compat, ... }: flake-parts.lib.mkFlake { inherit inputs; } { @@ -163,8 +162,6 @@ config.treefmt.build.devShell ]; - LIBCLANG_PATH = "${pkgs.clang.cc.lib}/lib"; - # here go packages that aren't required for builds but are used for # development, and might need to be version matched with build # dependencies (e.g. clippy or rust-analyzer). @@ -174,6 +171,7 @@ clang-tools clippy rust-analyzer + rustc ]; }; From d43fbfd642d4271f1fbae4e16fbc133fd006125e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= Date: Tue, 28 May 2024 19:25:13 +0200 Subject: [PATCH 5/5] docs: rm outdated msrv in install instructions --- INSTALL.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 99c33776..977f6456 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -17,8 +17,9 @@ Build dependencies: * zlib1g In addition a recent Rust toolchain is required (rustc, cargo), either by using -[rustup](https://rustup.rs/) or make sure to use a distribution where rustc (>=1.65) -is available. +[rustup](https://rustup.rs/) or make sure to use a distribution where a recent +enough rustc is available. Please check `rust-version` in `Cargo.toml` to see +the minimum supported Rust version (MSRV). ``` shell curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path