mkDummySrc: allow running a custom script after dummification is done (#116)

This commit is contained in:
Ivan Petkov 2022-09-26 23:25:09 -07:00 committed by GitHub
parent 1428f3ae64
commit 21e627606c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 216 additions and 11 deletions

View File

@ -26,6 +26,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `mkCargoDerivation` (along with any of its callers like `cargoBuild`,
`buildPackage`, etc.) now accept a `stdenv` argument which will override the
default environment (coming from `pkgs.stdenv`) for that particular derivation
* `mkDummySrc` now accepts `extraScript` which can be used to run a custom
script, and therefore customize what the dummy source contains
* `buildDepsOnly` now accepts `dummySrc` as a way to directly pass in the dummy
source to be used. Automatically derived via `args.src` if not specified.
## Fixed
* `cargoAudit` properly keeps any `audit.toml` files when cleaning the source

View File

@ -0,0 +1,2 @@
[build]
rustc = "./.cargo/myrustc"

View File

@ -0,0 +1,3 @@
#!/bin/sh
echo running custom rustc >&2
exec rustc "$@"

7
checks/custom-dummy/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "custom-dummy"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "custom-dummy"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -139,6 +139,14 @@ myPkgs // {
mkDummySrcTests = callPackage ./mkDummySrcTests { };
# https://github.com/ipetkov/crane/issues/111
mkDummySrcCustom = myLib.buildPackage {
src = ./custom-dummy;
extraDummyScript = ''
cp -r ${./custom-dummy/.cargo} -T $out/.cargo
'';
};
nextest = callPackage ./nextest.nix { };
simple = myLib.buildPackage {

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "mkDummySrcSimple"
version = "0.1.0"

View File

@ -0,0 +1,35 @@
[[bench]]
name = "bench1"
[[bench]]
name = "bench2"
path = "benches/custom.rs"
[[bin]]
name = "bin1"
[[bin]]
name = "bin2"
path = "src/bin/custom.rs"
[[example]]
name = "example1"
[[example]]
name = "example2"
path = "examples/custom.rs"
[[test]]
name = "test1"
[[test]]
name = "test2"
path = "tests/custom.rs"
[lib]
name = "foo"
[package]
edition = "2021"
name = "mkDummySrcSimple"
version = "0.1.0"

View File

@ -0,0 +1 @@
another additional file

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1 @@
some additional file

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,2 @@
#![allow(dead_code)]
pub fn main() {}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "mkDummySrcSimple"
version = "0.1.0"

View File

@ -0,0 +1,35 @@
[package]
name = "mkDummySrcSimple"
version = "0.1.0"
edition = "2021"
[lib]
name = "foo"
[[bin]]
name = "bin1"
[[bin]]
name = "bin2"
path = "src/bin/custom.rs"
[[example]]
name = "example1"
[[example]]
name = "example2"
path = "examples/custom.rs"
[[test]]
name = "test1"
[[test]]
name = "test2"
path = "tests/custom.rs"
[[bench]]
name = "bench1"
[[bench]]
name = "bench2"
path = "benches/custom.rs"

View File

@ -0,0 +1 @@
some additional file

View File

@ -0,0 +1,4 @@
fn main() {
println!("Hello, world!");
}

View File

@ -5,17 +5,21 @@
}:
let
cmpDummySrcRaw = name: input: expected:
doCompare = name: expected: actual:
runCommand "compare-${name}" { } ''
echo ${expected} ${actual}
diff -r ${expected} ${actual}
touch $out
'';
cmpDummySrcRaw = name: expected: input:
let
dummySrc = mkDummySrc {
src = input;
};
in
runCommand "compare-${name}" { } ''
echo ${expected} ${dummySrc}
diff -r ${expected} ${dummySrc}
touch $out
'';
doCompare name expected dummySrc;
cmpDummySrc = name: path:
let
@ -35,12 +39,27 @@ let
};
in
[
(cmpDummySrcRaw name input expected)
(cmpDummySrcRaw "${name}-filtered" filteredInput expected)
(cmpDummySrcRaw name expected input)
(cmpDummySrcRaw "${name}-filtered" expected filteredInput)
];
customizedDummy =
let
expected = ./customized/expected;
input = ./customized/input;
in
doCompare "customized" expected (mkDummySrc {
src = input;
extraDummyScript = ''
cp ${input}/extra-custom-file.txt $out
echo 'another additional file' >$out/another-custom-file.txt
'';
});
in
linkFarmFromDrvs "cleanCargoToml" (lib.flatten [
(cmpDummySrc "single" ./single)
(cmpDummySrc "single-alt" ./single-alt)
(cmpDummySrc "workspace" ./workspace)
customizedDummy
])

