flake.nix: create packaging, generate coverage

This commit is contained in:
Oystein Kristoffer Tveit 2024-08-09 19:05:16 +02:00
parent e420c1f4d5
commit dc29dd274a
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
3 changed files with 139 additions and 1 deletions

View File

@ -7,7 +7,9 @@
};
outputs = { self, nixpkgs, rust-overlay }@inputs:
let
let
inherit (nixpkgs) lib;
systems = [
"x86_64-linux"
"aarch64-linux"
@ -15,6 +17,7 @@
"aarch64-darwin"
"armv7l-linux"
];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: let
pkgs = import nixpkgs {
inherit system;
@ -27,6 +30,16 @@
toolchain = rust-bin.stable.latest.default;
in f system pkgs toolchain);
in {
apps = let
mkApp = program: { type = "app"; program = toString program; };
in forAllSystems (system: pkgs: _: {
mysqladm-rs = mkApp (lib.getExe self.packages.${system}.mysqladm-rs);
coverage = mkApp (pkgs.writeScript "mysqladm-rs-coverage" ''
${lib.getExe pkgs.python3} -m http.server -d "${self.packages.${system}.coverage}/html/src"
'');
});
devShell = forAllSystems (system: pkgs: toolchain: pkgs.mkShell {
nativeBuildInputs = with pkgs; [
toolchain
@ -36,5 +49,33 @@
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
});
overlays = {
default = self.overlays.mysqladm-rs;
greg-ng = final: prev: {
inherit (self.packages.${prev.system}) mysqladm-rs;
};
};
packages = let
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
cargoLock = ./Cargo.lock;
src = builtins.filterSource (path: type: let
baseName = baseNameOf (toString path);
in !(lib.any (b: b) [
(!(lib.cleanSourceFilter path type))
(baseName == "target" && type == "directory")
(baseName == "nix" && type == "directory")
(baseName == "flake.nix" && type == "regular")
(baseName == "flake.lock" && type == "regular")
])) ./.;
in forAllSystems (system: pkgs: _: {
default = self.packages.${system}.mysqladm-rs;
mysqladm-rs = pkgs.callPackage ./nix/default.nix { inherit cargoToml cargoLock src; };
coverage = pkgs.callPackage ./nix/coverage.nix { inherit cargoToml cargoLock src; };
filteredSource = pkgs.runCommandLocal "filtered-source" { } ''
ln -s ${src} $out
'';
});
};
}

75
nix/coverage.nix Normal file
View File

@ -0,0 +1,75 @@
{
lib
, stdenvNoCC
, rustPlatform
, cargoToml
, cargoLock
, src
, rust-bin
, cargo-nextest
, grcov
}:
stdenvNoCC.mkDerivation {
pname = "coverage-${cargoToml.package.name}";
version = cargoToml.package.version;
inherit src;
env = {
RUSTFLAGS = "-Cinstrument-coverage";
LLVM_PROFILE_FILE = "target/coverage/%p-%m.profraw";
};
cargoDeps = rustPlatform.importCargoLock {
lockFile = cargoLock;
};
nativeBuildInputs = [
rustPlatform.cargoSetupHook
cargo-nextest
grcov
(rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
extensions = [ "llvm-tools-preview" ];
}))
];
buildPhase = ''
runHook preBuild
export HOME="$(pwd)"
cargo nextest run --all-features --release --no-fail-fast
grcov \
--source-dir . \
--binary-path ./target/release/deps/ \
--excl-start 'mod test* \{' \
--ignore 'tests/*' \
--ignore "*test.rs" \
--ignore "*tests.rs" \
--ignore "*github.com*" \
--ignore "*libcore*" \
--ignore "*rustc*" \
--ignore "*liballoc*" \
--ignore "*cargo*" \
-t html \
-o ./target/coverage/html \
target/coverage/
runHook postBuild
'';
installPhase = ''
runHook preBuild
mv target/coverage $out
runHook postBuild
'';
meta = with lib; {
license = licenses.mit;
platforms = platforms.linux ++ platforms.darwin;
};
}

22
nix/default.nix Normal file
View File

@ -0,0 +1,22 @@
{
lib
, rustPlatform
, cargoToml
, cargoLock
, src
}:
let
in
rustPlatform.buildRustPackage {
pname = cargoToml.package.name;
version = cargoToml.package.version;
inherit src;
cargoLock.lockFile = cargoLock;
meta = with lib; {
license = licenses.mit;
platforms = platforms.linux ++ platforms.darwin;
mainProgram = (lib.head cargoToml.bin).name;
};
}