Expand description
§ser_nix
Nix is a declarative, atomic, and reproducible package manager that is configured with the nix programming language
{
a = 1;
b = "Hello World";
submap.foo = "bar";
}ser_nix can be used to serialise arbitrary rust types into corresponding nix data types. As the name implies, ser_nix does not provide deserialisation capabilities, as the process for doing so is non-trivial, and requires evaluating nix code.
ser_nix tries to follow the idioms of other serde libraries, like serde_json.
use serde::Serialize;
use ser_nix::to_string;
#[derive(Serialize)]
struct Person {
name: String,
age: u8,
}
let cm = Person {
name: "John Doe".into(),
age: 65,
};
let serialized = to_string(&cm).unwrap();
let expected = "{\n name = \"John Doe\";\n age = 65;\n}".to_string();
assert_eq!(serialized, expected);§Option and None values
Following serde_json conventions, Option::None values are serialized
as null by default. To omit fields when they are None, use the
#[serde(skip_serializing_if = "Option::is_none")] attribute:
use serde::Serialize;
use ser_nix::to_string;
#[derive(Serialize)]
struct Config {
enabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
optional: Option<String>,
}
let config = Config {
enabled: None,
optional: None,
};
let serialized = to_string(&config).unwrap();
// Output: { enabled = null; }§Nix paths
In Nix, paths like ./foo.nix or /etc/nixos/configuration.nix are written
without quotes. There are two ways to serialize paths as unquoted Nix paths:
§Using NixPathBuf wrapper type
use serde::Serialize;
use ser_nix::{to_string, NixPathBuf};
#[derive(Serialize)]
struct NixConfig {
source: NixPathBuf,
}
let config = NixConfig {
source: NixPathBuf::new("./hardware-configuration.nix"),
};
let serialized = to_string(&config).unwrap();
// Output: { source = ./hardware-configuration.nix; }For borrowed paths, use NixPath<'a>:
use ser_nix::{to_string, NixPath};
use std::path::Path;
let path = Path::new("./config.nix");
let result = to_string(&NixPath::new(path)).unwrap();
assert_eq!(result, "./config.nix");§Using #[serde(serialize_with = "...")]
use serde::Serialize;
use ser_nix::to_string;
use std::path::PathBuf;
#[derive(Serialize)]
struct NixConfig {
#[serde(serialize_with = "ser_nix::as_nix_path")]
source: PathBuf,
description: String,
}
let config = NixConfig {
source: PathBuf::from("./hardware-configuration.nix"),
description: "Hardware config".to_string(),
};
let serialized = to_string(&config).unwrap();
// source is unquoted: ./hardware-configuration.nix
// description is quoted: "Hardware config"Structs§
- NixLiteral
- A raw Nix expression that serializes without quotes.
- NixPath
- A borrowed path that serializes as a Nix path literal.
- NixPath
Buf - An owned path that serializes as a Nix path literal.
Enums§
- Error
- Error type for ser_nix serialisation
Functions§
- as_
literal - Serialize a string as a raw Nix expression (without quotes).
- as_
literal_ seq - Serialize a
Vec<String>or&'a [&'a str]as a list of raw Nix expressions. - as_
nix_ path - Serialize a
PathorPathBufas a Nix path literal. - as_
optional_ literal - Serialize an
Option<String>as a raw Nix expression, or null if None. - as_
optional_ nix_ path - Serialize an
Option<PathBuf>orOption<&Path>as a Nix path literal, or null if None. - to_
string - Serialise the given data structure as a String of Nix data