Skip to main content

Crate snix_serde

Crate snix_serde 

Source
Expand description

snix-serde bridges Rust types and snix_eval::Value via the standard serde traits, making it straightforward to use Nix as a configuration language.

§Entry points

FunctionDirection
from_strEvaluate a Nix expression string → T
from_valuesnix_eval::ValueT
to_valueTsnix_eval::Value

§Type mapping

Rust / serdeNix
booltrue / false
integers (i8i64, u8u32)integer
u64integer (errors if > i64::MAX)
f32, f64float
char, str, Stringstring
Option::None / unit / unit structnull
Option::Some(v) / newtype structinner value
sequence / tuplelist [ … ]
map / structattribute set { … }
unit enum variantstring of the variant name
newtype enum variant{ VariantName = value; }
tuple enum variant{ VariantName = [ … ]; }
struct enum variant{ VariantName = { … }; }

Bytes are not supported by the Nix value model and will produce an error.

§Examples

Deserialise a Nix expression:

#[derive(Deserialize)]
struct Config { host: String, port: u16 }

let cfg: Config = snix_serde::from_str(r#"{ host = "localhost"; port = 8080; }"#).unwrap();
assert_eq!(cfg.host, "localhost");
assert_eq!(cfg.port, 8080);

Serialise to a snix_eval::Value and back:

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Point { x: i64, y: i64 }

let p = Point { x: 1, y: 2 };
let value = snix_serde::to_value(&p).unwrap();
let q: Point = snix_serde::from_value(value).unwrap();
assert_eq!(p, q);

Modules§

de 🔒
Deserialisation from Nix to Rust values.
error 🔒
When (de-)serialising Nix goes wrong …
ser 🔒
Serialisation from Rust values to Nix.

Enums§

Error

Functions§

from_str
Evaluate the Nix code in src and attempt to deserialise the value it returns to T.
from_str_with_config
Evaluate the Nix code in src, with extra configuration for the snix_eval::Evaluation provided by the given closure.
from_value
Deserialise a snix_eval::Value directly into a T without going through Nix evaluation. This is the inverse of crate::to_value.
to_string
Serialise a Rust value into a Nix code string.
to_value
Serialise a Rust value into a snix_eval::Value.