tvl-depot/tvix/serde/examples/nixpkgs.rs
Vincent Ambo d0d1027a85 feat(tvix/serde): add an example using nixpkgs/lib
This displays how users can configure an impure evaluation for
tvix-serde, which makes it possible to use e.g. `nixpkgs/lib`.

We might want to add an example showing how the full Nix-glue
compatibility stuff can be added here, too.

Change-Id: I2224a3fc66e739969d4c723c3d9d8127a046b6fd
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10994
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: tazjin <tazjin@tvl.su>
2024-02-20 12:13:45 +00:00

34 lines
803 B
Rust

//! This program demonstrates deserialising some configuration
//! structure from Nix code that makes use of nixpkgs.lib
//!
//! This example does not add the full set of Nix features (i.e.
//! builds & derivations).
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Config {
host: String,
port: usize,
}
fn main() {
let code = r#"
let
lib = import <nixpkgs/lib>;
host = lib.strings.concatStringsSep "." ["foo" "example" "com"];
in {
inherit host;
port = 4242;
}
"#;
let result = tvix_serde::from_str_with_config::<Config, _>(code, |eval| {
eval.enable_impure(None);
});
match result {
Ok(cfg) => println!("Config says: {}:{}", cfg.host, cfg.port),
Err(e) => eprintln!("{:?} / {}", e, e),
}
}