pkgs-lib/formats: Use .attrs.json directly for TOML (#528060)

This commit is contained in:
Gaétan Lepage
2026-06-05 19:32:28 +00:00
committed by GitHub
2 changed files with 23 additions and 7 deletions

View File

@@ -472,14 +472,12 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ json2x ];
value = builtins.toJSON value;
inherit value;
preferLocalBuild = true;
__structuredAttrs = true;
}
''
valuePath="$TMPDIR/value"
printf "%s" "$value" > "$valuePath"
json2x toml "$valuePath" "$out"
json2x toml --unwrap value "$NIX_ATTRS_JSON_FILE" "$out"
''
) { };

View File

@@ -10,6 +10,9 @@ use argh::{FromArgValue, FromArgs};
/// Convert a JSON file to another format
#[derive(FromArgs)]
struct Args {
/// convert only value of this attribute
#[argh(option)]
unwrap: Option<String>,
/// format of the output file, possible values: toml
#[argh(positional)]
format: Format,
@@ -29,16 +32,31 @@ enum Format {
fn main() -> anyhow::Result<()> {
let args: Args = argh::from_env();
let input = read_to_string(&args.input)
let json_text = read_to_string(&args.input)
.with_context(|| format!("failed to read {}", args.input.display()))?;
let input: serde_json::Value = serde_json::from_str(&input)
let parsed_json: serde_json::Value = serde_json::from_str(&json_text)
.with_context(|| format!("failed to parse {}", args.input.display()))?;
let output_value = if let Some(unwrap_key) = args.unwrap {
parsed_json
.as_object()
.ok_or_else(|| anyhow::anyhow!("{} does not contain an object", args.input.display()))?
.get(&unwrap_key)
.ok_or_else(|| {
anyhow::anyhow!(
"{} does not containt attribute '{unwrap_key}'",
args.input.display()
)
})?
} else {
&parsed_json
};
let mut output = File::create(&args.output)
.with_context(|| format!("failed to create {}", args.output.display()))?;
match args.format {
Format::Toml => {
let content = toml::to_string(&input).context("failed to serialize value to toml")?;
let content =
toml::to_string(output_value).context("failed to serialize value to toml")?;
output
.write_all(content.as_bytes())
.with_context(|| format!("failed to write to {}", args.output.display()))?;