cpu / rust-flake

A beyond-the-basics Nix Flake for a Rust project using native code, supporting multiple Rust versions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Beyond-the-basics Rust Flake

This repo is a simple example project to accompany an article about writing a Nix Flake for a Rust project with some requirements beyond the basics. In particular it:

  • Defines multiple Flake outputs, for different Rust feature selections.
  • Defines multiple development environments for Rust stable, nightly, and the project's MSRV.
  • Supports Rust crate dependencies using cbindgen with native libraries.
  • Loads the crate TOML metadata in Nix to avoid duplicating project name, version and MSRV in more places.

Example Crate

This repository contains a simple Rust binary project that demonstrates text-to-speech on Linux. It depends on the tts-rs crate, which in turn uses the speech-dispatcher and speech-dispatcher-sys crates. The -sys crate uses pkg-config and cbindgen to generate FFI headers for speechd on Linux. It has an optional foobar feature that when enabled, changes the spoken message.

The Nix Flake defines a reproducible environment for:

  • Installing the required native code dependencies.
  • Ensuring cbindgen can locate native code dependencies.
  • Installing required Rust toolchains.
  • Setting up development environments for each Rust toolchain.
  • Outputting two package derivations (with or without the foobar feature enabled).

Usage

Run without cloning

Existing Nix/NixOS users can run the default package directly without needing to clone anything or do any additional setup:

nix run github:cpu/rust-flake

Default Package

After cloning the repo, you can run the default Flake output package directly:

nix run

Or, to run the output package that doesn't enable the "foobar" feature:

nix run '.#example-base'

Dev Environments

You can quickly enter a development environment for one of the three Rust versions:

# Rust nightly (default):
nix develop
# Rust stable:
nix develop '.#stable'
# MSRV:
nix develop '.#msrv'

Cargo

In each development environment you'll have the usual cargo tooling and any extra devDeps specified:

rustc --version && gdb --version && speech-dispatcher --version
cargo fmt && cargo clippy && cargo test
cargo run
cargo run --all-features --release

Quickly running a command

Rather than enter a development shell you can also run a command in the development environment directly:

# Nightly:
nix develop '.#nightly' --command cargo test
# Stable:
nix develop '.#stable'  --command cargo test
# MSRV:
nix develop '.#msrv'    --command cargo test

CI

You may also be interested in the CI configuration. It uses the Nix Flake to ensure code is formatted, linted with clippy, and tested with nightly, stable and MSRV Rust toolchains.

About

A beyond-the-basics Nix Flake for a Rust project using native code, supporting multiple Rust versions.

License:MIT License


Languages

Language:Nix 88.7%Language:Rust 11.3%