rusix 0.1.1

Typed Rust authoring, compilation and isolated evaluation for Nix
Documentation

Rusix

Rusix is a Rust library for defining packages and NixOS configuration in ordinary Rust, as an alternative to the Nix language. It compiles these definitions into Nix expressions that can use existing Nix packages and modules. Existing Nix code can import and use the generated definitions.

  • Model configuration with your own Rust types and compose definitions through ordinary Rust functions.
  • Generated expressions preserve Nix's lazy evaluation. NixOS handles option validation and module merging.
  • Source mappings help trace Nix evaluation errors back to the Rust code that produced them.

Add Rusix to Cargo.toml:

[dependencies]
rusix = "0.1"
use rusix::{IntoConfig, compile};

#[derive(IntoConfig)]
struct Service {
    /// Whether the service should run.
    enable: bool,
    /// Port on which the service listens.
    port: u16,
}

fn main() {
    let generated = compile(Service { enable: true, port: 8080 })
        .expect("valid configuration");
    println!("{}", generated.source);
}

In this example, the compiler turns Service into a Nix attribute set with enable and port fields.

Use rusix to write package and configuration definitions in your own Rust crates. Definitions can be shared through libraries and tested with Cargo. Tests can evaluate the generated Nix and check its results.

Rusix can identify both Rust definitions in a NixOS merge conflict. An excerpt from the merge-two fixture:

error[nixos-merge]: conflicting definitions for a NixOS option
  --> crates/rusix/src/bin/cli/merge_fixtures.rs:16:23
   = conflicting definition
  --> crates/rusix/src/bin/cli/merge_fixtures.rs:10:23
   = conflicting definition
   = option: services.openssh.authorizedKeysCommandUser

Error reporting shows complete diagnostics and their original Nix errors.

Rusix is experimental, and some Nix errors cannot be traced to a Rust source location.