Skip to content

CHANGELOG

  • #520: The command line interface now uses clap 4 instead of structopt and clap 2. All subcommands, flags and defaults stay the same, but:

    • usage errors, e.g. an unknown or missing argument, now exit with status 2 instead of 1. Other errors still exit with status 1.
    • --help output and error messages look different: there is no crate2nix <version> header line any more, the usage line is headed Usage: instead of USAGE:, options are listed in definition order, and value names are upper case (e.g. --output <OUTPUT>).

    Shell completions for crate2nix completions are generated by clap_complete.

  • #439: The hashes of git dependencies on a branch, a tag or the default branch are now stored in crate-hashes.json under a key that contains the locked commit, e.g. name 1.0.0 (git+https://github.com/org/repo?branch=main#<commit>). Before, crate2nix kept using the hash of the old commit after cargo update moved such a dependency to a new commit (or after switching to another tag with the same crate version), which broke the build with a hash mismatch. Existing entries without the commit are ignored, so crate2nix generate prefetches these dependencies once more and rewrites crate-hashes.json. Dependencies pinned with rev = "..." keep their keys. With tools.nix (generatedCargoNix), a checked-in crate-hashes.json with the old keys makes the build fail with a diff until it is regenerated. Hashes of git dependencies are also no longer reused for a crates.io dependency with the same name and version.

  • #561 (based on #276 by Andrew Poelstra): generatedCargoNix and appliedCargoNix take an optional cargoLock argument, a Cargo.lock to use instead of the one in src. This works for libraries without a checked-in Cargo.lock and for testing one source against several lock files. See Using another Cargo.lock.

  • #144 (in part): runTests also takes a list of the kinds of tests to run, named after cargo’s target selection flags: "lib" (--lib), "bins" (--bins) and "tests" (--test '*'), e.g. runTests = [ "lib" "bins" ] to run only the unit tests. runTests = true stays [ "lib" "bins" "tests" ], runTests = [ ] runs no tests and unknown kinds are an evaluation error. Doctests ("doc") are not supported yet. The test executables in the test build’s tests/ are now named <target name>-<hash> like cargo names them, and the test build writes tests.json, which records the target name and kind (lib, bin or test) of each of them; the test runner picks the executables to run from it. The check that an integration test doesn’t have the name of a binary (#530) moved from the test runner to the test build. The test builds of tested crates change, so expect them to be rebuilt once. See Choosing which tests run.

  • #482, #244, #196: rootCrate and every entry of workspaceMembers have a buildTests attribute that builds the crate’s test executables with its dev-dependencies without running them, e.g. to run integration tests in a NixOS VM test. It is overridable like build, and build.override { buildTests = true; } gives the same derivation. The test derivation of a build with runTests exposes the same test executables as test.testCrate. Thank you, @emanueljg! See Building the test binaries without running them.

  • crate2nix ships its own variant of nixpkgs’ buildRustCrate: a copy of nixpkgs’ pkgs/build-support/rust/build-rust-crate from a pinned revision (currently without changes), in nix/build-rust-crate with a script to update it. It is a drop-in replacement for pkgs.buildRustCrate and the base for upcoming features that need changes to buildRustCrate. It is opt-in for now: pass buildRustCrateForPkgs = crate2nix.lib.buildRustCrateForPkgs (or crate2nix.tools.${system}.buildRustCrateForPkgs, or buildRustCrateForPkgs of tools.nix) to a Cargo.nix or to appliedCargoNix (which takes the new argument buildRustCrateForPkgs). Both keep using pkgs.buildRustCrate by default; appliedCargoNix will switch to crate2nix’s buildRustCrate once it has its first own change (e.g. doctests). Everything works with both, but future features that need crate2nix’s buildRustCrate will fail with a clear error without it. See crate2nix’s buildRustCrate.

  • #513: Opt-in workspace feature unification: with the new Cargo.nix argument featureUnification = "workspace", the features of all workspace members are resolved together, so that the members share one build of each dependency (and their test builds another one), like cargo’s resolver.feature-unification = "workspace". The default, "package", keeps resolving each member on its own and gives the same derivations as before. See Feature unification in workspaces.

  • #542, #490, #496: Opt-in forwarding of each crate’s license, repository, and homepage from its Cargo.toml into the generated Cargo.nix, for consumers such as SBOM generators. Enable it with crate2nix generate --crate-metadata or "crateMetadata": true in crate2nix.json; see Generate Cargo.nix manually. Thank you, @charludo and @ksil!

  • #538 (based on #497 by Varun Shah): allWorkspaceMembers evaluates all members over one shared table of crate derivations, instead of instantiating the crates they share once per member, which makes evaluating large workspaces much faster. The new allWorkspaceMembers.members.<name> gives the derivation of each member, identical to workspaceMembers.<name>.build, for flakes that expose members separately. See Building all or many workspace members.

  • #540: With featureUnification = "workspace", the builds of all workspace members share their crate derivations, lazily: evaluating every workspaceMembers.<name>.build of a 300-member workspace takes 0.5 s instead of 13 s. The derivations are unchanged, except that a test build whose dev-dependency depends back on the tested member now evaluates (the dev-dependency gets the member without its dev-dependencies) instead of recursing infinitely. The docs now recommend featureUnification = "workspace" for workspaces that are built as a whole.

  • #543 (based on #495 by kkovalchick): Features are resolved like cargo’s feature resolver “2” when the project uses it, which edition 2021 and later imply: build dependencies, proc-macros and the crates they depend on get their own features, so that features only needed at build time no longer leak into the target build. crate2nix generate reads the resolver from Cargo.toml and renders it as the default of the new Cargo.nix argument resolverVersion. Regenerating the Cargo.nix of such a project changes the derivations of the crates used in both roles with different features, and of the crates depending on them. The new mergePackageFeaturesByKind and mergeWorkspaceFeaturesByKind return the features per kind; with resolver “2” or later, debugCrate’s mergedPackageFeatures does, too. See Feature resolver.

  • Issue #339: The crateConfig argument of Cargo.nix, which was accepted but ignored, now adds buildRustCrate arguments to every crate, e.g. crateConfig = { extraRustcOpts = [ "-C" "debuginfo=1" ]; codegenUnits = 16; }. Flags and inputs such as extraRustcOpts and buildInputs are appended to the crate’s, other values replace them. It can also be a function of a crate’s buildRustCrate arguments, for settings per crate. Without the argument, a crate-config.nix next to Cargo.nix is used if it exists. See Settings for all crates.

  • #479: JSON mode (lib/build-from-json.nix) takes a crateConfig argument, too. In both modes, a crateConfig can drop the dependencies of a cargo-hakari workspace-hack crate, so that members no longer depend on crates they do not use: see Workspace-hack crates. Thank you, @lovesegfault, for the idea!

  • #287 (based on #405 by @giodamelio and #406 by @lelongg): New Cargo.nix arguments for the sources of local crates: localCratesFileset chooses the files of each local crate as a lib.fileset, given the crate’s directory, name, version and enabled features. The default is exported as defaultLocalCratesFileset by Cargo.nix and tools.nix. localCratesSrc chooses the directory that local crates come from, e.g. localCratesSrc = path: mySrc + path. See Filtering local sources. Thank you, @giodamelio and @lelongg!

  • #347: Faster generatedCargoNix/appliedCargoNix: they clean src with the default filter for local crates, and with the new manifestsOnly = true, the input of crate2nix generate only contains the manifests, lock files, crate2nix configuration and empty placeholders for target roots and other files that cargo metadata reads. Editing a source file then doesn’t generate Cargo.nix again, and the local crates are built from the original src via localCratesSrc. manifestsOnly is off by default and planned to become the default in a future release. They also take localCratesFileset, which appliedCargoNix passes on to Cargo.nix. See Faster IFD. The cleaning removes the same files from the input of crate2nix generate as from the sources of local crates, e.g. also directories named target at any depth, so crate2nix generate doesn’t find crates in such directories anymore.

  • #563 (based on #266 by Ben Wolsieffer): Crate overrides set with buildRustCrateForPkgs = pkgs: pkgs.buildRustCrate.override { defaultCrateOverrides = ...; } are now applied when cross-compiling with nixpkgs 25.05 or older. When cross-compiling, these nixpkgs releases pass Cargo.nix a copy of pkgs.defaultCrateOverrides (it is “spliced” by callPackage). Cargo.nix took the copy for custom overrides and passed it to buildRustCrate, in place of the user’s overrides. Without custom overrides, crates for the build platform (build dependencies, proc-macros and their dependencies) now also get the crate overrides of pkgs.buildPackages instead of those of pkgs. The same applies to lib/build-from-json.nix. Thank you, @lopsided98, for finding this and the first fix!

  • #547: The debugging helper debug.internal.diffedDefaultPackageFeatures (from diffDefaultPackageFeatures) works again. It read attributes that did not exist and so always reported no feature differences. It now compares crate2nix’s resolved features with cargo’s resolvedDefaultFeatures, uses rootFeatures (or the new features argument) instead of always [ "default" ], and lists both sides in differentFeatures.

  • #430 by Domen Kožar: generatedCargoNix (and the other IFD helpers of tools.nix) can now handle git dependencies whose crate uses workspace inheritance (version.workspace = true, serde = { workspace = true, features = [...] }, lints.workspace = true, …), fixing #311. Git crates are vendored without their workspace root, so, like cargo vendor, the vendored crate now gets a standalone Cargo.toml with the inherited values filled in from the closest workspace root (or package.workspace). Features are added to those of the workspace, optional comes from the member, and relative paths are rebased onto the crate directory.

  • #403 by Sam Pointon: Dependencies under cfg(target_env = "...") are selected for the target’s actual target_env, as in Cargo (fixes #402). Before, Cargo.nix assumed target_env = "gnu" on every platform, e.g. also for musl, macOS ("") and WASI ("p1"), and --format json did not match "p1" or "". crate2nix takes target_env from nixpkgs’ rust.platform.env and, with older nixpkgs that lack it, derives it from the platform’s ABI.

  • #549: JSON mode (crate2nix generate --format json with lib/build-from-json.nix) builds git dependencies that live in a subdirectory of their repository (fixes #492). The JSON records the crate’s directory in the repository as path in its git source, and build-from-json.nix builds the crate there, inside the whole checkout, as Cargo does. If path is missing, e.g. in JSON generated from vendored sources, buildRustCrate searches the repository for the crate’s Cargo.toml, as with the generated Cargo.nix; regenerate older JSON to get path. Git dependencies are now also fetched with their submodules, as Cargo does.

  • #554: Crates are compiled with more of the environment variables that cargo sets:

    • CARGO, the path of the cargo binary, which build scripts and proc-macros such as sqlx’s run (fixes #409). It is the cargo that buildRustCrate uses, e.g. one from a Rust toolchain overlay.
    • CARGO_CRATE_NAME (partly addresses #158). It is the library’s name for all targets of a package, see Known restrictions.
    • CARGO_PKG_DESCRIPTION, with --crate-metadata (fixes #356), which now also forwards the description from Cargo.toml.

    --format json builds now also get CARGO_PKG_LICENSE, CARGO_PKG_REPOSITORY and CARGO_PKG_HOMEPAGE with --crate-metadata. Quotes, backticks and $ in these values no longer break the build. All crates’ derivations change, so expect a one-time rebuild.

  • #552: Dependencies under [target.'cfg(target_has_atomic = "64")'.dependencies] (and the other widths) are enabled on platforms with atomics of that width, as in Cargo (fixes #469). Before, the condition was always false, which broke e.g. arkworks 0.5. makeDefaultTarget now sets has_atomic to the widths rustc reports for the platform, e.g. [ "8" "16" "32" "64" "ptr" ] for x86_64-linux. nixpkgs does not know these, so they are derived from the CPU (or, like rustc, from max-atomic-width, target-pointer-width and atomic-cas of a custom target spec); pass extraTargetFlags.has_atomic if they differ from rustc --print cfg --target <target> for your target. The JSON output (lib/build-from-json.nix) evaluates target_has_atomic the same way, but has no override.

  • #555: Dependencies under cfg(target_arch = "x86") are now enabled for i686 (and i386, i486, i586) targets: crate2nix used nixpkgs’ CPU name "i686" where rustc says "x86". aarch64_be, mipsel and powerpcle are mapped to rustc’s aarch64, mips and powerpc as well. JSON mode (lib/build-from-json.nix) used the raw CPU name, so it also missed target_arch dependencies on ARM, mips64el and powerpc64le; it now uses the same mapping. Fixes #183.

  • #546 (based on #486 by Varun Shah): Crates that declare no default feature no longer get --cfg feature="default" when they are depended on with default features (or built as a root with rootFeatures = [ "default" ]), as in Cargo (fixes #485). Before, such a crate was built twice if it was also reached with default-features = false, and so was everything that depended on it. A crate that declares an empty default = [] keeps default when it is requested, and the generated Cargo.nix now lists it as default = [ ];. The derivations of crates without a default feature change, so expect a one-time rebuild.

  • #539: Optional dependencies that a feature names as dep:<name> no longer get an implicit feature <name>, as in Cargo. crate2nix added it whenever the dependency was enabled, and turned on the crate’s own feature of that name, if it had one: e.g. reqwest 0.13’s rustls (and with it aws-lc-rs) when only rustls-no-provider was asked for. Builds may lose such spurious features, e.g. --cfg feature="memchr" on aho-corasick.

  • #544: The same fix for --format json: an explicit feature that shares an optional dependency’s name no longer adds that dependency.

  • #541: Forward the manifest’s readme field, so buildRustCrate sets CARGO_PKG_README and build scripts that read it (e.g. rusqlite_migration’s) work (fixes #440). Most crates’ derivations change, so expect a one-time rebuild.

  • #515: The dev shell of the flake-binary template now sets LIBRARY_PATH, so it can link Rust binaries on macOS. It no longer fails with ld: library not found for -liconv.

  • #516: On macOS, cdylib and dylib crates get their store path as install name, so binaries linked against them find them at runtime (#116).

  • #530: runTests now fails with an explanation when a binary target and an integration test have the same name, e.g. src/main.rs of crate foo and tests/foo.rs. buildRustCrate writes the test executables of both to the same file, so with current nixpkgs the integration test silently did not run. Rename the integration test to fix it.

  • #494: JSON mode (lib/build-from-json.nix): the buildTests of a workspace member takes its dependencies from the crate graph shared with the normal builds, instead of evaluating a graph of its own for every member, which makes evaluating the test builds of many members faster. The derivations are unchanged, except that a dev-dependency that depends back on the tested member now gets the member’s normal build, as with cargo, and no longer its test build, which recursed infinitely. Thank you, @emmakloe!

  • #522: crate2nix renders Cargo.nix with tera 2 instead of tera 1. It generates the same Cargo.nix as before. Errors while rendering a template are now reported with file, line and column instead of a debug dump.
  • #366: Support for private registries. Thank you, @P-E-Meunier!
  • #390: Allow selected toolchain in generatedCargoNix. Thank you, @bengsparks!
  • #416: Add extraTargetFlags parameter for custom cfg conditions.
  • #357: Use mkDerivation with src instead of runCommand for test derivation.
  • #372: Fix build-dependencies resolution when cross-compiling. Thank you, @pnmadelaine!
  • #375: Fix cargo tests and clippy warnings. Thank you, @pnmadelaine!
  • #388: Respect fetchurl passed via top-level Cargo.nix. Thank you, @weihanglo!
  • #391: Fix incorrect resolution of aliased git dependencies when multiple versions of the same package are referenced. Thank you, @hallettj!
  • #407: Inherit the main crate’s meta when building with tests. Thank you, @jrobsonchase!
  • Fix parsing checksums from v1 manifests with latest cargo. Thank you, @hallettj!
  • #394: Fix handling of git dependencies with wildcard workspace members and subcrates in nested directories. Thank you, @Pacman99!
  • #359: Document using rust-overlay.
  • #417: Document custom toolchains. Thank you, @qknight!
  • #414: Update flake inputs. Thank you, @jrobsonchase!

Maintenance release with cross-compilation fixes, documentation improvements and removal of old workarounds.

  • #352:

    • rust.lib -> stdenv.hostPlatform.rust
    • Always filter src using filterSource
    • Get rid of dontStrip for Darwin as it’s no longer needed
    • Remove no longer needed unsafeDiscardStringContext workaround
  • #351: Set --extra-experimental-features flakes in regenerate_cargo_nix.sh.

  • #350: Document targetFeatures better.

340 make crate2nix compatible with the lock file changes from rust 1.77.x.

Move sources into /build/sources to maximize compatibility.

The usual internal version updates but there is more!

The old README.md had become very long and hard to navigate. Check out the new and shiny page at https://nix-community.github.io/crate2nix/!

Do you like to use import from derivation so that you do not have to regenerate Cargo.nix on every dependency change?

The related convenience functions are now also available via the flake attribute “tools”:

# ...
perSystem = { system, pkgs, lib, inputs', ... }:
let
cargoNix = inputs.crate2nix.tools.${system}.appliedCargoNix {
name = "rustnix";
src = ./.;
};
in
rec {
packages.default = cargoNix.rootCrate.build;
};
# ...

Check out the documentation.

Convert the pre-flake infrastructure step-by step to nix flakes, while attempting to preserve compatibility for non-flake users.

This is more of an internal change and should not affect the usage of crate2nix yet but a more flake friendly interface is planned.

Tests and some utilities are still working flake-less but use the flake inputs by default.

  • Cross compilation fixes. Thank you, @flokli!
  • Propagate crate links attribute. Thank you, @lblasc!
  • Determine target vendor via nixpkgs function. Thank you, @jordanisaacs!
  • Initial flake.nix for crate2nix. Thank you, @vleni!

Conditional features are now supported. The feature selection foo?/bar will only enable the bar feature of the foo dependency if that dependency would be enabled anyway (non-optional, or enabled by a different feature selection).

This was previously deprecated.

Section titled “Restore callPackage not raw import as the recommended method”

With buildRustCrate removed, this is now possible without running into issues.

Previously only renamed dependencies and build-dependencies worked. Now renamed dev-dependencies work also.

Thank you @Fuuzetsu!

Better cross and platform-specific compilation support

Section titled “Better cross and platform-specific compilation support”
  • Rust-style rather than Nixpkgs-style configs are used for [target."some-config"] conditional Cargo sections.

  • The nixpkgs Rust “lib” is used to implement the above and deduplicate other cfg reosolution.

  • Support custom cfg(target_family = "whatever") dependencies.

Fix IFD support library (tools.nix) for Nixpkkgs 22.11

Section titled “Fix IFD support library (tools.nix) for Nixpkkgs 22.11”
  • See the comment inside there for details.

Help needed! I don’t have the resources to meaningfully advance this project. Thank you for all the valuable contributions but I’d appreciate a co-maintainer who is motivated to put in some time into reviewing PRs, updating docs, fixing bugs, ..

Warning: This release does not work on Mac OS with a recent nixpkgs. Help wanted. Warning: the buildRustCrate argument will be removed soon.

The recommended convention to use Cargo.nix files generated by crate2nix has changed. Instead of callPackage you know should use import:

let cargo_nix = import ./Cargo.nix { inherit pkgs; };
in cargo_nix.workspaceMembers."${your_crate_name}".build

The reason is that the deprecated buildRustPackage argument gets automatically supplied from pkgs.buildRustPackage by pkgs.callPackage. This causes problems when cross compiling.

This was debugged by @symphorien, thank you!

New: Enable optional create feature if sub feature was enabled

Section titled “New: Enable optional create feature if sub feature was enabled”

Enable a feature with the same name of an optional dependency, if the optional dependency is indirectly enabled by enabling one of its features.

This mimicks the behavior of Cargo which is explained in this note.

Thank you @pandaman64!

This fixes the following isues:

  • #146 Features fail to contain optional dependencies that got enabled indirectly
  • #182 Can’t build crate with dependency num 0.4.0

Want to customize your test execution? Use the testPreRun and testPostRun crate attributes (next to runTests, see README.md). crate2nix executes the bash snippets in testPreRun and testPostRun directly before and after the actual test command, and in the same shell. Some example use-cases include:

  • Setting some environment variable that is needed for the test.

  • Setting (and then unsetting) the bash set +e option to not fail the derivation build even if a test fails. This is quite useful if your tests are not flaky and you want to cache failures.

Thank you @Fuuzetsu!

Help needed! I don’t have the resources to meaningfully advance this project. Thank you for all the valuable contributions but I’d appreciate a co-maintainer who is motivated to put in some time into reviewing PRs, updating docs, fixing bugs, ..

  • Remove long deprecated root_crate and workspace_members aliases in the generated Cargo.nix files.
  • Issue #83 Supporting depending on different versions of the same crate!
  • Some strides towards cross-compilation support. Docs missing and would be appreciated! Thanks, @Ericson2314, @lopsided98!
  • Experimental out-of-tree support — with no time to work further on it :(
  • Test execution is now closer to what cargo test does. Thank you, @symphorien!
  • Better direnv support in the source. Thanks, @Mic92!
  • Better support for upcoming nix 3.0. Thanks, @balsoft!
  • tests: avoid building two versions of the same crate. Thanks, @andir!
  • Remove usages of deprecated stdenv.lib. Thanks, @cole-h!

Organizational: @andir is now an additional official maintainer of crate2nix. Welcome!

Breaking change:

  • If you are building crates with git dependencies, you will now need to update to a recent version of nixpkgs-unstable. On the upside, crates in sub directories of git repositories will now work!

New features:

  • Issue #53: Search for matching Cargo.toml for git dependencies.
  • Running tests is now documented in the README.
  • Add testInputs to test execution. This allows users to add buildInputs to the test execution. This might be required as during test execution external programs will be called on generated output or in cases where the rust application is just a wrapper around some other tool (in some way or another).
  • Issue #47 Shorter package IDs: Use the crate name or the crate name together with the version as package ID if that is unique. That shortens the generated files, makes them more readable and also strips local paths from the package IDs in most cases. The last point means that you do not have an unncessary diff in you generated Cargo.nix because someone else was regenerating it.

Under the hood:

  • Trimmed down the dependency tree of crate2nix itself by disabling some features.
  • At least some smoke tests for debug functionality.

If you are interested in hacking on crate2nix and buildRustCrate: There are now some convenience flags for the ./run_tests.sh script for offline mode and running the tests against your own modified version of nixpkgs. Check out the README section “Hacking on buildRustCrate in nixpkgs”.

New features and improvements:

  • Use hashes from Cargo.lock instead of prefetching when available. This should work for any crates.io dependency. :)
  • Follow up to Issue #22 (and others) - Handling of “renamed crates”. Thanks a lot,@andir!
  • Support for multiple binaries from the same crate. Thank you, @kristoff3r!
  • Issue #34 - Support for git prefetching so that repositories with sub modules now work. Thank you, @cpcloud!
  • Issue #65 - Reexpose feature selection parameters from cargo metadata. This allows to include dependencies in the generated build file which are not used for the default features. Or to exclude dependencies for features that you do not care about.
  • Issue #67 - Support for additional lib types - in particular, cdylib. Thank you, @andir! Write a rust library that is used from C code :)
  • Issue #18 - Optional crate unavailable Allows building packages that have multiple versions of the same dependency (with different targets). In particular the flate2 package now builds. Thank you, @cchalmers!
  • Issue #37 - Conditional target expressions for dependencies can now also depend on features. Thank you, @cpcloud!
  • Issue #42 - Some efficiency improvements to prevent stack overflows for projects with huge dependency trees. Thank you, @jamii! * Issue #90 There is a follow up to this: @nagisa was seeing super-linear instantiation counts and provided a flamegraph. @andir proposed a likely fix in nixpkgs. Thank you!
  • Add fuchsia as an unsupported target (ef945396fcb700322b5b5f497a5d243950ed2513 ). Thank you, @jamii!
  • Issue #94: The defaultCrateOverrides argument to the build file has finally the desired effect again.
  • #75: Cleanly separate internal API by internal. attribute path element. Formally, this is no breaking change if it only effects private API but still. I will mitigate by allowing the old paths for a release and issue a warning.

Thank you to everyone who contributed with awesomely detailed issues, PRs or otherwise. You are amazing! Please let me know if I forgot something or forgot to give someone appropriate credit.

For contributors:

  • ./run_tests.sh now makes it easier to prepare your pull requests for review.
  • Build artifacts for linux are now properly pushed to eigenvalue.cachix.org. Adding that cache with cachix will speed up your installations and builds. And it speeds up our CI builds via github actions. Shout out to @domenkozar and other cachix contributors.
  • @alyssais contributed some fixes to the developer scripts, thank you!

Experimental and still undocumented:

  • cargo test-like test running support! Thank you very much for your great work, @andir!

Heads up! Feel free to discuss these planned changes in future releases with me:

  • #77: New/better override behavior that also allows overriding buildRustCrate more easily.
  • #82: Use a new file name for crate-hashes.json every time to prevent merge issues.
  • #102: Convenient support for out-of-tree sources (e.g. for nixpkgs)

Backported escaping fix for target expressions.

  • Issue #22 - Support renamed crates. Fixed in buildRustCrate in nixpkgs and in crate2nix by PR #24 @danieldk, thanks a lot! This is especially awesome because the popular rand crate recently made use of the “renamed crates” feature and therefore could not be build by buildRustCrate/crate2nix anymore.

  • Issue #15 - Support “overrideCrates” argument for modifying the derivation for a crate by name. Common use case, adding additional buildInputs. NixOS comes with [defaultCrateOverrides](https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/rust/default-crate-overrides.nix) for some common packages already. This is what I use in default.nix of crate2nix to add a conditional dependency under Mac OS:

    cargo_nix.rootCrate.build.override {
    crateOverrides = defaultCrateOverrides // {
    cssparser-macros = attrs: {
    buildInputs = stdenv.lib.optionals stdenv.isDarwin [darwin.apple_sdk.frameworks.Security]; };
    };
    };

    Many thanks to @Profpatsch for pointing to the problem and analyzing it.

  • Issue #43 - Support conditional dependencies using “test” configuration:

    [target.'cfg(test)'.dependencies]
    tracing = { version = "0.1.5", features = ["log"] }

    When building with crate2nix, dependencies will not lead to an error anymore and will simply be ignored (since we do not have test support yet). Thanks to @andir for the nice minimal example!

Infrastructure:

  • I moved the integration tests to tests.nix, they were in rust code before.
  • I also now build every push with github actions, and cachix/cachix-action. A suggestion from @vbrandl in #44. Unfortunately, the rust crates are not cached yet, I think, because they are not in the closure of the result. The cachix caches is called “eigenvalue” for now (I might change that in the future).

Don’t use ´Cargo.toml´ but ´Cargo.nix´ as default output! Thank you, @tilpner!

  • The default output file changed to “./Cargo.toml”. Specify “-o ./default.nix” explicitly to retain the old default.
  • Issue #10 - Changing default for -n/--nixpkgs-path to "<nixpkgs>" so that it works by default on NixOS AND Mac OS.
  • Issue #11 - Adding optional dependencies if any of their features is enabled.
  • Issue #14 - Only overwrite output if explicitly specified as output file.

Please change references to root_crate to rootCrate.build and references to workspace_members.${crateName} to workspaceMembers.${crateName}.build. The camel case attribute names are in line with the nixos style guide. The .build suffix allows future versions of crate2nix to add other convenient features such as source tarball packages, docker image derivations, … The old aliases still work but are deprecated.

The enabled features for a crate now are resolved at build time! That means you can easily override them:

  1. There is a “rootFeatures” argument to the generated build file which you can override when calling it from the command line:

    Terminal window
    nix build -f ....nix --arg rootFeatures '["default" "other"]' rootCrate.build
  2. Or when importing the build file with “callPackage”:

    let cargo_nix = callPackage ./Cargo.nix { rootFeatures = ["default" "other"]; };
    crate2nix = cargo_nix.rootCrate.build;
    in ...;
  3. Or by overriding them on the rootCrate or workspaceMembers:

    let cargo_nix = callPackage ./Cargo.nix {};
    crate2nix = cargo_nix.rootCrate.build.override { features = ["default" "other"]; };
    in ...;

For this release, I needed substantial amount of nix code so I created some nix unit tests. They are invoked by cargo test like all other tests and live in the ./templates/nix/crate2nix/tests directory.

Feedback: What is needed for a 1.0 release?

Section titled “Feedback: What is needed for a 1.0 release?”

I would really appreciate your thoughts. Please add comments to issue #8.

  • Issue #5: Support libPath for proc-macro crates.

Thank you @nuxeh for reporting this bug! Again :)

  • Issue #4: Support for libName != crateName.

Thank you @nuxeh for reporting this bug!

Example:

[dependencies]
"crate2nix" = { git = "https://github.com/kolloch/crate2nix" }
  • Added comments to the generated nix build file to indicate which attributes are public and unlikely to change.
  • While the command line help said that the “crate hashes” would be stored in a file called “crate-hashes.json”, it actually used the file “crate_hashes.json” by default. This release uses the documented name which means that after the update nix-prefetch-url might run again.
  • Issue #3: Do not depend on local channel configuration for shell anymore. Instead, we use a recent nixos-unstable because we still need a fix that’s not in stable.

If crate2nix is applied to a workspace, the resulting nix-file will contain a top-level “workspace_members” attribute set that refers the corresponding top-level crate derivations by name.

“cfg(…)” expressions and target triplets such as “i686-pc-windows-gnu” are compiled into nix expressions. Support should be reasonable but incomplete because e.g. it does not work for processor features. Please let me know if this causes problems for you!

Initial public release.