#config-file #config-parser #nix #file-format #error

nix-config-parser

A simple parser for the Nix configuration file format

4 releases

0.2.0 Oct 3, 2023
0.1.3 May 11, 2023
0.1.2 Mar 2, 2023
0.1.1 Feb 27, 2023
0.1.0 Feb 27, 2023

#2925 in Parser implementations

Download history 59/week @ 2024-07-21 95/week @ 2024-07-28 85/week @ 2024-08-04 169/week @ 2024-08-11 134/week @ 2024-08-18 103/week @ 2024-08-25 79/week @ 2024-09-01 117/week @ 2024-09-08 98/week @ 2024-09-15 43/week @ 2024-09-22 100/week @ 2024-09-29 31/week @ 2024-10-06 20/week @ 2024-10-13 26/week @ 2024-10-20 23/week @ 2024-10-27 37/week @ 2024-11-03

107 downloads per month
Used in nix-installer

LGPL-2.1

19KB
220 lines

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(())
}

Dependencies

~1–1.7MB
~32K SLoC