弥合 Nix 差距:从环境到 Rust 软件包应用程序
Closing the Nix gap: From environments to packaged applications for rust

原始链接: https://devenv.sh/blog/2025/08/22/closing-the-nix-gap-from-environments-to-packaged-applications-for-rust/

## 使用 Devenv 简化 Rust 打包 对于使用 Nix 的 Rust 开发者来说,一个常见痛点是选择合适的工具——`crate2nix`、`cargo2nix` 或 `naersk`——来打包应用程序。Devenv 最初通过 `languages.rust.enable` 简化了*开发*,为开发者提供了必要的 Rust 工具,而无需 Nix 方面的专业知识。然而,部署仍然需要开发者在这些打包选项中进行选择。 现在,devenv 通过 `languages.rust.import` 解决了这个问题,自动使用 **crate2nix** 打包 Rust 项目——这是一个经过仔细评估后为开发者做出的决定。这类似于之前从 `fenix` 到 `rust-overlay` 的成功过渡,确保了流畅的体验。 工作流程很简单:启用语言进行开发,然后使用 `import` 进行打包。这种一致的模式也扩展到其他语言,例如 Python(使用 `uv2nix`),为 Nix 中的语言工具提供了一种统一且经过测试的方法。Devenv 旨在抽象 Nix 打包的复杂性,让开发者专注于代码。

相关文章

原文

This tweet shows a common problem in Nix: "Should I use crate2nix, cargo2nix, or naersk for packaging my Rust application?"

devenv solved this for development environments differently: instead of making developers package everything with Nix, we provide tools through a simple languages.rust.enable. You get cargo, rustc, and rust-analyzer in your shell without understanding Nix packaging.

But when you're ready to deploy, you face the same problem: which lang2nix tool should you use? Developers don't want to compare crate2nix vs cargo2nix vs naersk vs crane—they want a tested solution that works.

devenv now provides languages.rust.import, which packages Rust applications using crate2nix. We evaluated the available tools and chose crate2nix, so you don't have to.

We've done this before. In PR #1500, we replaced fenix with rust-overlay for Rust toolchains because rust-overlay was better maintained. Users didn't need to change anything—devenv handled the transition while keeping the same languages.rust.enable = true interface.

One Interface for All Languages

The typical workflow:

  1. Development: Enable the language (languages.rust.enable = true) to get tools like cargo, rustc, and rust-analyzer.
  2. Packaging: When ready to deploy, use languages.rust.import to package with Nix.

The same pattern works for all languages:

Starting with Rust

languages.rust.import automatically generates Nix expressions from Cargo.toml and Cargo.lock.

Add the crate2nix input:

Import your Rust application:

Build your application:

Other Languages

This API extends to other languages, each using the best packaging tool:

We've also started using uv2nix to provide a similar interface for Python in PR #2115.

That's it

For feedback, join our Discord community.

Domen

联系我们 contact @ memedata.com