Add copyBinsFilter option for configuring which executables are copied

This commit is contained in:
Nicolas Mattia 2020-02-10 12:51:32 +01:00
parent 7b5659aacd
commit 206cf17e20
4 changed files with 24 additions and 8 deletions

View file

@ -45,8 +45,8 @@ it is converted to an attribute set equivalent to `{ root = theArg; }`.
| `version` | The version of the derivation. |
| `src` | Used by `naersk` as source input to the derivation. When `root` is not set, `src` is also used to discover the `Cargo.toml` and `Cargo.lock`. |
| `root` | Used by `naersk` to read the `Cargo.toml` and `Cargo.lock` files. May be different from `src`. When `src` is not set, `root` is (indirectly) used as `src`. |
| `cargoBuild` | The command to use for the build. The argument must be a function modifying the default value. <br/> Default: `''cargo $cargo_options build $cargo_build_options''` |
| `cargoBuildOptions` | Options passed to cargo build, i.e. `cargo build <OPTS>`. These options can be accessed during the build through the environment variable `cargo_build_options`. <br/> Note: naersk relies on the `--out-dir out` option. <br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" ]` |
| `cargoBuild` | The command to use for the build. The argument must be a function modifying the default value. <br/> Default: `''cargo $cargo_options build $cargo_build_options >> $cargo_build_output_json''` |
| `cargoBuildOptions` | Options passed to cargo build, i.e. `cargo build <OPTS>`. These options can be accessed during the build through the environment variable `cargo_build_options`. <br/> Note: naersk relies on the `--out-dir out` option and the `--message-format=json-diagnostic-rendered-ansi` option. <br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=json-diagnostic-rendered-ansi" ]` |
| `doCheck` | When true, `checkPhase` is run. Default: `true` |
| `cargoTestCommands` | The commands to run in the `checkPhase`. The argument must be a function modifying the default value. <br/> Default: `[ ''cargo $cargo_options test $cargo_test_options'' ]` |
| `cargoTestOptions` | Options passed to cargo test, i.e. `cargo test <OPTS>`. These options can be accessed during the build through the environment variable `cargo_test_options`. <br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' ]` |
@ -57,7 +57,8 @@ it is converted to an attribute set equivalent to `{ root = theArg; }`.
| `override` | An override for all derivations involved in the build. Default: `(x: x)` |
| `singleStep` | When true, no intermediary (dependency-only) build is run. Enabling `singleStep` greatly reduces the incrementality of the builds. Default: `false` |
| `targets` | The targets to build if the `Cargo.toml` is a virtual manifest. |
| `copyBins` | When true, the resulting binaries are copied to `$out/bin`. Default: `true` |
| `copyBins` | When true, the resulting binaries are copied to `$out/bin`. <br/> Note: this relies on cargo's `--message-format` argument, set in the default `cargoBuildOptions`. Default: `true` |
| `copyBinsFilter` | A [`jq`](https://stedolan.github.io/jq) filter for selecting which build artifacts to release. This is run on cargo's [`--message-format`](https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages) JSON output. <br/> The value is written to the `cargo_bins_jq_filter` variable. Default: `''select(.reason == "compiler-artifact" and .executable != null and .profile.test == false)''` |
| `copyDocsToSeparateOutput` | When true, the documentation is generated in a different output, `doc`. Default: `true` |
| `doDocFail` | When true, the build fails if the documentation step fails; otherwise the failure is ignored. Default: `false` |
| `removeReferencesToSrcFromDocs` | When true, references to the nix store are removed from the generated documentation. Default: `true` |

View file

@ -11,6 +11,7 @@
, compressTarget
#| Whether or not to copy binaries to $out/bin
, copyBins
, copyBinsFilter
, doCheck
, doDoc
, doDocFail
@ -186,6 +187,7 @@ let
cargo_options = cargoOptions;
cargo_build_options = cargoBuildOptions;
cargo_test_options = cargoTestOptions;
cargo_bins_jq_filter = copyBinsFilter;
configurePhase = ''
runHook preConfigure
@ -205,6 +207,7 @@ let
log "cargo_options: $cargo_options"
log "cargo_build_options: $cargo_build_options"
log "cargo_test_options: $cargo_test_options"
log "cargo_bins_jq_filter: $cargo_bins_jq_filter"
log "cargo_build_output_json (created): $cargo_build_output_json"
mkdir -p target
@ -295,7 +298,7 @@ let
bin_name=$(jq -cMr '.target.name' <<<"$to_copy")
log "found executable $bin_name -> $out/bin/$bin_name"
cp "$bin_path" "$out/bin/$bin_name"
done < <(jq -cMr 'select(.reason == "compiler-artifact" and .executable != null and .profile.test == false)' <"$cargo_build_output_json")
done < <(jq -cMr "$cargo_bins_jq_filter" <"$cargo_build_output_json")
else
log "$cargo_build_output_json: file wasn't written, using less reliable copying method"
find out -type f -executable \

View file

@ -33,8 +33,8 @@ let
# Options passed to cargo build, i.e. `cargo build <OPTS>`. These options
# can be accessed during the build through the environment variable
# `cargo_build_options`. <br/>
# Note: naersk relies on the `--out-dir out` option. <br/>
# NOTE: re-used in copy-bins
# Note: naersk relies on the `--out-dir out` option and the
# `--message-format=json-diagnostic-rendered-ansi` option. <br/>
# Note: these values are not (shell) escaped, meaning that you can use
# environment variables but must be careful when introducing e.g. spaces. <br/>
cargoBuildOptions =
@ -78,8 +78,18 @@ let
singleStep = attrs0.singleStep or false;
# The targets to build if the `Cargo.toml` is a virtual manifest.
targets = attrs0.targets or null;
# When true, the resulting binaries are copied to `$out/bin`.
# When true, the resulting binaries are copied to `$out/bin`. <br/>
# Note: this relies on cargo's `--message-format` argument, set in the
# default `cargoBuildOptions`.
copyBins = attrs0.copyBins or true;
# A [`jq`](https://stedolan.github.io/jq) filter for selecting which build
# artifacts to release. This is run on cargo's
# [`--message-format`](https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages)
# JSON output. <br/>
# The value is written to the `cargo_bins_jq_filter` variable.
copyBinsFilter = attrs0.copyBinsFilter or
''select(.reason == "compiler-artifact" and .executable != null and .profile.test == false)'';
# When true, the documentation is generated in a different output, `doc`.
copyDocsToSeparateOutput = attrs0.copyDocsToSeparateOutput or true;
# When true, the build fails if the documentation step fails; otherwise
@ -162,6 +172,7 @@ let
cargoBuild
cargoBuildOptions
copyBins
copyBinsFilter
copyTarget
doCheck

View file

@ -61,7 +61,7 @@ let
if [ -f "$file" ]; then touch "$file"; fi
done
'';
inherit (config) src cargoTestCommands copyTarget copyBins copyDocsToSeparateOutput;
inherit (config) src cargoTestCommands copyTarget copyBins copyBinsFilter copyDocsToSeparateOutput;
inherit gitDependencies;
} // config.buildConfig // {
builtDependencies = lib.optional (! config.isSingleStep)
@ -89,6 +89,7 @@ let
cargoTestCommands = map (cmd: "${cmd} || true") config.buildConfig.cargoTestCommands;
copyTarget = true;
copyBins = false;
copyBinsFilter = ".";
copyDocsToSeparateOutput = false;
builtDependencies = [];
}