diff --git a/README.md b/README.md index 59e4eb6..d6ce876 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ it is converted to an attribute set equivalent to `{ root = theArg; }`. | `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.
Default: `''cargo $cargo_options build $cargo_build_options >> $cargo_build_output_json''` | -| `cargoBuildOptions` | Options passed to cargo build, i.e. `cargo build `. These options can be accessed during the build through the environment variable `cargo_build_options`.
Note: naersk relies on the `--out-dir out` option and the `--message-format=json-diagnostic-rendered-ansi` option.
Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces.
The argument must be a function modifying the default value.
Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=json-diagnostic-rendered-ansi" ]` | +| `cargoBuildOptions` | Options passed to cargo build, i.e. `cargo build `. These options can be accessed during the build through the environment variable `cargo_build_options`.
Note: naersk relies on the `--out-dir out` option and the `--message-format` option. The `$cargo_message_format` variable is set based on the cargo version.
Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces.
The argument must be a function modifying the default value.
Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=$cargo_message_format" ]` | | `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.
Default: `[ ''cargo $cargo_options test $cargo_test_options'' ]` | | `cargoTestOptions` | Options passed to cargo test, i.e. `cargo test `. These options can be accessed during the build through the environment variable `cargo_test_options`.
Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces.
The argument must be a function modifying the default value.
Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' ]` | diff --git a/build.nix b/build.nix index 2624d65..246cfb1 100644 --- a/build.nix +++ b/build.nix @@ -202,7 +202,19 @@ let } cargo_build_output_json=$(mktemp) + cargo_version=$(cargo --version | grep -oP 'cargo \K.*') + # ANSI rendered diagnostics were introduced in 1.38: + # https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-138-2019-09-26 + if ! [[ "$cargo_version" < "1.38" ]] + then + cargo_message_format="json-diagnostic-rendered-ansi" + else + cargo_message_format="json" + fi + + log "cargo_version (read): $cargo_version" + log "cargo_message_format (set): $cargo_message_format" log "cargo_release: $cargo_release" log "cargo_options: $cargo_options" log "cargo_build_options: $cargo_build_options" @@ -253,7 +265,7 @@ let if [ "$cargo_ec" -ne "0" ] then - cat $cargo_build_output_json | jq -cMr 'select(.message.rendered != null) | .message.rendered' + cat "$cargo_build_output_json" | jq -cMr 'select(.message.rendered != null) | .message.rendered' log "cargo returned with exit code $cargo_ec, exiting" exit "$cargo_ec" fi diff --git a/config.nix b/config.nix index 59140f6..a245aa0 100644 --- a/config.nix +++ b/config.nix @@ -34,11 +34,12 @@ let # can be accessed during the build through the environment variable # `cargo_build_options`.
# Note: naersk relies on the `--out-dir out` option and the - # `--message-format=json-diagnostic-rendered-ansi` option.
+ # `--message-format` option. The `$cargo_message_format` variable is set + # based on the cargo version.
# Note: these values are not (shell) escaped, meaning that you can use # environment variables but must be careful when introducing e.g. spaces.
cargoBuildOptions = - allowFun attrs0 "cargoBuildOptions" [ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=json-diagnostic-rendered-ansi" ]; + allowFun attrs0 "cargoBuildOptions" [ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=$cargo_message_format" ]; # When true, `checkPhase` is run. doCheck = attrs0.doCheck or true;