In this tutorial, you will learn how to initialize a Rust executable project. You can use cargo
for most of the workflow. Thanks to rust-overlay, you can pick a particular channel of the Rust toolchain, and crane builds an executable in a reproducible manner with Nix. Rust Analyzer works out of the box.
Initialize a new project
Make
cargo
executable available:Use
cargo new
to create a new project:Enter the directory:
Add flake.nix to the project
Initialize the template from the root directory of the project:
By default, the template will pick the default profile of the latest version from the stable channel. If you prefer a different one, open
flake.nix
and edit the following settings to your preference. Check out the documentation of the Rust Overlay for details:Add
.envrc
:Allow direnv:
Developing
Use cargo
to develop the project as you would normally do without Nix.
Depending on the project, you may have to add dependencies such as pkg-config
, openssl
, etc. Some of these things are supported in the template, so just check out the flake.nix
and comment out relevant fields.
Building the Nix package
The package is exposed as default
package out of the box, so you can run the application:
It is recommended to add the settings for your binary cache to flake.nix
: