nix-config-parser

Crates.ionix-config-parser
lib.rsnix-config-parser
version0.2.0
sourcesrc
created_at2023-02-27 22:27:32.414254
updated_at2023-10-03 17:43:10.349638
descriptionA simple parser for the Nix configuration file format
homepage
repositoryhttps://github.com/DeterminateSystems/nix-config-parser
max_upload_size
id796398
size58,864
Cole Helbling (cole-h)

documentation

https://docs.rs/nix-config-parser/latest/nix_config_parser

README

nix-config-parser

A simple parser for the Nix configuration file format.

Based off of https://github.com/NixOS/nix/blob/0079d2943702a7a7fbdd88c0f9a5ad677c334aa8/src/libutil/config.cc#L80-L138.

Usage

Read from a file

use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    std::fs::write(
       "nix.conf",
       b"experimental-features = flakes nix-command\nwarn-dirty = false\n",
    )?;

    let nix_conf = nix_config_parser::NixConfig::parse_file(&std::path::Path::new("nix.conf"))?;

    assert_eq!(
       nix_conf.settings().get("experimental-features").unwrap(),
       "flakes nix-command"
    );
    assert_eq!(nix_conf.settings().get("warn-dirty").unwrap(), "false");

    std::fs::remove_file("nix.conf")?;

    Ok(())
}

Read from a string

use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let nix_conf_string = String::from("experimental-features = flakes nix-command");
    let nix_conf = nix_config_parser::NixConfig::parse_string(nix_conf_string, None)?;

    assert_eq!(
        nix_conf.settings().get("experimental-features").unwrap(),
        "flakes nix-command"
    );

    Ok(())
}
Commit count: 51

cargo fmt