Skip to content

Commit bfbc55f

Browse files
committed
build kinds
1 parent f549328 commit bfbc55f

File tree

15 files changed

+422
-630
lines changed

15 files changed

+422
-630
lines changed

crate2nix/Cargo.nix

Lines changed: 106 additions & 121 deletions
Large diffs are not rendered by default.

crate2nix/src/resolve.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Resolve dependencies and other data for CrateDerivation.
2-
32
use anyhow::format_err;
3+
use anyhow::Context;
44
use anyhow::Error;
55
use cargo_metadata::Node;
66
use cargo_metadata::Package;
@@ -141,11 +141,16 @@ impl CrateDerivation {
141141
.iter()
142142
.any(|t| t.kind.iter().any(|k| k == "proc-macro"));
143143

144+
// Binaries have one kind:
145+
// https://github.com/rust-lang/cargo/blob/94394ebf2a6b6f1c1d6c160b48865a06e54d5f30/src/cargo/core/manifest.rs#L126
144146
let binaries = package
145147
.targets
146148
.iter()
147149
.filter_map(|t| {
148-
if t.kind.iter().any(|k| k == "bin") {
150+
if t.kind
151+
.iter()
152+
.any(|k| k == "bin" || k == "example" || k == "bench" || k == "test")
153+
{
149154
BuildTarget::new(&t, &package_path).ok()
150155
} else {
151156
None
@@ -333,23 +338,33 @@ pub fn double_crate_with_rename() {
333338
pub struct BuildTarget {
334339
/// The name of the build target.
335340
pub name: String,
341+
/// The kind of the build target.
342+
pub kind: String,
336343
/// The relative path of the target source file.
337344
pub src_path: PathBuf,
338345
/// The crate's features that need to be enabled for this target to be compiled.
339346
/// Otherwise, this target is ignored.
340347
pub required_features: Vec<String>,
348+
/// Whether to test this binary
349+
pub test: bool,
341350
}
342351

343352
impl BuildTarget {
344353
pub fn new(target: &Target, package_path: impl AsRef<Path>) -> Result<BuildTarget, Error> {
345354
Ok(BuildTarget {
346355
name: target.name.clone(),
356+
kind: target
357+
.kind
358+
.first()
359+
.context("Target shouuld have kind")?
360+
.to_string(),
347361
src_path: target
348362
.src_path
349363
.canonicalize()?
350364
.strip_prefix(&package_path)?
351365
.to_path_buf(),
352366
required_features: target.required_features.clone(),
367+
test: target.test,
353368
})
354369
}
355370
}

crate2nix/templates/Cargo.nix.tera

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ rec {
106106
{
107107
name = {{ bin.name }};
108108
path = {{ bin.src_path }};
109+
kind = {{ bin.kind }};
110+
test = {{ bin.test }};
111+
# TODO (when cargo merges metadata):
112+
# harness
113+
# bench
109114
requiredFeatures = [ {% for feature in bin.required_features %}{{feature}} {% endfor %}];
110115
}
111116
{%- endfor %}

crate2nix/templates/nix/crate2nix/default.nix

Lines changed: 32 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -89,112 +89,34 @@ rec {
8989
|| baseName == "tests.nix"
9090
);
9191

92-
/* Returns a crate which depends on successful test execution
93-
of crate given as the second argument.
94-
95-
testCrateFlags: list of flags to pass to the test exectuable
96-
testInputs: list of packages that should be available during test execution
97-
*/
98-
crateWithTest = { crate, testCrate, testCrateFlags, testInputs, testPreRun, testPostRun }:
99-
assert builtins.typeOf testCrateFlags == "list";
100-
assert builtins.typeOf testInputs == "list";
101-
assert builtins.typeOf testPreRun == "string";
102-
assert builtins.typeOf testPostRun == "string";
103-
let
104-
# override the `crate` so that it will build and execute tests instead of
105-
# building the actual lib and bin targets We just have to pass `--test`
106-
# to rustc and it will do the right thing. We execute the tests and copy
107-
# their log and the test executables to $out for later inspection.
108-
test =
109-
let
110-
drv = testCrate.override
111-
(
112-
_: {
113-
buildTests = true;
114-
}
115-
);
116-
# If the user hasn't set any pre/post commands, we don't want to
117-
# insert empty lines. This means that any existing users of crate2nix
118-
# don't get a spurious rebuild unless they set these explicitly.
119-
testCommand = pkgs.lib.concatStringsSep "\n"
120-
(pkgs.lib.filter (s: s != "") [
121-
testPreRun
122-
"$f $testCrateFlags 2>&1 | tee -a $out"
123-
testPostRun
124-
]);
125-
in
126-
pkgs.runCommand "run-tests-${testCrate.name}"
127-
{
128-
inherit testCrateFlags;
129-
buildInputs = testInputs;
130-
} ''
131-
set -ex
132-
133-
export RUST_BACKTRACE=1
134-
135-
# recreate a file hierarchy as when running tests with cargo
136-
137-
# the source for test data
138-
${pkgs.xorg.lndir}/bin/lndir ${crate.src}
139-
140-
# build outputs
141-
testRoot=target/debug
142-
mkdir -p $testRoot
143-
144-
# executables of the crate
145-
# we copy to prevent std::env::current_exe() to resolve to a store location
146-
for i in ${crate}/bin/*; do
147-
cp "$i" "$testRoot"
148-
done
149-
chmod +w -R .
150-
151-
# test harness executables are suffixed with a hash, like cargo does
152-
# this allows to prevent name collision with the main
153-
# executables of the crate
154-
hash=$(basename $out)
155-
for file in ${drv}/tests/*; do
156-
f=$testRoot/$(basename $file)-$hash
157-
cp $file $f
158-
${testCommand}
159-
done
160-
'';
161-
in
162-
pkgs.runCommand "${crate.name}-linked"
163-
{
164-
inherit (crate) outputs crateName;
165-
passthru = (crate.passthru or { }) // {
166-
inherit test;
167-
};
168-
} ''
169-
echo tested by ${test}
170-
${lib.concatMapStringsSep "\n" (output: "ln -s ${crate.${output}} ${"$"}${output}") crate.outputs}
171-
'';
172-
17392
/* A restricted overridable version of builtRustCratesWithFeatures. */
17493
buildRustCrateWithFeatures =
17594
{ packageId
17695
, features ? rootFeatures
17796
, crateOverrides ? defaultCrateOverrides
17897
, buildRustCrateForPkgsFunc ? null
179-
, runTests ? false
180-
, testCrateFlags ? [ ]
181-
, testInputs ? [ ]
182-
# Any command to run immediatelly before a test is executed.
183-
, testPreRun ? ""
184-
# Any command run immediatelly after a test is executed.
185-
, testPostRun ? ""
98+
# Available: [ "lib" "bin" ] or ["test" "bench" "example"]
99+
, buildKinds ? [ "lib" "bin" ]
186100
}:
187101
lib.makeOverridable
188102
(
189103
{ features
190104
, crateOverrides
191-
, runTests
192-
, testCrateFlags
193-
, testInputs
194-
, testPreRun
195-
, testPostRun
105+
, buildKinds
196106
}:
197107
let
108+
isDevBuild =
109+
let
110+
inherit (pkgs.buildRustCrateHelpers.kinds) isLib isBin isExample isTest isBench;
111+
112+
notDev = builtins.any (k: isLib k || isBin k) buildKinds;
113+
isDev = builtins.any (k: isBench k || isExample k || isTest k) buildKinds;
114+
in
115+
assert (buildKinds != [ ]);
116+
# Can't have build dev and non dev kinds
117+
assert (notDev != isDev);
118+
isDev;
119+
198120
buildRustCrateForPkgsFuncOverriden =
199121
if buildRustCrateForPkgsFunc != null
200122
then buildRustCrateForPkgsFunc
@@ -207,31 +129,16 @@ rec {
207129
defaultCrateOverrides = crateOverrides;
208130
}
209131
);
132+
210133
builtRustCrates = builtRustCratesWithFeatures {
211-
inherit packageId features;
134+
inherit packageId features buildKinds isDevBuild;
212135
buildRustCrateForPkgsFunc = buildRustCrateForPkgsFuncOverriden;
213-
runTests = false;
214136
};
215-
builtTestRustCrates = builtRustCratesWithFeatures {
216-
inherit packageId features;
217-
buildRustCrateForPkgsFunc = buildRustCrateForPkgsFuncOverriden;
218-
runTests = true;
219-
};
220-
drv = builtRustCrates.crates.${packageId};
221-
testDrv = builtTestRustCrates.crates.${packageId};
222-
derivation =
223-
if runTests then
224-
crateWithTest
225-
{
226-
crate = drv;
227-
testCrate = testDrv;
228-
inherit testCrateFlags testInputs testPreRun testPostRun;
229-
}
230-
else drv;
137+
231138
in
232-
derivation
139+
builtRustCrates.crates.${packageId}
233140
)
234-
{ inherit features crateOverrides runTests testCrateFlags testInputs testPreRun testPostRun; };
141+
{ inherit features crateOverrides buildKinds; };
235142

236143
/* Returns an attr set with packageId mapped to the result of buildRustCrateForPkgsFunc
237144
for the corresponding crate.
@@ -241,21 +148,24 @@ rec {
241148
, features
242149
, crateConfigs ? crates
243150
, buildRustCrateForPkgsFunc
244-
, runTests
151+
, buildKinds
152+
, isDevBuild
245153
, makeTarget ? makeDefaultTarget
246154
} @ args:
247155
assert (builtins.isAttrs crateConfigs);
248156
assert (builtins.isString packageId);
249157
assert (builtins.isList features);
250158
assert (builtins.isAttrs (makeTarget stdenv.hostPlatform));
251-
assert (builtins.isBool runTests);
159+
assert (builtins.isBool isDevBuild);
160+
assert (builtins.isList buildKinds);
252161
let
253162
rootPackageId = packageId;
254163
mergedFeatures = mergePackageFeatures
255164
(
256165
args // {
257166
inherit rootPackageId;
258-
target = makeTarget stdenv.hostPlatform // { test = runTests; };
167+
# What does test do for target?
168+
target = makeTarget stdenv.hostPlatform // { test = false; };
259169
}
260170
);
261171
# Memoize built packages so that reappearing packages are only built once.
@@ -277,7 +187,7 @@ rec {
277187
builtins.removeAttrs crateConfig' [ "resolvedDefaultFeatures" "devDependencies" ];
278188
devDependencies =
279189
lib.optionals
280-
(runTests && packageId == rootPackageId)
190+
(isDevBuild && packageId == rootPackageId)
281191
(crateConfig'.devDependencies or [ ]);
282192
dependencies =
283193
dependencyDerivations {
@@ -351,6 +261,7 @@ rec {
351261
}
352262
);
353263
extraRustcOpts = lib.lists.optional (targetFeatures != [ ]) "-C target-feature=${lib.concatMapStringsSep "," (x: "+${x}") targetFeatures}";
264+
buildKinds = if (packageId == rootPackageId) then buildKinds else [ "lib" ];
354265
inherit features dependencies buildDependencies crateRenames release;
355266
}
356267
);
@@ -472,7 +383,7 @@ rec {
472383
, featuresByPackageId ? { }
473384
, target
474385
# Adds devDependencies to the crate with rootPackageId.
475-
, runTests ? false
386+
, isDevBuild ? false
476387
, ...
477388
} @ args:
478389
assert (builtins.isAttrs crateConfigs);
@@ -482,7 +393,7 @@ rec {
482393
assert (builtins.isList dependencyPath);
483394
assert (builtins.isAttrs featuresByPackageId);
484395
assert (builtins.isAttrs target);
485-
assert (builtins.isBool runTests);
396+
assert (builtins.isBool isDevBuild);
486397
let
487398
crateConfig = crateConfigs."${packageId}" or (builtins.throw "Package not found: ${packageId}");
488399
expandedFeatures = expandFeatures (crateConfig.features or { }) features;
@@ -517,7 +428,7 @@ rec {
517428
mergePackageFeatures {
518429
features = combinedFeatures;
519430
featuresByPackageId = cache;
520-
inherit crateConfigs packageId target runTests rootPackageId;
431+
inherit crateConfigs packageId target isDevBuild rootPackageId;
521432
}
522433
);
523434
cacheWithSelf =
@@ -533,7 +444,7 @@ rec {
533444
(
534445
crateConfig.dependencies or [ ]
535446
++ lib.optionals
536-
(runTests && packageId == rootPackageId)
447+
(isDevBuild && packageId == rootPackageId)
537448
(crateConfig.devDependencies or [ ])
538449
);
539450
cacheWithAll =

crate2nix/templates/nix/crate2nix/tests/packageFeatures.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ let
8888
crate2nix.mergePackageFeatures
8989
{
9090
target = crate2nix.makeDefaultTarget (stdenv.hostPlatform);
91-
runTests = false;
9291
rootPackageId = packageId;
9392
inherit crateConfigs packageId features;
9493
};

default.nix

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ let
1919
in
2020
!(baseName == "templates" && type == "directory");
2121
crate2nix = cargoNix.rootCrate.build.override {
22-
testCrateFlags = [
23-
"--skip nix_integration_tests"
24-
];
2522
crateOverrides = defaultCrateOverrides // {
2623
crate2nix = { src, ... }: {
2724
src =

nix/sources.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@
4848
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
4949
},
5050
"nixpkgs": {
51-
"branch": "nixos-unstable",
52-
"description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to",
53-
"homepage": "https://github.com/NixOS/nixpkgs",
54-
"owner": "NixOS",
51+
"branch": "brc-kinds",
52+
"description": "Nix Packages collection",
53+
"homepage": "",
54+
"owner": "jordanisaacs",
5555
"repo": "nixpkgs",
56-
"rev": "0c4800d579af4ed98ecc47d464a5e7b0870c4b1f",
57-
"sha256": "00gx09447gzgxlzwih4hdj51sdg62xanikkgr4bv4y7fpm98qirq",
56+
"rev": "812079dcb5dbbe5f89a02f107b840704f7bcb441",
57+
"sha256": "16p1mh1xvvdnsrfr8ha1310zxvh6yfg0n1yhxs19rvax6c2xw9wv",
5858
"type": "tarball",
59-
"url": "https://github.com/NixOS/nixpkgs/archive/0c4800d579af4ed98ecc47d464a5e7b0870c4b1f.tar.gz",
59+
"url": "https://github.com/jordanisaacs/nixpkgs/archive/812079dcb5dbbe5f89a02f107b840704f7bcb441.tar.gz",
6060
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
6161
},
6262
"nixpkgs-mozilla": {

0 commit comments

Comments
 (0)