No description
Find a file
2020-02-18 15:55:26 +01:00
.github Rename workflow and fix badge 2020-02-12 14:46:10 +01:00
builtins Implement toToml for 'int' 2020-01-14 19:52:10 +01:00
docparse Only accept function in allowFun params 2020-02-10 12:53:55 +01:00
nix Add script/test 2019-12-02 16:42:05 +01:00
script Generate configuration documentation from code 2019-12-02 20:30:50 +01:00
test Add reproduction for workspace build.rs failure 2020-02-11 23:44:42 +01:00
.gitignore Basic flake.nix 2020-01-15 20:41:43 +01:00
build.nix Forward extra arguments to mkDerivation 2020-02-18 15:55:26 +01:00
config.nix Set doCheck in test.nix 2020-02-18 15:55:26 +01:00
default.nix Forward extra arguments to mkDerivation 2020-02-18 15:55:26 +01:00
flake.nix Basic flake.nix 2020-01-15 20:41:43 +01:00
lib.nix Add support for new lockfile format 2020-02-07 17:35:30 +01:00
LICENSE Add MIT license 2019-12-01 14:07:23 +01:00
README.md Set doCheck in test.nix 2020-02-18 15:55:26 +01:00
README.tpl.md Forward extra arguments to mkDerivation 2020-02-18 15:55:26 +01:00
test.nix Set doCheck in test.nix 2020-02-18 15:55:26 +01:00

Naersk

GitHub Actions

Nix support for building cargo crates.

Install

Use niv:

$ niv add nmattia/naersk

And then

let
    pkgs = import <nixpkgs> {};
    sources = import ./nix/sources.nix;
    naersk = pkgs.callPackage sources.naersk {};
in naersk.buildPackage ./path/to/rust

NOTE: ./path/to/rust/ should contain a Cargo.lock.

Configuration

The buildPackage function also accepts an attribute set. The attributes are described below. Any attribute that is not listed below will be forwarded as is to stdenv.mkDerivation. When the argument passed in not an attribute set, e.g.

naersk.buildPackage theArg

it is converted to an attribute set equivalent to { root = theArg; }.

Attribute Description
name The name of the derivation.
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.
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.
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" ]
cargoTestCommands The commands to run in the checkPhase. Do not forget to set doCheck. 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 <OPTS>. 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"'' ]
buildInputs Extra buildInputs to all derivations. Default: []
cargoOptions Options passed to all cargo commands, i.e. cargo <OPTS> .... These options can be accessed during the build through the environment variable cargo_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: [ "-Z" "unstable-options" ]
doDoc When true, cargo doc is run and a new output doc is generated. Default: false
release When true, all cargo builds are run with --release. The environment variable cargo_release is set to --release iff this option is set. Default: true
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.
Note: this relies on cargo's --message-format argument, set in the default cargoBuildOptions. Default: true
copyBinsFilter A jq filter for selecting which build artifacts to release. This is run on cargo's --message-format JSON output.
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
compressTarget When true, the build output of intermediary builds is compressed with Zstandard. This reduces the size of closures. Default: true
copyTarget When true, the target/ directory is copied to $out. Default: false
usePureFromTOML Whether to use the fromTOML built-in or not. When set to false the python package remarshal is used instead (in a derivation) and the JSON output is read with builtins.fromJSON. This is a workaround for old versions of Nix. May be used safely from Nix 2.3 onwards where all bugs in builtins.fromTOML seem to have been fixed. Default: true

Comparison

There are two other notable Rust frameworks in Nix: rustPlatform and carnix.

naersk uses cargo directly, as opposed to carnix which emulates cargo's build logic. Moreover naersk sources build information directly from the project's Cargo.lock which makes any code generation unnecessary.

For the same reason, naersk does not need anything like rustPlatform's cargoSha256. All crates are downloaded using the sha256 checksums provided in the project's Cargo.lock.

Finally naersk supports incremental builds by first performing a dependency-only build, and then a build that depends on the top-level crate's code and configuration.