2022-01-10 02:48:13 +03:00
# API Documentation
2022-01-16 07:21:02 +03:00
## `mkLib`
`mkLib :: pkgs -> set`
Creates a `lib` instance bound to the specified (and instantiated) `pkgs` set.
This is a convenience escape hatch in case you want to use your own custom
2022-01-17 02:30:07 +03:00
instantiation of nixpkgs with the overlays you may need.
2022-01-16 07:21:02 +03:00
```nix
mkLib (import inputs.nixpkgs { system = "armv7l-linux"; })
```
2022-01-17 02:30:07 +03:00
Note that if you wish to override a particular package without having to overlay
2024-02-08 06:38:28 +03:00
it across all of nixpkgs, consider using `overrideScope` :
2022-01-17 02:30:07 +03:00
```nix
2024-02-08 06:38:28 +03:00
(mkLib pkgs).overrideScope (final: prev: {
2022-06-26 21:25:40 +03:00
cargo-tarpaulin = myCustomCargoTarpaulinVersion;
2022-01-17 02:30:07 +03:00
})
```
2022-06-26 21:25:40 +03:00
To overlay an entire rust toolchain (e.g. `cargo` , `rustc` , `clippy` , `rustfmt` ,
etc.) consider using `overrideToolchain` .
2023-03-20 07:13:23 +03:00
## `craneLib`
2022-01-10 02:48:13 +03:00
2023-03-20 07:13:23 +03:00
`craneLib` represents an instantiated value crated by `mkLib` above.
### `craneLib.appendCrateRegistries`
2022-01-30 03:12:22 +03:00
`appendCrateRegistries :: [registry mapping] -> new lib`
Creates a new `lib` instance which will make additional registries available for
use when downloading crate sources. Each entry can be defined using:
* `registryFromDownloadUrl` : if you know the exact `dl` URL as defined in the
registry's `config.json` file
* `registryFromGitIndex` : if you would like the download URL to be inferred from
the index's source directly.
2023-06-13 05:03:18 +03:00
* `registryFromSparse` : if you would like the download URL to be inferred from
the index's source directly, and the index is a sparse index.
2022-01-30 03:12:22 +03:00
See the documentation on each function for more specifics.
```nix
2023-03-20 07:13:23 +03:00
newLib = craneLib.appendCrateRegistries [
(craneLib.registryFromDownloadUrl {
2022-01-30 03:12:22 +03:00
indexUrl = "https://github.com/rust-lang/crates.io-index";
2024-03-20 00:55:28 +03:00
dl = "https://static.crates.io/crates";
2022-11-21 03:22:13 +03:00
fetchurlExtraArgs = {};
2022-01-30 03:12:22 +03:00
})
# Or, alternatively
2023-03-20 07:13:23 +03:00
(craneLib.registryFromGitIndex {
2022-01-31 02:10:28 +03:00
indexUrl = "https://github.com/Hirevo/alexandrie-index";
2022-01-30 03:12:22 +03:00
rev = "90df25daf291d402d1ded8c32c23d5e1498c6725";
2022-11-21 03:22:13 +03:00
fetchurlExtraArgs = {};
2022-01-30 03:12:22 +03:00
})
2023-06-13 05:03:18 +03:00
# Or even
(lib.registryFromSparse {
url = "https://index.crates.io/";
sha256 = "d16740883624df970adac38c70e35cf077a2a105faa3862f8f99a65da96b14a3";
})
2022-01-30 03:12:22 +03:00
];
```
2023-03-20 07:13:23 +03:00
### `craneLib.buildDepsOnly`
2022-01-10 02:48:13 +03:00
`buildDepsOnly :: set -> drv`
Create a derivation which will only build all dependencies of a cargo workspace.
Useful for splitting up cargo projects into two derivations: one which only
builds dependencies and needs to be rebuilt when a Cargo.lock file changes, and
another which inherits the cargo artifacts from the first and (quickly) builds
just the application itself.
The exact cargo commands being run (or the arguments passed into it) can be
easily updated to suit your needs. By default all artifacts from running `cargo
{check,build,test}` will be cached.
In addition to all default and overridden values being set as documented below,
all derivation attributes are delegated to `mkCargoDerivation` , and can be used
to influence its behavior.
* `cargoArtifacts` : set to `null` since this is our entry point for generating
cargo artifacts
* `doInstallCargoArtifacts` : set to `true`
* `pnameSuffix` : set to `"-deps"`
* `src` : set to the result of `mkDummySrc` after applying the arguments set.
This ensures that we do not need to rebuild the cargo artifacts derivation
whenever the application source changes.
#### Optional attributes
* `buildPhaseCargoCommand` : A command to run during the derivation's build
phase. Pre and post build hooks will automatically be run.
- Default value: `"${cargoCheckCommand} ${cargoExtraArgs}\n${cargoBuildCommand} ${cargoExtraArgs}"`
* `cargoBuildCommand` : A cargo (build) invocation to run during the derivation's build
phase
2022-09-20 04:08:24 +03:00
- Default value: `"cargo build --profile release"`
2022-07-23 21:10:45 +03:00
* `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
2022-07-24 00:11:53 +03:00
is selected; setting it to `""` will omit specifying a profile
2022-07-23 21:10:45 +03:00
altogether.
2022-01-10 02:48:13 +03:00
* `cargoCheckCommand` : A cargo (check) invocation to run during the derivation's build
phase (in order to cache additional artifacts)
2022-11-28 06:23:35 +03:00
- Default value: `"cargo check --profile release ${cargoCheckExtraArgs}"`
2022-07-23 21:10:45 +03:00
* `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
2022-07-24 00:11:53 +03:00
is selected; setting it to `""` will omit specifying a profile
2022-07-23 21:10:45 +03:00
altogether.
2022-10-10 01:04:37 +03:00
* `cargoCheckExtraArgs` : additional flags to be passed in the `cargoCheckCommand`
invocation
2022-11-28 06:23:35 +03:00
- Default value: `"--all-targets"` if `doCheck` is set to true, `""` otherwise
2022-01-10 02:48:13 +03:00
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation (e.g.
enabling specific features)
2023-08-27 22:17:58 +03:00
- Default value: `"--locked"`
2022-01-10 02:48:13 +03:00
* `cargoTestCommand` : A cargo invocation to run during the derivation's check
phase
2022-07-23 21:10:45 +03:00
- Default value: `"cargo test --profile release"`
* `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
2022-07-24 00:11:53 +03:00
is selected; setting it to `""` will omit specifying a profile
2022-07-23 21:10:45 +03:00
altogether.
2022-10-10 01:04:37 +03:00
* `cargoTestExtraArgs` : additional flags to be passed in the `cargoTestCommand`
invocation (e.g. enabling specific tests)
2023-11-30 03:43:47 +03:00
- Default value: `"--no-run"`
2022-01-10 02:48:13 +03:00
* `cargoVendorDir` : A path (or derivation) of vendored cargo sources which can
be consumed without network access. Directory structure should basically
follow the output of `cargo vendor` .
- Default value: the result of `vendorCargoDeps` after applying the arguments
set (with the respective default values)
* `checkPhaseCargoCommand` : A command to run during the derivation's check
phase. Pre and post check hooks will automatically be run.
- Default value: `"${cargoTestCommand} ${cargoExtraArgs}"`
* `doCheck` : whether the derivation's check phase should be run
- Default value: `true`
2022-09-27 09:25:09 +03:00
* `dummySrc` : the "dummy" source to use when building this derivation.
Automatically derived if not passed in
- Default value: `mkDummySrc args.src`
2022-01-10 02:48:13 +03:00
* `pname` : package name of the derivation
- Default value: inherited from calling `crateNameFromCargoToml`
* `version` : version of the derivation
- Default value: inherited from calling `crateNameFromCargoToml`
#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoBuildCommand`
* `cargoCheckCommand`
2022-10-10 01:04:37 +03:00
* `cargoCheckExtraArgs`
2022-01-10 02:48:13 +03:00
* `cargoExtraArgs`
* `cargoTestCommand`
2022-10-10 01:04:37 +03:00
* `cargoTestExtraArgs`
2022-09-27 09:25:09 +03:00
* `dummySrc`
2023-09-22 07:08:53 +03:00
* `outputHashes`
2024-01-27 20:52:30 +03:00
* `outputs`
2022-01-10 02:48:13 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.buildPackage`
2022-01-10 02:48:13 +03:00
`buildPackage :: set -> drv`
2022-10-10 00:55:36 +03:00
A(n opinionated) version of `mkCargoDerivation` which will install to the output
any binaries which were built by cargo in this invocation. All options
understood by `mkCargoDerivation` apply here as well, with the only difference
being some additional book keeping necessary to log cargo's results and
subsequently install from that log.
2022-01-10 02:48:13 +03:00
2023-06-08 04:00:25 +03:00
Note that only `bin` , `cdylib` , `dylib` , and `staticlib` , targets will be installed by
2023-01-22 03:50:20 +03:00
default (namely `rlib` targets will be ignored), though it is possible to adjust
the behavior by changing the `installPhaseCommand` or registering additional
install hooks.
2022-01-10 02:48:13 +03:00
#### Optional attributes
* `buildPhaseCargoCommand` : A command to run during the derivation's build
phase. Pre and post build hooks will automatically be run.
- Default value: `cargoBuildCommand` will be invoked along with
`cargoExtraArgs` passed in, except cargo's build steps will also be captured
and written to a log so that it can be used to find the build binaries.
- Note that the default install hook assumes that the build phase will create
a log of cargo's build results. If you wish to customize this command
completely, make sure that cargo is run with `--message-format
json-render-diagnostics` and the standard output captured and saved to a
file. The `cargoBuildLog` shell variable should point to this log.
2022-10-10 00:55:36 +03:00
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- Default value: the result of `buildDepsOnly` after applying the arguments
2023-04-11 04:37:58 +03:00
set (with the respective default values).
2023-06-07 08:38:53 +03:00
- `installPhase` and `installPhaseCommand` will be removed, and no
installation hooks will be run
2022-01-10 02:48:13 +03:00
* `cargoBuildCommand` : A cargo invocation to run during the derivation's build
phase
2022-07-23 21:10:45 +03:00
- Default value: `"cargo build --profile release"`
* `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
2022-07-24 00:11:53 +03:00
is selected; setting it to `""` will omit specifying a profile
2022-07-23 21:10:45 +03:00
altogether.
2022-01-10 02:48:13 +03:00
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation (e.g.
enabling specific features)
2023-08-27 22:17:58 +03:00
- Default value: `"--locked"`
2022-10-10 00:55:36 +03:00
* `cargoTestCommand` : A cargo invocation to run during the derivation's check
phase
- Default value: `"cargo test --profile release"`
* `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
is selected; setting it to `""` will omit specifying a profile
altogether.
* `cargoTestExtraArgs` : additional flags to be passed in the `cargoTestCommand`
invocation (e.g. enabling specific tests)
- Default value: `""`
* `doCheck` : whether the derivation's check phase should be run
- Default value: `true`
2022-01-10 02:48:13 +03:00
* `doInstallCargoArtifacts` : controls whether cargo's `target` directory should
be copied as an output
- Default value: `false`
2022-10-10 00:55:36 +03:00
* `installPhaseCommand` : the command(s) which are expected to install the
derivation's outputs.
- Default value: will look for a cargo build log and install all binary
targets listed there
#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoBuildCommand`
* `cargoExtraArgs`
* `cargoTestCommand`
* `cargoTestExtraArgs`
2023-09-22 07:08:53 +03:00
* `outputHashes`
2022-01-10 02:48:13 +03:00
#### Native build dependencies and included hooks
The following hooks are automatically added as native build inputs:
* `installFromCargoBuildLogHook`
2022-10-09 05:03:05 +03:00
* `jq`
2022-09-25 02:45:56 +03:00
* `removeReferencesToVendoredSourcesHook`
2022-01-10 02:48:13 +03:00
2023-04-29 22:34:39 +03:00
### `craneLib.buildTrunkPackage`
`buildTrunkPackage :: set -> drv`
Create a derivation which will build a distributable directory for a WASM application.
Except where noted below, all derivation attributes are delegated to
`mkCargoDerivation` , and can be used to influence its behavior.
#### Optional attributes
* `buildPhaseCargoCommand` : A command to run during the derivation's build
phase. Pre and post build hooks will automatically be run.
- Default value: `trunk build` will be invoked along with `trunkExtraArgs` ,
`trunkExtraBuildArgs` , and `trunkIndexpath` passed in. If `$CARGO_PROFILE`
is set to `release` then the `--release` flag will also be set for the build
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- Default value: the result of `buildDepsOnly` after applying the arguments
set (with the respective default values).
- `CARGO_BUILD_TARGET` will be set to `"wasm32-unknown-unknown"` if not specified.
- `doCheck` will be set to `false` if not specified.
- `installPhase` and `installPhaseCommand` will be removed (in favor of their
default values provided by `buildDepsOnly` )
* `installPhaseCommand` : the command(s) which are expected to install the
derivation's outputs.
- Default value: will install trunk's `dist` output directory
* `trunkExtraArgs` pass additional arguments to `trunk`
- Default value: `""`
* `trunkExtraBuildArgs` pass additional arguments to `trunk build`
- Default value: `""`
* `trunkIndexPath` A path to the index.html of your trunk project
- Default value: `"./index.html"`
2023-12-20 19:24:53 +03:00
* `wasm-bindgen-cli` The package used to satisfy the `wasm-bindgen-cli`
dependency of `trunk` , the version used here must match the version
of `wasm-bindgen` in the `Cargo.lock` file of your project *exactly* .
- Default value: `pkgs.wasm-bindgen-cli`
2023-04-29 22:34:39 +03:00
#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `trunkExtraArgs`
* `trunkExtraBuildArgs`
* `trunkIndexPath`
#### Native build dependencies and included hooks
The following hooks are automatically added as native build inputs:
* `binaryen`
2023-08-11 03:38:14 +03:00
* `dart-sass`
2023-04-29 22:34:39 +03:00
* `trunk`
2023-03-20 07:13:23 +03:00
### `craneLib.cargoAudit`
2022-07-30 18:52:07 +03:00
`cargoAudit :: set -> drv`
Create a derivation which will run a `cargo audit` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
2022-10-09 21:53:55 +03:00
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run `cargo audit -n -d ${advisory-db}` in
2022-07-30 18:52:07 +03:00
the workspace.
2022-10-09 21:53:55 +03:00
* `cargoArtifacts` will be set to `null` as they are not needed
2022-08-02 01:55:52 +03:00
* `cargoVendorDir` will be set to `null` as it is not needed
2022-07-30 18:52:07 +03:00
* `doInstallCargoArtifacts` is disabled
* `pnameSuffix` will be set to `"-audit"`
2022-08-02 01:55:52 +03:00
* `src` will be filtered to only keep `Cargo.lock` files
#### Required attributes
* `advisory-db` : A path (or derivation) which contains the advisory database
- It is possible to track the advisory database as a flake input and avoid
having to manually update hashes or specific revisions to check out
* `src` : The project source to audit, it must contain a `Cargo.lock` file
- Note that the source will internally be filtered to omit any files besides
`Cargo.lock` . This avoids having to audit the project again until either the
advisory database or the dependencies change.
2022-07-30 18:52:07 +03:00
#### Optional attributes
* `cargoAuditExtraArgs` : additional flags to be passed in the cargo-audit invocation
2023-10-15 00:50:48 +03:00
- Default value: `"--ignore yanked"`
2022-08-02 01:55:52 +03:00
* `pname` : the name of the derivation; will _not_ be introspected from a
`Cargo.toml` file
- Default value: `"crate"`
* `version` : the version of the derivation, will _not_ be introspected from a
`Cargo.toml` file
- Default value: `"0.0.0"`
2022-07-30 18:52:07 +03:00
#### Native build dependencies
The `cargo-audit` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
#### Remove attributes
The following attributes will be removed before being lowered to
2022-10-10 00:02:27 +03:00
`mkCargoDerivation` . If you absolutely need these attributes present as
2022-07-30 18:52:07 +03:00
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoAuditExtraArgs`
2023-10-21 00:45:03 +03:00
### `craneLib.cargoDeny`
`cargoDeny :: set -> drv`
Create a derivation which will run a `cargo deny` invocation in a cargo
workspace.
Note that although `cargo deny` can serve as a replacement for `cargo audit` ,
`craneLib.cargoDeny` does not expose this functionality because `cargo deny`
requires the full source tree, rather than working from just the `Cargo.lock`
file, meaning it will be re-run when any source file changes, rather than only
when dependencies change.
Except where noted below, all derivation attributes are delegated to
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run
`cargo --offline $cargoExtraArgs deny $cargoDenyExtraArgs check
$cargoDenyChecks` in the workspace.
* `cargoArtifacts` will be set to `null`
* `doInstallCargoArtifacts` will be set to `false`
* `pnameSuffix` will be set to `"-deny"`
#### Optional attributes
* `cargoDenyChecks` : check types to run
- Default value: `"bans licenses sources"`
* `cargoDenyExtraArgs` : additional flags to be passed in the cargo-deny invocation
- Default value: `""`
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation
- Default value: `""`
#### Native build dependencies
The `cargo-deny` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoDenyExtraArgs`
* `cargoExtraArgs`
2023-03-20 07:13:23 +03:00
### `craneLib.cargoBuild`
2022-01-10 02:48:13 +03:00
`cargoBuild :: set -> drv`
2022-10-10 01:17:25 +03:00
Create a derivation which will run a `cargo build` invocation in a cargo
workspace. Consider using `buildPackage` if all you need is to build the
workspace and install the resulting application binaries.
2022-01-10 02:48:13 +03:00
2022-10-10 01:17:25 +03:00
Except where noted below, all derivation attributes are delegated to
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run `cargo build --profile release` for
the workspace.
- `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
is selected; setting it to `""` will omit specifying a profile
altogether.
* `pnameSuffix` will be set to `"-build"`
2022-01-10 02:48:13 +03:00
2022-10-10 01:17:25 +03:00
#### Required attributes
2022-01-10 02:48:13 +03:00
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
2022-10-10 01:17:25 +03:00
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
#### Optional attributes
2022-01-10 02:48:13 +03:00
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation (e.g.
enabling specific features)
2023-08-27 22:17:58 +03:00
- Default value: `"--locked"`
2022-01-10 02:48:13 +03:00
#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoExtraArgs`
2023-03-20 07:13:23 +03:00
### `craneLib.cargoClippy`
2022-01-10 02:48:13 +03:00
`cargoClippy :: set -> drv`
Create a derivation which will run a `cargo clippy` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
2022-10-09 22:02:17 +03:00
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run `cargo clippy --profile release` for
2022-08-12 02:58:32 +03:00
the workspace.
2022-07-23 21:10:45 +03:00
- `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
2022-07-24 00:11:53 +03:00
is selected; setting it to `""` will omit specifying a profile
2022-07-23 21:10:45 +03:00
altogether.
2022-01-10 02:48:13 +03:00
* `pnameSuffix` will be set to `"-clippy"`
#### Required attributes
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
#### Optional attributes
* `cargoClippyExtraArgs` : additional flags to be passed in the clippy invocation (e.g.
deny specific lints)
2022-08-12 02:58:32 +03:00
- Default value: `"--all-targets"`
2022-01-10 02:48:13 +03:00
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation (e.g.
enabling specific features)
2023-08-27 22:17:58 +03:00
- Default value: `"--locked"`
2022-01-10 02:48:13 +03:00
#### Native build dependencies
The `clippy` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
#### Remove attributes
The following attributes will be removed before being lowered to
2022-10-10 00:02:27 +03:00
`mkCargoDerivation` . If you absolutely need these attributes present as
2022-01-10 02:48:13 +03:00
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoClippyExtraArgs`
2022-10-09 22:02:17 +03:00
* `cargoExtraArgs`
2022-01-10 02:48:13 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.cargoDoc`
2022-09-20 04:05:43 +03:00
`cargoDoc :: set -> drv`
Create a derivation which will run a `cargo doc` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
2022-10-09 21:48:53 +03:00
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run `cargo doc --profile release` for
2022-09-20 04:05:43 +03:00
the workspace.
- `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
is selected; setting it to `""` will omit specifying a profile
altogether.
2023-09-03 20:29:13 +03:00
* `doInstallCargoArtifacts` will default to `false` if not specified
2022-09-20 04:05:43 +03:00
* `pnameSuffix` will be set to `"-doc"`
#### Required attributes
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
#### Optional attributes
* `cargoDocExtraArgs` : additional flags to be passed in the rustdoc invocation (e.g.
deny specific lints)
- Default value: `"--no-deps"`
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation (e.g.
enabling specific features)
2023-08-27 22:17:58 +03:00
- Default value: `"--locked"`
2024-05-04 20:49:24 +03:00
* `docInstallRoot` : defines the exact directory to install to `$out/share` ,
useful for overriding when compiling different targets. By default will honor
`$CARGO_TARGET_DIR` (or default to `./target` if not set) and
`$CARGO_BUILD_TARGET` (if set).
- Default value: `"${CARGO_TARGET_DIR:-target}/${CARGO_BUILD_TARGET:-}/doc"`
2022-09-20 04:05:43 +03:00
#### Remove attributes
The following attributes will be removed before being lowered to
2022-10-10 00:02:27 +03:00
`mkCargoDerivation` . If you absolutely need these attributes present as
2022-09-20 04:05:43 +03:00
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoDocExtraArgs`
2022-10-09 21:48:53 +03:00
* `cargoExtraArgs`
2024-05-04 20:49:24 +03:00
* `docInstallRoot`
2022-09-20 04:05:43 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.cargoFmt`
2022-01-17 04:49:39 +03:00
`cargoFmt :: set -> drv`
Create a derivation which will run a `cargo fmt` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
2022-10-09 21:45:27 +03:00
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run `cargo fmt` (in check mode) in the
workspace.
2022-01-17 04:49:39 +03:00
* `cargoArtifacts` is disabled/cleared
* `cargoVendorDir` is disabled/cleared
* `pnameSuffix` will be set to `"-fmt"`
#### Optional attributes
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation
- Default value: `""`
2022-10-09 21:45:27 +03:00
* `rustFmtExtraArgs` : additional flags to be passed in the rustfmt invocation
- Default value: `""`
2022-01-17 04:49:39 +03:00
#### Native build dependencies
The `rustfmt` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
#### Remove attributes
The following attributes will be removed before being lowered to
2022-10-10 00:02:27 +03:00
`mkCargoDerivation` . If you absolutely need these attributes present as
2022-01-17 04:49:39 +03:00
environment variables during the build, you can bring them back via
`.overrideAttrs` .
2022-10-09 21:45:27 +03:00
* `cargoExtraArgs`
2022-01-17 04:49:39 +03:00
* `rustFmtExtraArgs`
2023-03-20 07:13:23 +03:00
### `craneLib.cargoLlvmCov`
2023-03-20 02:44:14 +03:00
`cargoLlvmCov :: set -> drv`
Create a derivation which will run a `cargo llvm-cov` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run `cargo llvm-cov test --release` in
the workspace.
* `installPhaseCommand` will be set to `""` , as the default settings creates
a file instead of directory at `$out` .
* `doInstallCargoArtifacts` will be set to `false` for the same reason as
`installPhaseCommand`
* `pnameSuffix` will be set to `"-llvm-cov"`
#### Required attributes
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
#### Optional attributes
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation
2023-08-27 22:17:58 +03:00
- Default value: `"--locked"`
2023-03-20 02:44:14 +03:00
* `cargoLlvmCovCommand` : cargo-llvm-cov command to run
- Default value: `"test"`
* `cargoLlvmCovExtraArgs` : additional flags to be passed in the cargo
llvm-cov invocation
- Default value: `"--lcov --output-path $out"`
#### Native build dependencies
The `cargo-llvm-cov` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
Note that this would require the `llvm-tools-preview` component for the Rust toolchain,
which you would need to provide yourself using fenix or rust-overlay.
#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoExtraArgs`
* `cargoLlvmCovCommand`
* `cargoLlvmCovExtraArgs`
2023-03-20 07:13:23 +03:00
### `craneLib.cargoNextest`
2022-07-24 01:04:06 +03:00
`cargoNextest :: set -> drv`
Create a derivation which will run a `cargo nextest` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
2022-10-09 22:12:51 +03:00
`mkCargoDerivation` , and can be used to influence its behavior.
* `checkPhaseCargoCommand` will be set to run `cargo nextest run --profile release`
2022-07-24 01:04:06 +03:00
for the workspace.
- `CARGO_PROFILE` can be set on the derivation to alter which cargo profile
is selected; setting it to `""` will omit specifying a profile
altogether.
* `pnameSuffix` will be set to `"-nextest"` and may include partition numbers
#### Required attributes
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
#### Optional attributes
* `buildPhaseCargoCommand` , unless specified, will be set to print the nextest version
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation (e.g.
enabling specific features)
- Default value: `""`
2024-03-23 01:39:45 +03:00
* `cargoLlvmCovExtraArgs` : additional flags to be passed in the cargo
llvm-cov invocation
- Default value: `"--lcov --output-path $out/coverage"`
2024-05-01 04:33:35 +03:00
* `cargoNextestExtraArgs` : additional flags to be passed in the nextest invocation
(e.g. specifying a profile)
2022-07-24 01:04:06 +03:00
- Default value: `""`
* `partitions` : The number of separate nextest partitions to run. Useful if the
test suite takes a long time and can be parallelized across multiple build
nodes.
- Default value: `1`
* `partitionType` : The kind of nextest partition to run (e.g. `"count"` or
`"hash"` based).
- Default value: `"count"`
2024-03-23 01:39:45 +03:00
* `withLlvmCov` : Whether or not to run nextest through `cargo llvm-cov`
- Default value: `false`
- Note that setting `withLlvmCov = true;` is not currently supported if
`partitions > 1` .
2022-07-24 01:04:06 +03:00
#### Native build dependencies
The `cargo-nextest` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
#### Remove attributes
The following attributes will be removed before being lowered to
2022-10-10 00:02:27 +03:00
`mkCargoDerivation` . If you absolutely need these attributes present as
2022-07-24 01:04:06 +03:00
environment variables during the build, you can bring them back via
`.overrideAttrs` .
2022-10-09 22:12:51 +03:00
* `cargoExtraArgs`
2024-03-23 01:39:45 +03:00
* `cargoLlvmCovExtraArgs`
2022-07-24 01:04:06 +03:00
* `cargoNextestExtraArgs`
* `partitions`
* `partitionType`
2024-03-23 01:39:45 +03:00
* `withLlvmCov`
2022-07-24 01:04:06 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.cargoTarpaulin`
2022-01-17 05:16:21 +03:00
`cargoTarpaulin :: set -> drv`
Create a derivation which will run a `cargo tarpaulin` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
2022-10-09 21:58:21 +03:00
`mkCargoDerivation` , and can be used to influence its behavior.
* `buildPhaseCargoCommand` will be set to run `cargo tarpaulin --profile release` in
2022-07-23 21:10:45 +03:00
the workspace.
- `CARGO_PROFILE` can be set on the derivation to alter which cargo profile is
2022-07-24 00:11:53 +03:00
selected; setting it to `""` will omit specifying a profile altogether.
2022-01-17 05:16:21 +03:00
* `pnameSuffix` will be set to `"-tarpaulin"`
2022-10-10 00:22:23 +03:00
#### Required attributes
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
2022-01-17 05:16:21 +03:00
#### Optional attributes
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation
- Default value: `""`
* `cargoTarpaulinExtraArgs` : additional flags to be passed in the cargo
tarpaulin invocation
2023-10-01 21:22:09 +03:00
- Default value: `"--skip-clean --out xml --output-dir $out"`
2023-06-21 04:13:46 +03:00
* `doNotLinkInheritedArtifacts` will be set to `true` if not specified.
2022-01-17 05:16:21 +03:00
#### Native build dependencies
The `cargo-tarpaulin` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
#### Remove attributes
The following attributes will be removed before being lowered to
2022-10-10 00:02:27 +03:00
`mkCargoDerivation` . If you absolutely need these attributes present as
2022-01-17 05:16:21 +03:00
environment variables during the build, you can bring them back via
`.overrideAttrs` .
2022-10-09 21:58:21 +03:00
* `cargoExtraArgs`
2022-01-17 05:16:21 +03:00
* `cargoTarpaulinExtraArgs`
2023-03-20 07:13:23 +03:00
### `craneLib.cargoTest`
2022-10-10 00:00:17 +03:00
`cargoTest :: set -> drv`
Create a derivation which will run a `cargo test` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
* `buildPhaseCargoCommand` will be set to run `cargo test --profile release` in
the workspace.
- `CARGO_PROFILE` can be set on the derivation to alter which cargo profile is
selected; setting it to `""` will omit specifying a profile altogether.
* `pnameSuffix` will be set to `"-test"`
#### Optional attributes
* `cargoExtraArgs` : additional flags to be passed in the cargo invocation
2023-08-27 22:17:58 +03:00
- Default value: `"--locked"`
2022-10-10 00:00:17 +03:00
* `cargoTestArgs` : additional flags to be passed in the cargo
invocation
- Default value: `""`
#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `cargoExtraArgs`
* `cargoTestExtraArgs`
2023-03-20 07:13:23 +03:00
### `craneLib.cleanCargoSource`
2022-09-21 04:27:47 +03:00
`cleanCargoSource :: path or drv -> drv`
Cleans a source tree to omit things like version control directories as well
omit any non-Rust/non-cargo related files. Useful to avoid rebuilding a project
when unrelated files are changed (e.g. `flake.nix` or any other nix files).
2022-12-01 03:14:59 +03:00
The final output will be cleaned by both `cleanSource` (from nixpkgs) and
2023-03-20 07:13:23 +03:00
`craneLib.filterCargoSources` . See each of them for more details on which files are
2022-09-21 04:27:47 +03:00
kept.
If it is necessary to customize which files are kept, a custom filter can be
2023-03-20 07:13:23 +03:00
written (which may want to also call `craneLib.filterCargoSources` ) to achieve the
2022-09-21 04:27:47 +03:00
desired behavior.
```nix
2024-06-11 06:53:46 +03:00
craneLib.cleanCargoSource ./.
2022-09-21 04:27:47 +03:00
```
2023-03-20 07:13:23 +03:00
### `craneLib.cleanCargoToml`
2022-01-10 02:48:13 +03:00
`cleanCargoToml :: set -> set`
Cleans all definitions from a Cargo.toml file which are irrelevant for a
minimal build of a package's dependencies. See `mkDummySrc` for more information
on how the result is applied.
In general, the following types of attributes are kept from the original input:
* basic package definitions (like name and version)
* dependency definitions
* feature definitions
* workspace definitions
* anything pertaining to project structure (like bin/lib targets, tests, etc.)
```nix
2023-03-20 07:13:23 +03:00
craneLib.cleanCargoToml { cargoToml = ./Cargo.toml; }
2022-01-10 02:48:13 +03:00
# { dependencies = { byteorder = "*"; }; package = { edition = "2021"; name = "simple"; version = "0.1.0"; }; }
```
#### Input attributes
* `cargoToml` : a path to a Cargo.toml file
* `cargoTomlContents` : the contents of a Cargo.toml file as a string
At least one of the above attributes must be specified, or an error will be
raised during evaluation.
2023-03-20 07:13:23 +03:00
### `craneLib.crateNameFromCargoToml`
2022-01-10 02:48:13 +03:00
`crateNameFromCargoToml :: set -> set`
Extract a crate's name and version from its Cargo.toml file.
2022-10-16 20:12:31 +03:00
The resulting `pname` attribute will be populated with the value of the
2024-05-04 22:36:42 +03:00
Cargo.toml's (top-level) attributes in the following order, where the first
attribute (with a string value) will be chosen:
1. `package.metadata.crane.name`
1. `package.name`
1. `workspace.metadata.crane.name`
1. (Deprecated) `workspace.package.name`
1. Otherwise a placeholder name will be used
2022-10-16 20:12:31 +03:00
The resulting `version` attribute will be populated with the value of the
2024-05-04 22:36:42 +03:00
Cargo.toml's (top-level) attributes in the following order, where the first
attribute (with a string value) will be chosen:
1. `package.version`
1. `workspace.package.version`
1. Otherwise a placeholder version will be used
2022-10-16 20:12:31 +03:00
2024-02-04 22:31:22 +03:00
Note that *only the root `Cargo.toml` of the specified source will be checked* .
Directories **will not be crawled** to resolve potential workspace inheritance.
2022-01-10 02:48:13 +03:00
```nix
2023-03-20 07:13:23 +03:00
craneLib.crateNameFromCargoToml { cargoToml = ./Cargo.toml; }
2022-01-10 02:48:13 +03:00
# { pname = "simple"; version = "0.1.0"; }
```
2023-03-20 07:13:23 +03:00
### `craneLib.crateRegistries`
2022-01-30 03:12:22 +03:00
`crateRegistries :: set`
A set of crate registries made available for use in downloading crate sources.
The keys are registry URLs as used in the Cargo.lock file (e.g.
"registry+https://...") and the values are the download URL for that registry,
including any [placeholder
values](https://doc.rust-lang.org/cargo/reference/registries.html#index-format)
cargo is expected to populate for downloads.
This definition can be updated via `appendCrateRegistries` .
2022-01-10 02:48:13 +03:00
#### Input attributes
* `src` : a directory which includes a Cargo.toml file at its root.
* `cargoToml` : a path to a Cargo.toml file
* `cargoTomlContents` : the contents of a Cargo.toml file as a string
At least one of the above attributes must be specified, or an error will be
raised during evaluation.
#### Output attributes
* `pname` : the name of the crate
- Default value: `"cargo-package"` if the specified Cargo.toml file did not
include a name
* `version` : the version of the crate
2022-01-16 06:58:31 +03:00
- Default value: `"0.0.1"` if the specified Cargo.toml file did not
2022-01-10 02:48:13 +03:00
include a version
2023-09-03 20:33:10 +03:00
### `craneLib.devShell`
`devShell :: set -> drv`
A thin wrapper around
[`pkgs.mkShell` ](https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell ) for
creating development shells for use with `nix develop` (see [“Local
Development”](local_development.md)). Except where noted below, all derivation
attributes are passed straight through, so any `mkShell` behavior can be used
as expected: namely, all key-value pairs other than those `mkShell` consumes
will be set as environment variables in the resulting shell.
2023-10-17 03:24:15 +03:00
Note that the current toolchain's `cargo` , `clippy` , `rustc` , and `rustfmt`
packages will automatically be added to the devShell.
2023-09-03 20:33:10 +03:00
#### Optional attributes
* `checks` : A set of checks to inherit inputs from, typically
`self.checks.${system}` . Build inputs from the values in this attribute set
are added to the created shell environment for interactive use.
* `inputsFrom` : A list of extra packages to inherit inputs from. Note that
these packages are _not_ added to the result environment; use
`packages` for that.
* `packages` : A list of extra packages to add to the created shell environment.
* `shellHook` : A string of bash statements that will be executed when the shell
is entered with `nix develop` .
See the [quick start example ](examples/quick-start.md ) for usage in a
`flake.nix` file.
```nix
craneLib.devShell {
checks = self.checks.${system};
packages = [
pkgs.ripgrep
];
# Set a `cargo-nextest` profile:
NEXTEST_PROFILE = "local";
}
```
```nix
craneLib.devShell {
checks = {
my-package-clippy = craneLib.cargoClippy commonArgs;
my-package-doc = craneLib.cargoDoc commonArgs;
my-package-nextest = craneLib.cargoNextest commonArgs;
};
}
```
2023-03-20 07:13:23 +03:00
### `craneLib.downloadCargoPackage`
2022-01-10 02:48:13 +03:00
`downloadCargoPackage :: set -> drv`
Download a packaged cargo crate (e.g. from crates.io) and prepare it for
vendoring.
2022-11-21 03:22:13 +03:00
The registry's `fetchurlExtraArgs` will be passed through to `fetchurl` when
downloading the crate, making it possible to influence interacting with the
registry's API if necessary.
2022-01-10 02:48:13 +03:00
#### Required input attributes
* `checksum` : the (sha256) checksum recorded in the Cargo.lock file
* `name` : the name of the crate
* `source` : the source key recorded in the Cargo.lock file
* `version` : the version of the crate
2024-05-19 02:39:46 +03:00
#### Attributes of the vendor-prep derivation
* `dontBuild` : `true`
* `dontConfigure` : `true`
2024-05-20 00:59:49 +03:00
* `dontFixup` : `true`
2024-05-19 02:39:46 +03:00
* `pname` : `"cargo-package-"` suffixed by the package name in `Cargo.lock`
2024-05-20 00:59:49 +03:00
* `sourceRoot` : `"./crate"`
2024-05-19 02:39:46 +03:00
* `version` : inherited from the package version in `Cargo.lock`
* `unpackPhase` : This phase will:
1. run the `preUnpack` hook
2024-05-20 00:59:49 +03:00
1. create an empty directory named `./crate`
1. unpack the crate's tarball under `./crate`
2024-05-19 02:39:46 +03:00
1. run the `postUnpack` hook
* `installPhase` : This phase will:
1. run the `preInstall` hook
2024-05-20 00:59:49 +03:00
1. move the contents of the current directory (i.e. `./crate` by default) to
`$out`
2024-05-19 02:39:46 +03:00
1. populate `$out/.cargo-checksum.json`
1. run the `postInstall` hook
2023-03-20 07:13:23 +03:00
### `craneLib.downloadCargoPackageFromGit`
2022-02-08 07:03:12 +03:00
`downloadCargoPackageFromGit :: set -> drv`
2022-02-11 07:32:31 +03:00
Download a git repository containing a cargo crate or workspace, and prepare it
any crates it contains for vendoring.
2022-02-08 07:03:12 +03:00
#### Required input attributes
* `git` : the URL to the repository
* `rev` : the exact revision to check out
2022-02-19 04:46:05 +03:00
#### Optional attributes
2023-09-22 07:08:53 +03:00
* `allRefs` : whether all git refs should be fetched in order to look for the
specified `rev`
- Default value: `true` if `ref` is set to `null` , `false` otherwise
2022-02-19 04:46:05 +03:00
* `ref` : the ref (i.e. branch or tag) to which `rev` belongs to. For branches it
should be `"refs/head/${branch}"` and for tags it should be
`"refs/tags/${tag}"`
- Default value: `null`
2023-09-22 07:08:53 +03:00
* `sha256` : the sha256 hash of the (unpacked) download. If provided `fetchgit` will be used
(instead of `builtins.fetchGit` ) which allows for offline evaluations.
- Default value: `null`
2022-02-19 04:46:05 +03:00
2024-05-19 02:39:46 +03:00
#### Attributes of the vendor-prep derivation
* `dontBuild` : `true`
* `dontConfigure` : `true`
2024-05-20 00:59:49 +03:00
* `dontFixup` : `true`
2024-05-19 02:39:46 +03:00
* `installPhase` : This phase will:
1. run the `preInstall` hook
1. Prepare the current directory for vendoring by:
- Searching for all `Cargo.toml` files
- Copying their parent directory to `$out/$crate` (where `$crate` is the
package name and version as defined in `Cargo.toml` )
- Populating `.cargo-checksum.json`
- Running `crane-resolve-workspace-inheritance` on the `Cargo.toml`
- Note that duplicate crates (whose name and version collide) are ignored
1. run the `postInstall` hook
* `nativeBuildInputs` : A list of the `cargo` , `craneUtils` , and `jq` packages
* `name` : set to `"cargo-git"`
* `src` : the git repo checkout, as determined by the input parameters
2023-03-20 07:13:23 +03:00
### `craneLib.findCargoFiles`
2022-01-29 05:24:51 +03:00
`findCargoFiles :: path -> set of lists`
Given a path, recursively search it for any `Cargo.toml` , `.cargo/config` or
`.cargo/config.toml` files.
```nix
2023-03-20 07:13:23 +03:00
craneLib.findCargoFiles ./src
2022-01-29 05:24:51 +03:00
# { cargoTomls = [ "..." ]; cargoConfigs = [ "..." ]; }
```
2023-03-20 07:13:23 +03:00
### `craneLib.filterCargoSources`
2022-09-21 04:27:47 +03:00
`filterCargoSources :: path -> string -> bool`
A source filter which when used with `cleanSourceWith` (from nixpkgs's `lib` )
will retain the following files from a given source:
- Cargo files (`Cargo.toml`, `Cargo.lock` , `.cargo/config.toml` , `.cargo/config` )
- Rust files (files whose name end with `.rs` )
- TOML files (files whose name end with `.toml` )
```nix
cleanSourceWith {
2024-06-11 06:53:46 +03:00
src = ./.;
2023-03-20 07:13:23 +03:00
filter = craneLib.filterCargoSources;
2024-06-11 06:53:46 +03:00
name = "source"; # Be reproducible, regardless of the directory name
2022-09-21 04:27:47 +03:00
}
```
Note that it is possible to compose source filters, especially if
`filterCargoSources` omits files which are relevant to the build. For example:
```nix
let
# Only keeps markdown files
2022-11-17 05:40:40 +03:00
markdownFilter = path: _type: builtins.match ".*md$" path != null;
2022-09-21 04:27:47 +03:00
markdownOrCargo = path: type:
2023-03-20 07:13:23 +03:00
(markdownFilter path type) || (craneLib.filterCargoSources path type);
2022-09-21 04:27:47 +03:00
in
cleanSourceWith {
2024-06-11 06:53:46 +03:00
src = ./.;
2022-09-21 04:27:47 +03:00
filter = markdownOrCargo;
2024-06-11 06:53:46 +03:00
name = "source"; # Be reproducible, regardless of the directory name
2022-09-21 04:27:47 +03:00
}
```
2023-03-20 07:13:23 +03:00
### `craneLib.mkCargoDerivation`
2022-01-10 02:48:13 +03:00
`mkCargoDerivation :: set -> drv`
A thin wrapper around `stdenv.mkDerivation` which includes common hooks for
building a derivation using cargo. Except where noted below, all derivation
attributes are passed straight through, so any common derivation behavior can be
2023-02-09 03:27:05 +03:00
used as expected: namely all key-value pairs will be set as environment
variables for the derivation's build script.
2022-01-10 02:48:13 +03:00
This is a fairly low-level abstraction, so consider using `buildPackage` or
`cargoBuild` if they fit your needs.
#### Required attributes
* `buildPhaseCargoCommand` : A command (likely a cargo invocation) to run during
the derivation's build phase. Pre and post build hooks will automatically be
run.
* `cargoArtifacts` : A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
#### Optional attributes
* `buildPhase` : the commands used by the build phase of the derivation
- Default value: the build phase will run `preBuild` hooks, print the cargo
version, log and evaluate `buildPhaseCargoCommand` , and run `postBuild`
hooks
2023-10-15 04:42:24 +03:00
* `cargoLock` : if set will be passed through to the derivation and the path it
points to will be copied as the workspace `Cargo.lock`
- Unset by default
* `cargoLockContents` : if set and `cargoLock` is missing or null, its value will
be written as the workspace `Cargo.lock`
- Unset by default
* `cargoLockParsed` : if set and both `cargoLock` and `cargoLockContents` are
missing or null, its value will be serialized as TOML and the result written
as the workspace `Cargo.lock`
- Unset by default
2022-10-09 21:24:44 +03:00
* `cargoVendorDir` : A path (or derivation) of vendored cargo sources which can
be consumed without network access. Directory structure should basically
follow the output of `cargo vendor` .
- Default value: the result of `vendorCargoDeps` after applying the arguments
set (with the respective default values)
2022-01-10 02:48:13 +03:00
* `checkPhase` : the commands used by the check phase of the derivation
- Default value: the check phase will run `preCheck` hooks, log and evaluate
`checkPhaseCargoCommand` , and run `postCheck` hooks
2022-10-09 21:42:10 +03:00
* `checkPhaseCargoCommand` : A command (likely a cargo invocation) to run during
the derivation's check phase. Pre and post check hooks will automatically be
run.
- Default value: `""`
2022-09-18 04:18:03 +03:00
* `configurePhase` : the commands used by the configure phase of the derivation
- Default value: the configure phase will run `preConfigureHooks` hooks, then
run `postConfigure` hooks
2022-01-10 02:48:13 +03:00
* `doInstallCargoArtifacts` : controls whether cargo's `target` directory should
be copied as an output
- Default value: `true`
* `installPhase` : the commands used by the install phase of the derivation
- Default value: the install phase will run `preInstall` hooks, log and evaluate
`installPhaseCommand` , and run `postInstall` hooks
* `installPhaseCommand` : the command(s) which are expected to install the
derivation's outputs.
- Default value: `"mkdir -p $out"`
- By default an output directory is created such that any other `postInstall`
hooks can successfully run. Consider overriding this value with an
appropriate installation commands for the package being built.
2022-10-09 21:35:26 +03:00
* `pname` : the name of the derivation
- Default value: the package name listed in `Cargo.toml`
2022-01-10 02:48:13 +03:00
* `pnameSuffix` : a suffix appended to `pname`
- Default value: `""`
2022-09-15 20:20:48 +03:00
* `stdenv` : the standard build environment to use for this derivation
- Default value: `pkgs.stdenv`
2022-10-09 21:35:26 +03:00
* `version` : the version of the derivation
- Default value: the version listed in `Cargo.toml`
2022-01-10 02:48:13 +03:00
#### Remove attributes
The following attributes will be removed before being lowered to
`stdenv.mkDerivation` . If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs` .
* `buildPhaseCargoCommand`
2023-03-20 03:33:17 +03:00
* `cargoLock`
* `cargoLockContents`
* `cargoLockParsed`
2022-01-10 02:48:13 +03:00
* `checkPhaseCargoCommand`
* `installPhaseCommand`
2023-09-22 07:08:53 +03:00
* `outputHashes`
2022-01-10 02:48:13 +03:00
* `pnameSuffix`
2022-09-15 20:20:48 +03:00
* `stdenv`
2022-01-10 02:48:13 +03:00
#### Native build dependencies and included hooks
The `cargo` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller, along with the following
hooks:
2022-07-23 20:04:55 +03:00
* `cargoHelperFunctionsHook`
2022-01-10 02:48:13 +03:00
* `configureCargoCommonVarsHook`
* `configureCargoVendoredDepsHook`
* `inheritCargoArtifactsHook`
* `installCargoArtifactsHook`
2023-10-15 04:42:24 +03:00
* `replaceCargoLockHook`
2023-06-20 00:50:17 +03:00
* `rsync`
2022-10-09 05:03:05 +03:00
* `zstd`
2022-01-10 02:48:13 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.mkDummySrc`
2022-01-10 02:48:13 +03:00
`mkDummySrc :: set -> drv`
Converts a given source directory of a cargo workspace to the smallest, most
trivial form needed to build all dependencies such that their artifacts can be
cached.
The actual source files of the project itself are ignored/replaced with
empty programs, such that changes to the source files does not invalidate any
build caches. More specifically:
* The Cargo.lock file is kept as-is
- Any changes to it will invalidate the build cache
* Any cargo configuration files (i.e. files name `config` or `config.toml` whose
parent directory is named `.cargo` ) are kept as-is.
- Any changes to these files will invalidate the build cache
* Any files named `Cargo.toml` are reduced via `cleanCargoToml` and the result
is kept. Only the following changes will result in invalidating the build
cache:
- Any changes to listed dependencies
- Any changes to feature definitions
- Any changes to the workspace member metadata
- Any changes to the `[package]` definition such as name and version
- Any changes to the name or path of any target (such as benches, bins,
examples, libs, or tests)
#### Required attributes
* `src` : a source directory which should be turned into a "dummy" form
#### Optional attributes
* `cargoLock` : a path to a Cargo.lock file
- Default value: `src + /Cargo.lock`
2022-11-28 06:23:35 +03:00
* `dummyrs` : a path to a file which will be used in place of all dummy rust
files (e.g. `main.rs` , `lib.rs` , etc.). This can be useful to customize dummy
source files (e.g. enable certain lang features for a given target).
- Default value: an empty `fn main` declaration and conditionally enabled
`#![no_std]` if the `target_os` cfg is set to `"none"` or `"uefi"` .
2022-09-27 09:25:09 +03:00
* `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
# The _entire_ source of the project. mkDummySrc will automatically
# filter out irrelevant files as described above
2023-03-20 07:13:23 +03:00
src = craneLib.path ./.;
2022-09-27 09:25:09 +03:00
2023-11-26 21:15:16 +03:00
dotCargoOnly = lib.cleanSourceWith {
inherit src;
# Only keep `*/.cargo/*`
filter = path: _type: lib.hasInfix ".cargo" path;
};
in
mkDummySrc {
inherit src;
# Note that here we scope the path to only contain any `.cargo` directory
# and its contents 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 incorrectly consume the entire source root,
# and therefore rebuild everything when any file changes, which defeats
# artifact caching.
2022-09-27 09:25:09 +03:00
#
# Also note the `--no-target-directory` flag which ensures the results are
# copied to `$out/.cargo` instead of something like `$out/HASH-.cargo`
extraDummyScript = ''
2023-11-26 21:15:16 +03:00
cp -r ${dotCargoOnly} --no-target-directory $out/
2022-09-27 09:25:09 +03:00
'';
}
```
2022-01-10 02:48:13 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.overrideToolchain`
2022-06-26 21:25:40 +03:00
2024-06-29 21:29:52 +03:00
`overrideToolchain :: (set -> drv) -> set`
`overrideToolchain :: drv -> set` (legacy)
2022-06-26 21:25:40 +03:00
A convenience method to override and use tools (like `cargo` , `clippy` ,
`rustfmt` , `rustc` , etc.) from one specific toolchain. The input should be a
single derivation which contains all the tools as binaries. For example, this
can be the output of `oxalica/rust-overlay` .
2024-06-29 21:29:52 +03:00
Note that in order to best support cross compilation, `overrideToolchain` should
be provided a function (whose argument is a cross-compilation aware version of
`pkgs` ) which constructs the toolchain:
2022-06-26 21:25:40 +03:00
```nix
2024-06-29 21:29:52 +03:00
craneLib.overrideToolchain (p: myCustomToolchainForPkgs p)
2022-06-26 21:25:40 +03:00
```
2023-03-20 07:13:23 +03:00
### `craneLib.path`
2023-03-20 06:57:15 +03:00
`path :: path -> drv`
`path :: set -> drv`
A convenience wrapper around `builtins.path` which will automatically set the
path's `name` to the workspace's package name (or a placeholder value of
`"source"` if a name cannot be determined).
It should be used anywhere a relative path like `./.` or `./..` is needed so
that the result is reproducible and caches can be reused. Otherwise the store
path [will depend on the name of the parent
directory](https://nix.dev/anti-patterns/language#reproducibility-referencing-top-level-directory-with) which may cause unnecessary rebuilds.
```nix
2023-03-20 07:13:23 +03:00
craneLib.path ./.
2023-03-20 06:57:15 +03:00
# "/nix/store/wbhf6c7wiw9z53hsn487a8wswivwdw81-source"
```
```nix
2023-03-20 07:13:23 +03:00
craneLib.path ./checks/simple
2023-03-20 06:57:15 +03:00
# "/nix/store/s9scn97c86kqskf7yv5n2k85in5y5cmy-simple"
```
It is also possible to use as a drop in replacement for `builtins.path` :
```nix
2023-03-20 07:13:23 +03:00
craneLib.path {
2023-03-20 06:57:15 +03:00
path = ./.;
name = "asdf";
}
# "/nix/store/23zy3c68v789cg8sysgba0rbgbfcjfhn-asdf"
```
2023-03-20 07:13:23 +03:00
### `craneLib.registryFromDownloadUrl`
2022-01-30 03:12:22 +03:00
`registryFromDownloadUrl :: set -> set`
Prepares a crate registry into a format that can be passed directly to
`appendCrateRegistries` using the registry's download URL.
If the registry in question has a stable download URL (which either never
changes, or it does so very infrequently), then `registryFromDownloadUrl` is a
great and lightweight choice for including the registry. To get started, look up
the
[`config.json` ](https://github.com/rust-lang/crates.io-index/blob/24ecfa9c82456a79ec115736f1fcefc0be375b52/config.json#L2 ) at the registry's root and copy the value of the `dl` entry.
If the registry's download endpoint changes more frequently and you would like
to infer the configuration directly from a git revision, consider using
`registryFromGitIndex` as an alternative.
2022-11-21 03:22:13 +03:00
If the registry needs a special way of accessing crate sources the
`fetchurlExtraArgs` set can be used to influence the behavior of fetching the
crate sources (e.g. by setting `curlOptsList` )
2022-01-30 03:12:22 +03:00
#### Required attributes
* `dl` : the value of the `dl` entry in the registry's `config.json` file
* `indexUrl` : an HTTP URL to the index
2022-11-21 03:22:13 +03:00
#### Optional attributes
* `fetchurlExtraArgs` : a set of arguments which will be passed on to the
`fetchurl` for each crate being sourced from this registry
2022-01-30 03:12:22 +03:00
```nix
2023-03-20 07:13:23 +03:00
craneLib.registryFromDownloadUrl {
2024-03-20 00:55:28 +03:00
dl = "https://static.crates.io/crates";
2022-01-30 03:12:22 +03:00
indexUrl = "https://github.com/rust-lang/crates.io-index";
}
2022-11-21 03:22:13 +03:00
# {
# "registry+https://github.com/rust-lang/crates.io-index" = {
2024-03-20 00:55:28 +03:00
# downloadUrl = "https://static.crates.io/crates/{crate}/{version}/download";
2022-11-21 03:22:13 +03:00
# fetchurlExtraArgs = {};
# };
# }
2022-01-30 03:12:22 +03:00
```
2023-03-20 07:13:23 +03:00
### `craneLib.registryFromGitIndex`
2022-01-30 03:12:22 +03:00
`registryFromGitIndex :: set -> set`
Prepares a crate registry into a format that can be passed directly to
`appendCrateRegistries` using a revision of the registry index to infer the
download URL.
Note that the specified git revision _does not need to track updates to the
index itself_ as long as the pinned revision contains the most recent version of
the `config.json` file. In other words, this commit revision only needs to be
updated if the `config.json` file changes.
Also note that this approach means that the contents of the entire index at the
specified revision will be added to the Nix store during evaluation time, and
that IFD will need to be enabled. If this is unsatisfactory, consider using
`registryFromDownloadUrl` as a simpler alternative.
2022-11-21 03:22:13 +03:00
If the registry needs a special way of accessing crate sources the
`fetchurlExtraArgs` set can be used to influence the behavior of fetching the
crate sources (e.g. by setting `curlOptsList` )
2022-01-30 03:12:22 +03:00
#### Required attributes
2022-01-31 02:10:28 +03:00
* `indexUrl` : an HTTP URL to the index
2022-01-30 03:12:22 +03:00
* `rev` : any git revision which contains the latest `config.json` definition
2022-11-21 03:22:13 +03:00
#### Optional attributes
* `fetchurlExtraArgs` : a set of arguments which will be passed on to the
`fetchurl` for each crate being sourced from this registry
2022-01-30 03:12:22 +03:00
```nix
2023-03-20 07:13:23 +03:00
craneLib.registryFromGitIndex {
2022-01-30 03:12:22 +03:00
url = "https://github.com/Hirevo/alexandrie-index";
rev = "90df25daf291d402d1ded8c32c23d5e1498c6725";
}
2022-11-21 03:22:13 +03:00
# {
# "registry+https://github.com/Hirevo/alexandrie-index" = {
# downloadUrl = "https://crates.polomack.eu/api/v1/crates/{crate}/{version}/download";
# fetchurlExtraArgs = {};
# };
# }
2022-01-30 03:12:22 +03:00
```
2023-03-20 07:13:23 +03:00
### `craneLib.urlForCargoPackage`
2022-01-10 02:48:13 +03:00
2022-11-21 03:22:13 +03:00
`urlForCargoPackage :: set -> set`
Returns info pertaining to the URL for downloading a particular crate if the
crate's registry is configured (an error will be thrown if it is not).
2022-01-10 02:48:13 +03:00
2022-11-21 03:22:13 +03:00
The result will contain two attributes:
- `url` : A string representing the URL at which the crate can be fetched
- `fetchurlExtraArgs` : A set of attributes specific to this registry which will
be passed on to the `fetchurl` invocation.
2022-01-10 02:48:13 +03:00
#### Required input attributes
* `name` : the name of the crate
* `source` : the source key recorded in the Cargo.lock file
* `version` : the version of the crate
2023-03-20 07:13:23 +03:00
### `craneLib.vendorCargoDeps`
2022-01-10 02:48:13 +03:00
`vendorCargoDeps :: set -> drv`
Creates a derivation which will download all crates referenced by a Cargo.lock
file, and prepare a vendored directory which cargo can use for subsequent builds
without needing network access.
2022-01-31 01:30:15 +03:00
Each unique crate index will be vendored as its own subdirectory within the
output of the derivation. A `config.toml` file will also be placed at the root
of the output which will contain the necessary configurations to point cargo to
the vendored directories (i.e. this configuration can be appended to the
`.cargo/config.toml` definition of the project).
2022-01-10 02:48:13 +03:00
#### Input attributes
* `src` : a directory which includes a Cargo.lock file at its root.
* `cargoLock` : a path to a Cargo.lock file
* `cargoLockContents` : the contents of a Cargo.lock file as a string
2023-03-20 03:33:17 +03:00
* `cargoLockParsed` : the parsed contents of Cargo.lock as an attribute set
2022-01-10 02:48:13 +03:00
At least one of the above attributes must be specified, or an error will be
raised during evaluation.
2023-09-22 07:08:53 +03:00
#### Optional attributes
* `outputHashes` : a mapping of package-source to the sha256 of the (unpacked)
download. Useful for supporting fully offline evaluations.
- Default value: `[]`
2024-05-19 02:39:46 +03:00
* `overrideVendorCargoPackage` : a function that will be called on every crate
vendored from a cargo registry, which allows for modifying the derivation
which will unpack the cargo tarball (e.g. to patch the crate source).
It will be called with the following parameters:
1. The `Cargo.lock` entry for that package (to allow conditional overrides
based on the package name/version/source, etc.)
1. The default `downloadCargoPackage` derivation
- Default value: `_p: drv: drv`
* `overrideVendorGitCheckout` : a function that will be called on every unique
checkout vendored from a git repository, which allows for modifying the
derivation which will unpack the cargo crates found in the checkout (e.g. to
patch the crate sources). It will be called with the following
parameters:
1. A list of the `Cargo.lock` entries for each package which shares the same
repo URL and revision to checkout (to allow conditional overrides based on
the repo/checkout etc.)
1. The default `downloadCargoPackageFromGit` derivation
- Default value: `_ps: drv: drv`
2023-09-22 07:08:53 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.vendorCargoRegistries`
2022-02-07 04:16:49 +03:00
`vendorCargoRegistries :: set -> set`
Creates the derivations necessary to download all crates from all registries
referenced by a `Cargo.lock` file, and prepare the vendored directories which
cargo can use for subsequent builds without needing network access.
#### Input attributes
* `lockPackages` : a list of all `[[package]]` entries found in the project's
2022-06-01 18:49:09 +03:00
`Cargo.lock` file (parsed via `builtins.fromTOML` )
2022-02-07 04:16:49 +03:00
2023-04-02 20:57:17 +03:00
#### Optional attributes
* `cargoConfigs` : a list of paths to all `.cargo/config.toml` files which may
appear in the project. Ignored if `registries` is set.
- Default value: `[]`
2024-05-19 02:39:46 +03:00
* `overrideVendorCargoPackage` : a function that will be called on every crate
vendored from a cargo registry, which allows for modifying the derivation
which will unpack the cargo tarball (e.g. to patch the crate source).
It will be called with the following parameters:
1. The `Cargo.lock` entry for that package (to allow conditional overrides
based on the package name/version/source, etc.)
1. The default `downloadCargoPackage` derivation
- Default value: `_p: drv: drv`
2023-04-02 20:57:17 +03:00
* `registries` : an attrset of registry names to their index URL. The default
("crates-io") registry need not be specified, as it will automatically be
available, but it can be overridden if required.
- Default value: if not specified, `cargoConfigs` will be used to identify any
configured registries
2022-02-07 04:16:49 +03:00
#### Output attributes
* `config` : the configuration entires needed to point cargo to the vendored
crates. This is intended to be appended to `$CARGO_HOME/config.toml` verbatim
* `sources` : an attribute set of all the newly created cargo sources' names to
their location in the Nix store
2023-03-20 07:13:23 +03:00
### `craneLib.vendorGitDeps`
2022-02-08 07:03:12 +03:00
`vendorGitDeps :: set -> set`
Creates the derivations necessary to download all crates from all git
dependencies referenced by a `Cargo.lock` file, and prepare the vendored
directories which cargo can use for subsequent builds without needing network
access.
#### Input attributes
* `lockPackages` : a list of all `[[package]]` entries found in the project's
2022-06-01 18:49:09 +03:00
`Cargo.lock` file (parsed via `builtins.fromTOML` )
2022-02-08 07:03:12 +03:00
2023-09-22 07:08:53 +03:00
#### Optional attributes
* `outputHashes` : a mapping of package-source to the sha256 of the (unpacked)
download. Useful for supporting fully offline evaluations.
- Default value: `[]`
2024-05-19 02:39:46 +03:00
* `overrideVendorGitCheckout` : a function that will be called on every unique
checkout vendored from a git repository, which allows for modifying the
derivation which will unpack the cargo crates found in the checkout (e.g. to
patch the crate sources). It will be called with the following
parameters:
1. A list of the `Cargo.lock` entries for each package which shares the same
repo URL and revision to checkout (to allow conditional overrides based on
the repo/checkout etc.)
1. The default `downloadCargoPackageFromGit` derivation
- Default value: `_ps: drv: drv`
2023-09-22 07:08:53 +03:00
2022-02-08 07:03:12 +03:00
#### Output attributes
* `config` : the configuration entires needed to point cargo to the vendored
sources. This is intended to be appended to `$CARGO_HOME/config.toml` verbatim
* `sources` : an attribute set of all the newly created cargo sources' names to
their location in the Nix store
2023-04-02 20:57:17 +03:00
### `craneLib.vendorMultipleCargoDeps`
`vendorMultipleCargoDeps :: set -> drv`
Creates a derivation which will download all crates referenced by several
`Cargo.lock` files, and prepare a vendored directory which cargo can use for
subsequent builds without needing network access. Duplicate packages listed in
different `Cargo.lock` files will automatically be filtered out.
Each unique crate index will be vendored as its own subdirectory within the
output of the derivation. A `config.toml` file will also be placed at the root
of the output which will contain the necessary configurations to point cargo to
the vendored directories (i.e. this configuration can be appended to the
`.cargo/config.toml` definition of the project).
#### Optional attributes
* `cargoConfigs` : a list of paths to all `.cargo/config.toml` files which may
appear in the project. Ignored if `registries` is set.
- Default value: `[]`
* `cargoLockContentsList` : a list of strings representing the contents of
different `Cargo.lock` files to be included while vendoring. The strings will
automatically be parsed during evaluation.
- Default value: `[]`
* `cargoLockList` : a list of paths to different `Cargo.lock` files to be
included while vendoring. The paths will automatically be read and parsed
during evaluation.
- Default value: `[]`
* `cargoLockParsedList` : a list of attrsets representing the parsed contents of
different `Cargo.lock` files to be included while vendoring.
- Default value: `[]`
2023-09-22 07:08:53 +03:00
* `outputHashes` : a mapping of package-source to the sha256 of the (unpacked)
download. Useful for supporting fully offline evaluations.
- Default value: `[]`
2024-05-19 02:39:46 +03:00
* `overrideVendorCargoPackage` : a function that will be called on every crate
vendored from a cargo registry, which allows for modifying the derivation
which will unpack the cargo tarball (e.g. to patch the crate source).
It will be called with the following parameters:
1. The `Cargo.lock` entry for that package (to allow conditional overrides
based on the package name/version/source, etc.)
1. The default `downloadCargoPackage` derivation
- Default value: `_p: drv: drv`
* `overrideVendorGitCheckout` : a function that will be called on every unique
checkout vendored from a git repository, which allows for modifying the
derivation which will unpack the cargo crates found in the checkout (e.g. to
patch the crate sources). It will be called with the following
parameters:
1. A list of the `Cargo.lock` entries for each package which shares the same
repo URL and revision to checkout (to allow conditional overrides based on
the repo/checkout etc.)
1. The default `downloadCargoPackageFromGit` derivation
- Default value: `_ps: drv: drv`
2023-04-02 20:57:17 +03:00
* `registries` : an attrset of registry names to their index URL. The default
("crates-io") registry need not be specified, as it will automatically be
available, but it can be overridden if required.
- Default value: if not specified, `cargoConfigs` will be used to identify any
configured registries
2023-03-20 07:13:23 +03:00
### `craneLib.writeTOML`
2022-01-10 02:48:13 +03:00
`writeTOML :: String -> String -> drv`
Takes a file name and an attribute set, converts the set to a TOML document and
writes it to a file with the given name.
```nix
2023-03-20 07:13:23 +03:00
craneLib.writeTOML "foo.toml" { foo.bar = "baz"; }
2022-01-10 02:48:13 +03:00
# «derivation /nix/store/...-foo.toml.drv»
```
2022-01-16 04:59:34 +03:00
## Hooks
2023-03-20 07:13:23 +03:00
### `craneLib.cargoHelperFunctionsHook`
2022-07-23 20:04:55 +03:00
Defines helper functions for internal use. It is probably not a great idea to
depend on these directly as their behavior can change at any time, but it is
worth documenting them just in case:
* Defines a `cargo()` function which will immediately invoke the `cargo` command
found on the `$PATH` after echoing the exact arguments that were passed in.
Useful for automatically logging all cargo invocations to the log.
2022-07-23 21:10:45 +03:00
* Defines a `cargoWithProfile()` function which will invoke `cargo` with the
provided arguments. If `$CARGO_PROFILE` is set, then `--profile
$CARGO_PROFILE` will be injected into the `cargo` invocation
- Note: a default value of `$CARGO_PROFILE` is set via
`configureCargoCommonVarsHook` . You can set `CARGO_PROFILE = "something"` in
your derivation to change which profile is used, or set `CARGO_PROFILE =
2022-07-24 00:11:53 +03:00
"";` to omit it altogether.
2022-07-23 20:04:55 +03:00
2023-03-20 07:13:23 +03:00
### `craneLib.configureCargoCommonVarsHook`
2022-01-16 04:59:34 +03:00
Defines `configureCargoCommonVars()` which will set various common cargo-related
variables, such as honoring the amount of parallelism dictated by Nix, disabling
incremental artifacts, etc. More specifically:
* `CARGO_BUILD_INCREMENTAL` is set to `false` if not already defined
* `CARGO_BUILD_JOBS` is set to `$NIX_BUILD_CORES` if not already defined
* `CARGO_HOME` is set to `$PWD/.cargo-home` if not already defined.
- The directory that `CARGO_HOME` points to will be created
2022-07-23 21:10:45 +03:00
* `CARGO_PROFILE` is set to `release` if not already defined.
- Note that this is is used internally specify a cargo profile (e.g. `cargo
build --profile release`) and not something natively understood by cargo.
2022-01-16 04:59:34 +03:00
* `RUST_TEST_THREADS` is set to `$NIX_BUILD_CORES` if not already defined
**Automatic behavior:** runs as a post-patch hook
2023-03-20 07:13:23 +03:00
### `craneLib.configureCargoVendoredDepsHook`
2022-01-16 04:59:34 +03:00
Defines `configureCargoVendoredDeps()` which will prepare cargo to use a
directory of vendored crate sources. It takes two positional arguments:
1. a path to the vendored sources
* If not specified, the value of `$cargoVendorDir` will be used
* If `cargoVendorDir` is not specified, an error will be raised
1. a path to a cargo config file to modify
* If not specified, the value of `$CARGO_HOME/config.toml` will be used
* This cargo config file will be appended with a stanza which will instruct
2022-01-31 01:30:15 +03:00
cargo to use the vendored sources (instead of downloading the sources
directly) as follows:
- If the vendored directory path contains a file named `config.toml` ,
then its contents will be appended to the specified cargo config path.
- Otherwise the entire vendored directory path will be treated as if it
only vendors the crates.io index and will be configured as such.
2022-01-16 04:59:34 +03:00
**Automatic behavior:** if `cargoVendorDir` is set, then
`configureCargoVendoredDeps "$cargoVendorDir" "$CARGO_HOME/config.toml"` will be
run as a pre configure hook.
2023-03-20 07:13:23 +03:00
### `craneLib.inheritCargoArtifactsHook`
2022-01-16 04:59:34 +03:00
Defines `inheritCargoArtifacts()` which will pre-populate cargo's artifact
directory using a previous derivation. It takes two positional arguments:
1. a path to the previously prepared artifacts
* If not specified, the value of `$cargoArtifacts` will be used
* If `cargoArtifacts` is not specified, an error will be raised
* If the specified path is a directory which contains a file called
2022-10-29 21:48:29 +03:00
`target.tar.zst` , then that file will be used as specified below
* If the specified path is a file (and not a directory) it is assumed that it
contains a zstd compressed tarball and will be decompressed and unpacked
into the specified cargo artifacts directory
* If the specified path is a directory which contains another directory
called `target` , then that directory will be used as specified below
* If the specified path is a directory, its contents will be copied into the
specified cargo artifacts directory
2022-01-16 04:59:34 +03:00
* The previously prepared artifacts are expected to be a zstd compressed
tarball
1. the path to cargo's artifact directory, where the previously prepared
artifacts should be unpacked
* If not specified, the value of `$CARGO_TARGET_DIR` will be used
* If `CARGO_TARGET_DIR` is not set, cargo's default target location (i.e.
`./target` ) will be used.
2023-06-21 04:13:46 +03:00
Note that as an optimization, some dependency artifacts will be symlinked
instead of (deeply) copied to `$CARGO_TARGET_DIR` . To disable this behavior set
`doNotLinkInheritedArtifacts` , and all artifacts will be copied as plain,
writable files.
2022-01-16 04:59:34 +03:00
**Automatic behavior:** if `cargoArtifacts` is set, then
`inheritCargoArtifacts "$cargoArtifacts" "$CARGO_TARGET_DIR"` will be run as a
post patch hook.
2022-10-09 05:03:05 +03:00
**Required nativeBuildInputs**: assumes `zstd` is available on the `$PATH`
2023-03-20 07:13:23 +03:00
### `craneLib.installCargoArtifactsHook`
2022-01-16 04:59:34 +03:00
2022-10-29 21:48:29 +03:00
Defines `compressAndInstallCargoArtifactsDir()` which handles installing
cargo's artifact directory to the derivation's output as a zstd compressed
tarball. It takes two positional arguments:
2022-01-16 04:59:34 +03:00
1. the installation directory for the output.
2022-10-29 21:48:29 +03:00
* An error will be raised if not specified
2022-01-16 04:59:34 +03:00
* Cargo's artifact directory will be compressed as a reproducible tarball
with zstd compression. It will be written to this directory and named
`target.tar.zstd`
2022-10-29 21:48:29 +03:00
1. the path to cargo's artifact directory
* An error will be raised if not specified
2023-11-05 23:34:43 +03:00
If `$zstdCompressionExtraArgs` is set, `compressAndInstallCargoArtifactsDir()`
will pass its contents along to `zstd` when compressing artifacts.
2022-10-29 21:48:29 +03:00
Defines `dedupAndInstallCargoArtifactsDir()` which handles installing
cargo's artifact directory to the derivation's output after deduplicating
identical files against a directory of previously prepared cargo artifacts.
It takes three positional arguments:
1. the installation directory for the output.
* An error will be raised if not specified
* If the specified path is a directory which exists then the current cargo
artifacts will be compared with the contents of said directory. Any files
whose contents and paths match will be symbolically linked together to
reduce the size of the data stored in the Nix store.
1. the path to cargo's artifact directory
* An error will be raised if not specified
1. a path to the previously prepared cargo artifacts
* An error will be raised if not specified
* `/dev/null` can be specified here if there is no previous directory to
deduplicate against
Defines `prepareAndInstallCargoArtifactsDir()` which handles installing cargo's
artifact directory to the derivation's output. It takes three positional
arguments:
1. the installation directory for the output.
* If not specified, the value of `$out` will be used
* Cargo's artifact directory will be installed based on the installation mode
selected below
2022-01-16 04:59:34 +03:00
1. the path to cargo's artifact directory
* If not specified, the value of `$CARGO_TARGET_DIR` will be used
* If `CARGO_TARGET_DIR` is not set, cargo's default target location (i.e.
`./target` ) will be used.
2022-10-29 21:48:29 +03:00
1. the installation mode to apply
2023-10-20 17:11:01 +03:00
* If specified, the value of `$installCargoArtifactsMode` will be used,
2023-12-19 06:30:44 +03:00
otherwise, a default value of `"use-zstd"` will be used
2022-10-29 21:48:29 +03:00
* If set to "use-symlink" then `dedupAndInstallCargoArtifactsDir()` will be
used.
- If `$cargoArtifacts` is defined and `$cargoArtifacts/target` is a valid
directory, it will be used during file deduplication
* If set to "use-zstd" then `compressAndInstallCargoArtifactsDir()` will be
used.
* Otherwise an error will be raised if the mode is not recognized
2022-01-16 04:59:34 +03:00
**Automatic behavior:** if `doInstallCargoArtifacts` is set to `1` , then
`prepareAndInstallCargoArtifactsDir "$out" "$CARGO_TARGET_DIR"` will be run as a
post install hook.
2022-10-09 05:03:05 +03:00
**Required nativeBuildInputs**: assumes `zstd` is available on the `$PATH`
2023-03-20 07:13:23 +03:00
### `craneLib.installFromCargoBuildLogHook`
2022-01-16 04:59:34 +03:00
Defines `installFromCargoBuildLog()` which will use a build log produced by
cargo to find and install any binaries and libraries which have been built. It
takes two positional arguments:
1. a path to where artifacts should be installed
* If not specified, the value of `$out` will be used
* Binaries will be installed in a `bin` subdirectory
* Libraries will be installed in a `lib` subdirectory
- Note that only library targets with the `staticlib` and `cdylib`
crate-types will be installed. Library targets with the `rlib` crate-type
will be ignored
1. a path to a JSON formatted build log written by cargo
* If not specified, the value of `$cargoBuildLog` will be used
* If `cargoBuildLog` is not set, an error will be raised
* This log can be captured, for example, via `cargo build --message-format
json-render-diagnostics >cargo-build.json`
**Automatic behavior:** none
2022-09-25 02:45:56 +03:00
2022-10-09 05:03:05 +03:00
**Required nativeBuildInputs**: assumes `cargo` and `jq` are available on the `$PATH`
2023-03-20 07:13:23 +03:00
### `craneLib.removeReferencesToVendoredSourcesHook`
2022-09-25 02:45:56 +03:00
Defines `removeReferencesToVendoredSources()` which handles removing all
references to vendored sources from the installed binaries, which ensures that
nix does not consider the binaries as having a (runtime) dependency on the
sources themselves. It takes two positional arguments:
1. the installation directory for the output.
* If not specified, the value of `$out` will be used
* If `out` is not specified, an error will be raised
1. a path to the vendored sources
* If not specified, the value of `$cargoVendorDir` will be used
* If `cargoVendorDir` is not specified, an error will be raised
* Note: it is expected that this directory has the exact structure as would
2023-03-20 07:13:23 +03:00
be produced by `craneLib.vendorCargoDeps`
2022-09-25 02:45:56 +03:00
2023-10-15 23:09:08 +03:00
Any patched binaries on `aarch64-darwin` will be [signed ](https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html ). You can disable this functionality by setting `doNotSign` .
2022-09-25 02:45:56 +03:00
**Automatic behavior:** if `cargoVendorDir` is set and
`doNotRemoveReferencesToVendorDir` is not set, then
`removeReferencesToVendoredSources "$out" "$cargoVendorDir"` will be run as a
post install hook.
2023-10-15 04:42:24 +03:00
### `craneLib.replaceCargoLockHook`
Defines `replaceCargoLock()` which handles replacing or inserting a specified
`Cargo.lock` file in the current directory. It takes one positional argument:
1. a file which will be copied to `Cargo.lock` in the current directory
* If not specified, the value of `$cargoLock` will be used
* If `$cargoLock` is not set, an error will be raised
**Automatic behavior:** if `cargoLock` is set and
`doNotReplaceCargoLock` is not set, then `replaceCargoLock "$cargoLock"` will be
2023-10-17 06:31:33 +03:00
run as a pre patch hook.