forked from nix-community/harmonia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackages.nix
More file actions
173 lines (149 loc) · 5.56 KB
/
packages.nix
File metadata and controls
173 lines (149 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
pkgs,
lib,
crane,
makeWrapper,
nix,
curl,
nix-src,
}:
let
craneLib = crane.mkLib pkgs;
# Extract version from Cargo.toml
cargoToml = lib.importTOML ./Cargo.toml;
version = cargoToml.workspace.package.version;
# Filter source to include only rust-related files
src = lib.cleanSourceWith {
src = craneLib.path ./.;
filter =
path: type:
(lib.hasSuffix "\.toml" path)
|| (lib.hasSuffix "\.lock" path)
|| (lib.hasSuffix "\.rs" path)
|| (lib.hasInfix "/harmonia-" path)
||
# Include test keys
(lib.hasSuffix ".pk" path)
|| (lib.hasSuffix ".sk" path)
|| (lib.hasSuffix ".pem" path)
|| (craneLib.filterCargoSources path type);
};
commonArgs = {
inherit src version;
pname = "harmonia";
strictDeps = true;
# Use mold linker for faster builds on ELF platforms
stdenv =
p: if p.stdenv.hostPlatform.isElf then p.stdenvAdapters.useMoldLinker p.stdenv else p.stdenv;
};
# Build *just* the cargo dependencies
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Coverage RUSTFLAGS matching what `cargo llvm-cov show-env` produces
coverageRustflags = "-C instrument-coverage --cfg=coverage --cfg=trybuild_no_target";
# Build dependencies with coverage instrumentation for reuse in coverage tests
cargoArtifactsCov = craneLib.buildDepsOnly (
commonArgs
// {
pnameSuffix = "-cov";
CARGO_BUILD_RUSTFLAGS = coverageRustflags;
# Discard profraw from build scripts/proc-macros (sandbox is read-only anyway)
LLVM_PROFILE_FILE = "/dev/null";
# Build debug profile to match `cargo build` / `cargo test` defaults
CARGO_PROFILE = "";
}
);
# Build the actual crate
harmonia = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
# Add runtime dependencies
nativeBuildInputs = [ makeWrapper ];
doCheck = false;
postInstall = ''
wrapProgram $out/bin/harmonia \
--prefix PATH : ${lib.makeBinPath [ nix ]}
'';
meta = with lib; {
description = "Nix binary cache implemented in rust";
homepage = "https://github.com/nix-community/harmonia";
license = with licenses; [ mit ];
maintainers = [ maintainers.mic92 ];
platforms = platforms.all;
};
}
);
# Test derivation with coverage - follows nomad pattern
# https://github.com/nomad/nomad/blob/main/nix/coverage.nix
# Uses mkCargoDerivation directly since cargoLlvmCov overrides buildPhaseCargoCommand
tests = craneLib.mkCargoDerivation (
commonArgs
// {
cargoArtifacts = cargoArtifactsCov;
pnameSuffix = "-llvm-cov";
doInstallCargoArtifacts = false;
# Use same RUSTFLAGS as cargoArtifactsCov to avoid rebuilding dependencies
CARGO_BUILD_RUSTFLAGS = coverageRustflags;
nativeBuildInputs = [
nix
curl
pkgs.cargo-llvm-cov
pkgs.cargo-nextest
pkgs.jq
];
# Custom build command following nomad pattern:
# 1. Build binaries with instrumentation (deps already built via cargoArtifactsCov)
# 2. Run tests with instrumented binaries via env vars
# 3. Generate report separately with --codecov
buildPhaseCargoCommand = ''
export NIX_UPSTREAM_SRC=${nix-src}
export LLVM_COV=${pkgs.llvmPackages.bintools-unwrapped}/bin/llvm-cov
export LLVM_PROFDATA=${pkgs.llvmPackages.bintools-unwrapped}/bin/llvm-profdata
${lib.optionalString pkgs.stdenv.isDarwin ''
export _NIX_TEST_NO_SANDBOX="1"
''}
# Set up coverage profile output (but don't override RUSTFLAGS - already set via CARGO_BUILD_RUSTFLAGS)
export LLVM_PROFILE_FILE="$PWD/target/harmonia-%p-%8m.profraw"
export CARGO_LLVM_COV=1
export CARGO_LLVM_COV_TARGET_DIR="$PWD/target"
# Build workspace binaries with coverage instrumentation
# Dependencies already built with same flags via cargoArtifactsCov
cargo build --workspace
# Point integration tests to instrumented binaries for coverage
export HARMONIA_DAEMON_BIN="$PWD/target/debug/harmonia-daemon"
export HARMONIA_CACHE_BIN="$PWD/target/debug/harmonia-cache"
# Run tests with nextest (they will use the instrumented binaries and write profraw data)
cargo nextest run --workspace
# Generate coverage report in codecov JSON format
cargo llvm-cov report --codecov --output-path coverage-raw.json
# Fix paths: strip build directory prefix to get repo-relative paths
# e.g., /nix/var/nix/builds/.../source/harmonia-cache/src/foo.rs -> harmonia-cache/src/foo.rs
# Also filter out stdlib paths (rustc-*/library/...) that leak into coverage
mkdir -p $out
jq '
.coverage |= (
# First filter out stdlib and other non-project paths
with_entries(select(.key | test("rustc-.*-src") | not))
# Then fix paths by extracting repo-relative portion
| with_entries(.key |= (capture(".*/source/(?<path>.*)") // {path: .}).path)
# Finally keep only harmonia-* paths (our crates)
| with_entries(select(.key | startswith("harmonia-")))
)
' coverage-raw.json > $out/${pkgs.stdenv.hostPlatform.system}.json
'';
installPhaseCommand = "";
}
);
# Clippy check derivation
clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets --all-features -- -D warnings";
}
);
in
{
inherit harmonia clippy tests;
default = harmonia;
}