Skip to main content

Crate serde_nixos

Crate serde_nixos 

Source
Expand description

§serde-nixos

Generate NixOS type definitions from your Rust structures.

This crate provides a derive macro that automatically generates NixOS module definitions from your Rust configuration structures, ensuring type safety across both Rust and Nix.

§Quick Start

use serde::{Serialize, Deserialize};
use serde_nixos::NixosType;

#[derive(Serialize, Deserialize, NixosType)]
struct ServerConfig {
    #[nixos(description = "The server port to listen on")]
    port: u16,

    #[nixos(default = "\"localhost\"", description = "The hostname to bind to")]
    host: String,

    #[nixos(description = "Maximum number of concurrent connections")]
    max_connections: Option<u32>,

    #[nixos(description = "Database configuration")]
    database: DatabaseConfig,
}

#[derive(Serialize, Deserialize, NixosType)]
struct DatabaseConfig {
    #[nixos(description = "Database connection string")]
    url: String,

    #[nixos(default = "5", description = "Connection pool size")]
    pool_size: u32,
}

Then generate the NixOS module:

let nixos_module = ServerConfig::nixos_type_definition();
println!("{}", nixos_module);

This will output a NixOS module definition that can be used in your NixOS configuration.

§Multi-Type Module Generation

For generating .nix files that compose multiple types with proper let bindings and cross-references, use NixosModuleGenerator:

use serde::{Serialize, Deserialize};
use serde_nixos::{NixosType, type_registration};
use serde_nixos::generator::NixosModuleGenerator;

#[derive(Serialize, Deserialize, NixosType)]
struct Inner { value: String }

#[derive(Serialize, Deserialize, NixosType)]
struct Outer { child: Inner }

let nix = NixosModuleGenerator::new()
    .register(type_registration!(Inner))
    .register(type_registration!(Outer))
    .export_all_types()
    .generate();

Re-exports§

pub use generator::NixosModuleGenerator;
pub use generator::TypeRegistration;

Modules§

generator
Generator utilities for building NixOS modules Generator utilities for creating NixOS modules from Rust types
utils
Utility functions for working with NixOS types

Macros§

nixos_module
Generate the full NixOS type definition (with let bindings for nested types) from a Rust type at compile time.
type_registration
Create a TypeRegistration from a type that derives NixosType.

Traits§

Deserialize
Re-export commonly used serde traits for convenience A data structure that can be deserialized from any data format supported by Serde.
NixosTypeGenerator
Helper trait for types that can generate NixOS definitions
Serialize
Re-export commonly used serde traits for convenience A data structure that can be serialized into any data format supported by Serde.

Derive Macros§

Deserialize
Re-export commonly used serde traits for convenience
NixosType
Derive macro for generating NixOS type definitions from Rust structures.
Serialize
Re-export commonly used serde traits for convenience