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>
This commit is contained in:
parent
e839116e5c
commit
d0d1027a85
1 changed files with 34 additions and 0 deletions
34
tvix/serde/examples/nixpkgs.rs
Normal file
34
tvix/serde/examples/nixpkgs.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
//! 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),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue