ronix
Serialize Rust structs to Nix expressions — the bridge between serde and NixOS.
ronix also ships a Nix library (toRON / fromRON) so the conversion works in both directions.
Quick start
[]
= "0.1"
= { = "1", = ["derive"] }
use Serialize;
let curve = FanCurve ;
let nix = to_nix.unwrap;
// {
// name = "cpu";
// temp = 60;
// pwm = 150;
// }
API
| Function | Description |
|---|---|
to_nix(value) |
Serialize any Serialize type to a Nix expression string |
to_nix_module(value, attr_path) |
Same, wrapped in a NixOS module (_: { attr.path = …; }) |
ron_to_nix(ron) |
Parse a RON string and convert to Nix |
ron_to_nix_module(ron, attr_path) |
Parse RON and wrap in a NixOS module |
escape_nix_string(s) |
Escape a string for embedding in Nix |
RON → Nix
let nix = ron_to_nix.unwrap;
assert!;
NixOS modules
let nix = to_nix_module.unwrap;
// _: {
// services.myapp.settings = {
// ...
// };
// }
CLI helpers
Enable the cli feature to get a reusable clap argument struct you can embed in your own binary:
[]
= { = "0.1", = ["cli"] }
= { = "4", = ["derive"] }
use Parser;
Nix library
ronix provides a pure-Nix library for converting between Nix values and RON. Add the flake as an input and use the library directly:
{
inputs.ronix.url = "codeberg:caniko/ronix";
outputs = { ronix, ... }: {
# Nix → RON
ronString = ronix.lib.toRON 0 {
name = "hello";
count = 42;
};
# RON → Nix
nixValue = ronix.lib.fromRON ''(name: "hello", count: 42)'';
# Read a .ron file directly
config = ronix.lib.importRON ./config.ron;
};
}
Type constructors
The Nix library uses mkRON to represent RON types that have no direct Nix equivalent:
{ mkRON, ... }: {
char = mkRON.char "A";
opt = mkRON.optional "value"; # Some("value")
none = mkRON.optional null; # None
tuple = mkRON.tuple [ 1 2 3 ];
enum_ = mkRON.enum "Variant" [ ]; # unit variant
map = mkRON.map [ { key = "a"; value = 1; } ];
}
NixOS service helper
{ ronix, ... }: {
config = ronix.nixosHelpers.mkRonService {
name = "smartcool";
package = pkgs.smartcool;
settings = { temp_target = 65; fan_mode = "auto"; };
execStart = "${pkgs.smartcool}/bin/sc daemon -c /etc/smartcool/config.ron";
};
}
This generates /etc/smartcool/config.ron and a matching systemd service.
Supported types
| Rust / RON | Nix output |
|---|---|
bool |
true / false |
| integers | 42, -7 |
| floats | 2.5, 1.0 |
String / &str |
"hello" (with proper escaping) |
Vec<T> / sequences |
[ ... ] |
| structs / maps | { key = value; ... } |
Option<T> |
Some unwraps, None omits the field |
char |
"c" |
Nix interpolation syntax (${...}) is automatically escaped.
Building with Nix