From d0d1027a853882f8bb44ac26c865f04bfa42ed70 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 20 Feb 2024 18:32:03 +0700 Subject: 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 Autosubmit: tazjin --- tvix/serde/examples/nixpkgs.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tvix/serde/examples/nixpkgs.rs (limited to 'tvix') diff --git a/tvix/serde/examples/nixpkgs.rs b/tvix/serde/examples/nixpkgs.rs new file mode 100644 index 000000000000..ad8c4160b5d0 --- /dev/null +++ b/tvix/serde/examples/nixpkgs.rs @@ -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 ; + host = lib.strings.concatStringsSep "." ["foo" "example" "com"]; + in { + inherit host; + port = 4242; + } + "#; + + let result = tvix_serde::from_str_with_config::(code, |eval| { + eval.enable_impure(None); + }); + + match result { + Ok(cfg) => println!("Config says: {}:{}", cfg.host, cfg.port), + Err(e) => eprintln!("{:?} / {}", e, e), + } +} -- cgit 1.4.1