project: perform clippy/rustfmt checking via a higher order derivation transformer

Instead of patching the derivation in-place via flags, we just have
a higher order function that takes the Rust package derivation and override it
into a Rustfmt / Clippy oriented derivation: it turns off checks and adds its
required dependencies.
This commit is contained in:
Raito Bezarius 2023-05-29 00:09:07 +02:00
parent 5b22893473
commit b77ef07cec
4 changed files with 105 additions and 136 deletions

View File

@ -62,12 +62,14 @@
uefiPkgs = import nixpkgs { uefiPkgs = import nixpkgs {
inherit system; inherit system;
crossSystem = { crossSystem = {
config = "${pkgs.hostPlatform.linuxArch}-windows"; # linuxArch is wrong here, it will yield arm64 instead of aarch64.
rustc.config = "${pkgs.hostPlatform.linuxArch}-unknown-uefi"; config = "${pkgs.hostPlatform.qemuArch}-windows";
rustc.config = "${pkgs.hostPlatform.qemuArch}-unknown-uefi";
libc = null; libc = null;
useLLVM = true; useLLVM = true;
}; };
}; };
utils = import ./nix/packages/utils.nix;
inherit (pkgs) lib; inherit (pkgs) lib;
@ -100,19 +102,21 @@
overlayAttrs = { inherit (config.packages) tool; }; overlayAttrs = { inherit (config.packages) tool; };
checks = let checks =
let
nixosLib = import (pkgs.path + "/nixos/lib") { }; nixosLib = import (pkgs.path + "/nixos/lib") { };
runTest = module: runTest = module:
nixosLib.runTest { nixosLib.runTest {
imports = [ module ]; imports = [ module ];
hostPkgs = pkgs; hostPkgs = pkgs;
}; };
in { in
toolFmt = (tool.override { enableFmt = true; }); {
stubFmt = (stub.override { enableFmt = true; }); stubFmt = uefiPkgs.callPackage (utils.rustfmt stub) { };
toolClippy = (tool.override { enableLint = true; }); toolFmt = pkgs.callPackage (utils.rustfmt tool) { };
stubClippy = (stub.override { enableLint = true; }); toolClippy = pkgs.callPackage (utils.clippy tool) { };
fatStubClippy = (fatStub.override { enableLint = true; }); stubClippy = uefiPkgs.callPackage (utils.clippy stub) { };
fatStubClippy = uefiPkgs.callPackage (utils.clippy fatStub) { };
} // (import ./nix/tests/lanzaboote.nix { } // (import ./nix/tests/lanzaboote.nix {
inherit pkgs; inherit pkgs;
lanzabooteModule = self.nixosModules.lanzaboote; lanzabooteModule = self.nixosModules.lanzaboote;

View File

@ -1,31 +1,15 @@
{ rust, rustPlatform, clippy, rustfmt, stdenv, lib, runCommand, enableFmt ? false, enableLint ? false, fatVariant ? false }: { rustPlatform, stdenv, lib, fatVariant ? false }:
let
targetSpec = rust.toRustTargetSpec stdenv.hostPlatform;
targetIsJSON = lib.hasSuffix ".json" targetSpec;
shortTarget =
if targetIsJSON then
(lib.removeSuffix ".json" (builtins.baseNameOf "${targetSpec}"))
else targetSpec;
in
rustPlatform.buildRustPackage rustPlatform.buildRustPackage
({ {
pname = "lanzaboote_stub"; pname = "lanzaboote_stub";
version = "0.3.0"; version = "0.3.0";
src = runCommand "src" { } '' src = lib.cleanSource ../../rust/stub;
install -D ${../../rust/stub/Cargo.toml} $out/Cargo.toml
install -D ${../../rust/stub/Cargo.lock} $out/Cargo.lock
cp -r ${../../rust/stub/src} $out/src
'';
# We don't want the thin code. # We don't want the thin code.
buildNoDefaultFeatures = true; buildNoDefaultFeatures = true;
buildFeatures = if fatVariant then [ "fat" ] else [ "thin" ]; buildFeatures = if fatVariant then [ "fat" ] else [ "thin" ];
# We don't want the thin code.
buildNoDefaultFeatures = fatVariant;
buildFeatures = lib.optional fatVariant "fat";
cargoLock = { cargoLock = {
lockFile = ../../rust/stub/Cargo.lock; lockFile = ../../rust/stub/Cargo.lock;
}; };
@ -42,25 +26,4 @@ rustPlatform.buildRustPackage
license = licenses.mit; license = licenses.mit;
platforms = [ "x86_64-windows" "aarch64-windows" "i686-windows" ]; platforms = [ "x86_64-windows" "aarch64-windows" "i686-windows" ];
}; };
} // lib.optionalAttrs enableLint { }
buildPhase = ''
cargo clippy --target ${shortTarget} --all-features -- -D warnings
if grep -R 'dbg!' ./src; then
echo "use of dbg macro found in code!"
false
fi
'';
installPhase = ''
touch $out
'';
} // lib.optionalAttrs enableFmt {
buildPhase = ''
echo "checking formatting..."
cargo fmt --all -- --check
'';
installPhase = ''
touch $out
'';
})

View File

@ -1,31 +1,18 @@
{ stdenv { systemd
, systemd
, binutils-unwrapped , binutils-unwrapped
, sbsigntool , sbsigntool
, rustPlatform , rustPlatform
, lib , lib
, runCommand
, fetchurl
, clippy
, rustfmt
, path
, enableLint ? false
, enableFmt ? false
}: }:
rustPlatform.buildRustPackage rustPlatform.buildRustPackage
({ {
pname = "lanzaboote_tool"; pname = "lanzaboote_tool";
version = "0.3.0"; version = "0.3.0";
src = runCommand "src" { } '' src = lib.cleanSource ../../rust/tool;
install -D ${../../rust/tool/Cargo.toml} $out/Cargo.toml
install -D ${../../rust/tool/Cargo.lock} $out/Cargo.lock
cp -r ${../../rust/tool/src} $out/src
'';
TEST_SYSTEMD = systemd; TEST_SYSTEMD = systemd;
nativeBuildInputs = lib.optional enableLint clippy ++ lib.optional enableFmt rustfmt;
cargoLock = { cargoLock = {
lockFile = ../../rust/tool/Cargo.lock; lockFile = ../../rust/tool/Cargo.lock;
}; };
@ -40,28 +27,4 @@ rustPlatform.buildRustPackage
homepage = "https://github.com/nix-community/lanzaboote"; homepage = "https://github.com/nix-community/lanzaboote";
license = licenses.mit; license = licenses.mit;
}; };
} // lib.optionalAttrs enableLint { }
doCheck = false;
buildPhase = ''
cargo clippy --all-targets --all-features -- -D warnings
if grep -R 'dbg!' ./src; then
echo "use of dbg macro found in code!"
false
fi
'';
installPhase = ''
touch $out
'';
} // lib.optionalAttrs enableFmt {
doCheck = false;
buildPhase = ''
echo "checking formatting..."
cargo fmt --all -- --check
'';
installPhase = ''
touch $out
'';
})

39
nix/packages/utils.nix Normal file
View File

@ -0,0 +1,39 @@
{
clippy = rustPackage: { lib, rust, clippy }:
let
targetSpec = rust.toRustTargetSpec rustPackage.stdenv.hostPlatform;
inherit (lib) optionalString concatStringsSep;
in
rustPackage.overrideAttrs (old: {
nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ [ clippy ];
doCheck = false;
buildPhase = ''
echo "checking via clippy..."
cargo clippy --target ${targetSpec} ${optionalString (old.buildNoDefaultFeatures or false) "--no-default-features "}${optionalString ((old.buildFeatures or null) != null) ''--features="${concatStringsSep " " old.buildFeatures}" ''}-- -D warnings
if grep -R 'dbg!' ./src; then
echo "use of dbg macro found in code!"
false
fi
'';
installPhase = ''
touch $out
'';
});
rustfmt = rustPackage: { rustfmt }: rustPackage.overrideAttrs (old: {
nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ [ rustfmt ];
doCheck = false;
buildPhase = ''
echo "checking formatting..."
cargo fmt --all -- --check
'';
installPhase = ''
touch $out
'';
});
}