View File

@ -114,6 +114,9 @@ to influence its behavior.
- Default value: `"${cargoTestCommand} ${cargoExtraArgs}"`
* `doCheck`: whether the derivation's check phase should be run
- Default value: `true`
* `dummySrc`: the "dummy" source to use when building this derivation.
Automatically derived if not passed in
- Default value: `mkDummySrc args.src`
* `pname`: package name of the derivation
- Default value: inherited from calling `crateNameFromCargoToml`
* `version`: version of the derivation
@ -129,6 +132,7 @@ environment variables during the build, you can bring them back via
* `cargoCheckCommand`
* `cargoExtraArgs`
* `cargoTestCommand`
* `dummySrc`
### `lib.buildPackage`
@ -794,6 +798,41 @@ build caches. More specifically:
#### Optional attributes
* `cargoLock`: a path to a Cargo.lock file
- Default value: `src + /Cargo.lock`
* `extraDummyScript`: additional shell script which will be run inside the builder
verbatim. Useful for customizing what the dummy sources include by running any
arbitrary commands.
- Default value: `""`
- Note that this script will run in an environment
_where the original source is not present_ as doing so would cause a rebuild
if any part of the source changed. Additional files can be copied to the
derivation's result, but care must be taken that the derivation only depends
on (i.e. is rebuilt if) the smallest subset of the original source as
required.
- Here is an example of how to include an entire directory, in this case
`.cargo`, but any other directory would work as well:
```nix
let
in
mkDummySrc {
# The _entire_ source of the project. mkDummySrc will automatically
# filter out irrelevant files as described above
src = ./.;
# Note that here we scope the path to just `./.cargo` and not any other
# directories which may exist at the root of the project. Also note that
# the entire path is inside of the `${ }` which ensures that the
# derivation only consumes that directory. Writing `${./.}/.cargo` would
# incorectly consume the entire source root, and therefore rebuild
# everything when any file changes, which defeats artifact caching.
#
# Also note the `--no-target-directory` flag which ensures the results are
# copied to `$out/.cargo` instead of something like `$out/HASH-.cargo`
extraDummyScript = ''
cp -r ${./.cargo} --no-target-directory $out/.cargo
'';
}
```
### `lib.overrideToolchain`

View File

@ -17,6 +17,7 @@ let
"cargoCheckCommand"
"cargoExtraArgs"
"cargoTestCommand"
"dummySrc"
];
throwMsg = throw ''
@ -28,10 +29,10 @@ let
path = args.src or throwMsg;
cargoToml = path + "/Cargo.toml";
dummySrc =
if builtins.pathExists cargoToml
dummySrc = args.dummySrc or
(if builtins.pathExists cargoToml
then mkDummySrc args
else throwMsg;
else throwMsg);
in
mkCargoDerivation (cleanedArgs // {
src = dummySrc;

View File

@ -8,6 +8,7 @@
{ src
, cargoLock ? null
, extraDummyScript ? ""
, ...
}:
let
@ -187,4 +188,5 @@ runCommandLocal "dummy-src" { } ''
cp --recursive --no-preserve=mode,ownership ${cleanSrc}/. -t $out
${copyCargoLock}
${copyAndStubCargoTomls}
${extraDummyScript}
''