drvfmt/
drvfmt.rs

1use std::{collections::BTreeMap, io::Read};
2
3use nix_compat::derivation::Derivation;
4use serde_json::json;
5
6use mimalloc::MiMalloc;
7
8#[global_allocator]
9static GLOBAL: MiMalloc = MiMalloc;
10
11/// construct a serde_json::Value from a Derivation.
12/// Some environment values can be non-valid UTF-8 strings.
13/// `serde_json` prints them out really unreadable.
14/// This is a tool to print A-Terms in a more readable fashion, so we brutally
15/// use the [std::string::ToString] implementation of [bstr::BString] to get
16/// a UTF-8 string (replacing invalid characters with the Unicode replacement
17/// codepoint).
18fn build_serde_json_value(drv: Derivation) -> serde_json::Value {
19    json!({
20        "args": drv.arguments,
21        "builder": drv.builder,
22        "env":   drv.environment.into_iter().map(|(k,v)| (k, v.to_string())).collect::<BTreeMap<String, String>>(),
23        "inputDrvs": drv.input_derivations,
24        "inputSrcs": drv.input_sources,
25        "outputs": drv.outputs,
26        "system": drv.system,
27    })
28}
29
30fn main() {
31    // read A-Term from stdin
32    let mut buf = Vec::new();
33    std::io::stdin()
34        .read_to_end(&mut buf)
35        .expect("failed to read from stdin");
36
37    match Derivation::from_aterm_bytes(&buf) {
38        Ok(drv) => {
39            println!(
40                "{}",
41                serde_json::to_string_pretty(&build_serde_json_value(drv))
42                    .expect("unable to serialize")
43            );
44        }
45        Err(e) => eprintln!("unable to parse derivation: {:#?}", e),
46    }
47}