Rust: Difference between revisions

imported>Figsoda
m fenix added support for stable toolchains
imported>Mic92
add ways of building rust packages.
Line 92: Line 92:
})
})
</syntaxHighlight>
</syntaxHighlight>
== Packaging Rust projects with nix ==
At the time of writing, there are now no less than 6 different solutions for building Rust code with Nix. In the following table they are compared:
{|
| Name
| Cargo.lock solution
| Derivations
| Build logic
| Supports cross
| Notes
|-
| [https://github.com/NixOS/nixpkgs/blob/4fc53b59aecbc25c0e173163d60155f8fca14bfd/doc/languages-frameworks/rust.section.md <code>buildRustPackage</code>]
| Checksum
| 1
| cargo
| Yes
| Built into nixpkgs
|-
| <code>carnix</code>
| Codegen
| Many
| <code>buildRustCrate</code>
| No?
| Unmaintained, AFAICT; [https://nest.pijul.com/pmeunier/carnix:master repository] is gone
|-
| [https://github.com/kolloch/crate2nix <code>crate2nix</code>]
| Codegen (with optional IFD)
| Many
| <code>buildRustCrate</code>
| No
| Spiritual successor to carnix; [https://github.com/kolloch/crate2nix/pull/152#issuecomment-715603056 nobody currently spending much time maintaining it]
|-
| [https://github.com/nmattia/naersk/ <code>naersk</code>]
| Import
| 2
| cargo
| [https://github.com/nmattia/naersk/issues/79 No?]
| [https://github.com/nmattia/naersk/blob/22b96210b2433228d42bce460f3befbdcfde7520/rust/rustc.nix#L22-L29 Seems to only support building on x86]
|-
| [https://github.com/cargo2nix/cargo2nix <code>cargo2nix</code>]
| Codegen
| Many
| Custom
| Yes
| Seems to only support prebuilt rustc from the <code>nixpkgs-mozilla</code> overlay, not rustc provided by Nixpkgs
|-
| [https://github.com/edolstra/import-cargo <code>import-cargo</code>]
| Import
| 1
| cargo
| Unclear
| More of a proof of concept than a full working solution
|}
Explanation for the columns
* '''Cargo.lock solution:''' How does this solution handle reproducibly determining what crates need to be downloaded from the Cargo.lock file? “Checksum” means it requires you to specify the checksum of all the downloaded dependencies. “Import” means it dynamically imports and parses Cargo.lock from a Nix expression, which means Cargo.lock needs to be present in the same repository as the nix expressions (or IFD must be used). “Codegen” means it generates a .nix file from the Cargo.lock, which is then committed to source control.
* '''Derivations:''' How many derivations does this solution use to compile rust code? “1” means the project and all its dependencies are compiled in one derivation. “2” means all dependencies are moved into a separate derivation, so the project can be updated independently, but any change to the set of dependencies rebuilds everything. “Many” means each dependency is built in its own derivation, so changes to dependencies only do the minimal amount of rebuilding necessary (and, ideally, different projects can share dependences, although I haven’t checked if this works in practice).
* '''Build logic:''' How does this solution orchestrate building of crates? “Cargo” means it relies on Cargo; <code>buildRustCrate</code> means it uses Nixpkgs’ <code>buildRustCrate</code>; “custom” means it uses its own custom logic (in Nix)
* '''Supports cross:''' Does the solution allow for cross-compilation of crates?


== Shell.nix example ==
== Shell.nix example ==