about summary refs log tree commit diff
path: root/tvix/serde/examples/nixpkgs.rs
blob: ad8c4160b5d0b5e944fc984b48a6e9ddd957a95a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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),
    }
}