Revert "pkgs-lib/formats: Use .attrs.json directly for TOML"

This commit is contained in:
Matt Sturgeon
2026-06-10 13:30:28 +01:00
committed by GitHub
parent 9e87430ac7
commit 36709df191
2 changed files with 7 additions and 23 deletions
+4 -2
View File
@@ -472,12 +472,14 @@ optionalAttrs allowAliases aliases
runCommand name
{
nativeBuildInputs = [ json2x ];
inherit value;
value = builtins.toJSON value;
preferLocalBuild = true;
__structuredAttrs = true;
}
''
json2x toml --unwrap value "$NIX_ATTRS_JSON_FILE" "$out"
valuePath="$TMPDIR/value"
printf "%s" "$value" > "$valuePath"
json2x toml "$valuePath" "$out"
''
) { };
+3 -21
View File
@@ -10,9 +10,6 @@ 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,
@@ -32,31 +29,16 @@ enum Format {
fn main() -> anyhow::Result<()> {
let args: Args = argh::from_env();
let json_text = read_to_string(&args.input)
let input = read_to_string(&args.input)
.with_context(|| format!("failed to read {}", args.input.display()))?;
let parsed_json: serde_json::Value = serde_json::from_str(&json_text)
let input: serde_json::Value = serde_json::from_str(&input)
.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(output_value).context("failed to serialize value to toml")?;
let content = toml::to_string(&input).context("failed to serialize value to toml")?;
output
.write_all(content.as_bytes())
.with_context(|| format!("failed to write to {}", args.output.display()))?;