use anyhow::{ anyhow, Context, Result, }; use clap::{ Parser, }; use serde::{ Deserialize, }; use std::collections::HashMap; use std::process::Command; #[derive(Deserialize, Debug)] struct FlakeMetadata { lastModified: i64, locked: FlakeLockedInfo, locks: FlakeLocks, original: FlakeSource, path: String, resolved: FlakeSource, revision: String, } #[derive(Deserialize, Debug)] struct FlakeSource { owner: Option, repo: Option, r#ref: Option, rev: Option, r#type: String, url: Option, } fn assemble_flake_git_uri(flake_source: &FlakeSource) -> Result { let mut out = String::new(); out.push_str("git+"); out.push_str(&flake_source.url.clone() .context("Flake git uri does not contain an url")?); let mut query: Vec = Vec::new(); if let Some(r#ref) = &flake_source.r#ref { query.push(format!("ref={}", r#ref)); } if let Some(rev) = &flake_source.rev { query.push(format!("rev={}", rev)); } if query.len() > 0 { out.push_str("?"); out.push_str(&query.join("&")); } Ok(out) } fn assemble_flake_github_uri(flake_source: &FlakeSource) -> Result { let mut out = String::new(); out.push_str("github:"); out.push_str(&flake_source.owner.clone() .context("Flake github uri does not contain an owner")?); out.push_str("/"); out.push_str(&flake_source.repo.clone() .context("Flake github uri does not contain an repo")?); if let Some(rev) = &flake_source.rev { out.push_str("/"); out.push_str(&rev); } Ok(out) } fn assemble_flake_tarball_uri(flake_source: &FlakeSource) -> Result { let mut out = String::new(); out.push_str(&flake_source.url.clone() .context("Flake tarball uri does not contain an url")?); if let Some(rev) = &flake_source.rev { out.push_str("?rev="); out.push_str(rev); } Ok(out) } trait FlakeUri { fn flake_uri(&self) -> Result; } impl FlakeUri for FlakeSource { fn flake_uri(&self) -> Result { match self.r#type.as_str() { "git" => assemble_flake_git_uri(self), "github" => assemble_flake_github_uri(self), "tarball" => assemble_flake_tarball_uri(self), other => Err(anyhow!("Unsupported flake uri type {}", other)), } } } #[derive(Deserialize, Debug)] struct FlakeLockedInfo { lastModified: i64, narHash: String, owner: Option, repo: Option, r#ref: Option, rev: Option, r#type: String, url: Option, } impl FlakeUri for FlakeLockedInfo { fn flake_uri(&self) -> Result { Into::::into(self).flake_uri() } } impl Into for &FlakeLockedInfo { fn into(self) -> FlakeSource { FlakeSource { owner: self.owner.clone(), repo: self.repo.clone(), r#ref: self.r#ref.clone(), rev: self.rev.clone(), r#type: self.r#type.clone(), url: self.url.clone(), } } } #[derive(Deserialize, Debug)] struct FlakeLocks { nodes: HashMap, root: String, version: u64, } #[derive(Deserialize, Debug)] #[serde(untagged)] enum FlakeLocksNodeInputs { String(String), Array(Vec), } #[derive(Deserialize, Debug)] struct FlakeLocksNode { inputs: Option>, locked: Option, original: Option, } #[derive(Parser)] #[command(version, about, long_about = None)] struct Cli { flake_uri: String, } fn main() -> Result<()> { let cli = Cli::parse(); let flake_metadata_raw = Command::new("nix") .arg("flake") .arg("metadata") .arg("--json") .arg(cli.flake_uri) .output() .context("Failed to fetch flake metadata")?; let flake_metadata: FlakeMetadata = serde_json::from_slice(&flake_metadata_raw.stdout) .context("Failed to parse flake metadata")?; println!("{}", flake_metadata.original.flake_uri()?); for (input_name, input_data) in &flake_metadata.locks.nodes { if let Some(original_flake_source) = &input_data.original { println!("{}", original_flake_source.flake_uri()?); } if let Some(locked_flake_source) = &input_data.locked { println!("{}", locked_flake_source.flake_uri()?); } } Ok(()) }