From e9494787547ec75f714a47aa1e9830518a5f4bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Thu, 12 Sep 2024 11:56:04 +0200 Subject: [PATCH] feat: parse installables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christina Sørensen --- crates/nix-weather/src/cli.rs | 5 ++++- crates/nix-weather/src/main.rs | 6 +++++- crates/nix-weather/src/nix.rs | 29 +++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/crates/nix-weather/src/cli.rs b/crates/nix-weather/src/cli.rs index 9f35095..78b8d0d 100644 --- a/crates/nix-weather/src/cli.rs +++ b/crates/nix-weather/src/cli.rs @@ -6,12 +6,15 @@ use clap::{arg, command, crate_authors, value_parser, ArgAction, Command}; const DEFAULT_CACHE: &str = "cache.nixos.org"; +const DEFAULT_INSTALLABLE: &str = "./#nixosConfigurations.{}.config.system.build.toplevel"; pub fn build_cli() -> Command { command!() .author(crate_authors!("\n")) + // TODO: parse multiple installables, like e.g. build does? + .arg(arg!([installable] "A nix installable").required(false)) .arg( - arg!(--cache "check a specific cache") + arg!(--cache "Check a specific cache") .required(false) .default_value(DEFAULT_CACHE), ) diff --git a/crates/nix-weather/src/main.rs b/crates/nix-weather/src/main.rs index d69c3b1..e0ba065 100644 --- a/crates/nix-weather/src/main.rs +++ b/crates/nix-weather/src/main.rs @@ -103,7 +103,11 @@ async fn main() -> io::Result<()> { .build() .unwrap(); - let binding = get_requisites(&host_name, &config_dir); + let binding = get_requisites( + &host_name, + &config_dir, + matches.get_one::("installable").cloned(), + ); let get_requisites_duration = initial_time.elapsed().as_secs(); diff --git a/crates/nix-weather/src/nix.rs b/crates/nix-weather/src/nix.rs index fe8c6ac..bc89f15 100644 --- a/crates/nix-weather/src/nix.rs +++ b/crates/nix-weather/src/nix.rs @@ -9,8 +9,9 @@ use std::{ process::{Command, Stdio}, }; -pub fn get_requisites(host: &str, config_dir: &str) -> String { - let get_drv_path = Command::new("nix") +/// Get nixosConfiguration derivation path +fn get_config_drv_path(host: &str, config_dir: &str) -> std::io::Result { + Command::new("nix") .current_dir(Path::new(config_dir)) .args([ "build", @@ -23,10 +24,30 @@ pub fn get_requisites(host: &str, config_dir: &str) -> String { "--json", ]) .output() - .unwrap(); +} + +/// Get installable derivation path +fn get_installable_drv_path( + host: &str, + config_dir: &str, + installable: &str, +) -> std::io::Result { + Command::new("nix") + .current_dir(Path::new(config_dir)) + .args(["build", "--quiet", installable, "--dry-run", "--json"]) + .output() +} + +pub fn get_requisites(host: &str, config_dir: &str, installable: Option) -> String { + let mut drv_path; + if let Some(installable) = installable { + drv_path = get_installable_drv_path(host, config_dir, &installable).unwrap(); + } else { + drv_path = get_config_drv_path(host, config_dir).unwrap(); + } let drv_path_json: Value = - serde_json::from_str(&String::from_utf8(get_drv_path.stdout).unwrap()).unwrap(); + serde_json::from_str(&String::from_utf8(drv_path.stdout).unwrap()).unwrap(); let drv_path = drv_path_json[0]["drvPath"].clone(); log::debug!("drv_path: {}", &drv_path);