Skip to content

Commit

Permalink
Make system optional, defaulting to builtins.currentSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Jan 30, 2024
1 parent 1d40147 commit 5d6ef7b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod nix;
fn process(
collected_paths: &Arc<Mutex<std::collections::HashSet<String>>>,
flake_ref: &str,
system: &str,
system: Option<&str>,
attribute_path: &str,
offline: &bool,
) -> Vec<DerivationDescription> {
Expand Down Expand Up @@ -60,18 +60,21 @@ fn process(

pub fn nixtract(
flake_ref: impl AsRef<str>,
system: impl AsRef<str>,
system: Option<impl AsRef<str>>,
attribute_path: Option<impl AsRef<str>>,
offline: &bool,
) -> Result<Vec<DerivationDescription>> {
let flake_ref = flake_ref.as_ref();
let system = system.as_ref();
let attribute_path = attribute_path.as_ref();
// Convert system to a Option<&str>
let system = system.as_ref().map(AsRef::as_ref);
let attribute_path = attribute_path.as_ref().map(AsRef::as_ref);

log::info!(
"Starting nixtract with flake_ref: {}, system: {}, attribute_path: {:?}",
flake_ref,
system,
system
.map(AsRef::as_ref)
.unwrap_or("builtins.currentSystem"),
attribute_path.map(AsRef::as_ref).unwrap_or("")
);

Expand Down
23 changes: 19 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use clap::Parser;
use nixtract::nixtract;

Expand All @@ -9,11 +11,15 @@ struct Args {
#[arg(short, long = "target-attribute-path")]
attribute_path: Option<String>,
#[arg(short, long = "target-system")]
system: String,
system: Option<String>,
#[arg(long, default_value_t = false)]
offline: bool,
#[arg(long, default_value_t = false)]
pretty: bool,
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,
#[arg()]
output_path: Option<PathBuf>,
}

fn main() {
Expand All @@ -33,8 +39,17 @@ fn main() {
)
.unwrap();

// Display the results
for result in results {
println!("{}", serde_json::to_string(&result).unwrap());
// Print the results
let output = if opts.pretty {
serde_json::to_string_pretty(&results).unwrap()
} else {
serde_json::to_string(&results).unwrap()
};

if let Some(output_path) = opts.output_path {
log::info!("Writing results to {:?}", output_path);
std::fs::write(output_path, output).unwrap();
} else {
println!("{}", output);
}
}
2 changes: 1 addition & 1 deletion src/nix/describe_derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let

# Arguments have to be taken from environment when using `nix` command
targetFlakeRef = builtins.getEnv "TARGET_FLAKE_REF";
targetSystem = builtins.getEnv "TARGET_SYSTEM";
targetAttributePath = builtins.getEnv "TARGET_ATTRIBUTE_PATH";
targetSystem = let env = builtins.getEnv "TARGET_SYSTEM"; in if env == "" then builtins.currentSystem else env;

# Get pkgs
targetFlake = builtins.getFlake targetFlakeRef;
Expand Down
24 changes: 15 additions & 9 deletions src/nix/describe_derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,28 @@ pub struct BuiltInput {

pub fn describe_derivation(
flake_ref: impl AsRef<str>,
system: impl AsRef<str>,
system: Option<impl AsRef<str>>,
attribute_path: impl AsRef<str>,
offline: &bool,
) -> Result<DerivationDescription> {
let lib = Lib::new()?;

let expr = include_str!("describe_derivation.nix");

let env_vars = HashMap::from([
("TARGET_FLAKE_REF".to_owned(), flake_ref.as_ref().to_owned()),
("TARGET_SYSTEM".to_owned(), system.as_ref().to_owned()),
(
"TARGET_ATTRIBUTE_PATH".to_owned(),
attribute_path.as_ref().to_owned(),
),
]);
// Create a scope so env_vars isn't needlessly mutable
let env_vars: HashMap<String, String> = {
let mut res = HashMap::from([
("TARGET_FLAKE_REF".to_owned(), flake_ref.as_ref().to_owned()),
(
"TARGET_ATTRIBUTE_PATH".to_owned(),
attribute_path.as_ref().to_owned(),
),
]);
if let Some(system) = system {
res.insert("TARGET_SYSTEM".to_owned(), system.as_ref().to_owned());
}
res
};

// Run the nix command, with the provided environment variables and expression
let mut command: Command = Command::new("nix");
Expand Down
2 changes: 1 addition & 1 deletion src/nix/find_attribute_paths.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let
# Arguments have to be taken from environment when using `nix` command
targetFlakeRef = builtins.getEnv "TARGET_FLAKE_REF";
targetAttributePath = builtins.getEnv "TARGET_ATTRIBUTE_PATH";
targetSystem = builtins.getEnv "TARGET_SYSTEM";
targetSystem = let env = builtins.getEnv "TARGET_SYSTEM"; in if env == "" then builtins.currentSystem else env;

# Get pkgs
targetFlake = builtins.getFlake targetFlakeRef;
Expand Down
11 changes: 6 additions & 5 deletions src/nix/find_attribute_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct FoundDrv {

pub fn find_attribute_paths(
flake_ref: impl AsRef<str>,
system: impl AsRef<str>,
system: Option<impl AsRef<str>>,
attribute_path: Option<impl AsRef<str>>,
offline: &bool,
) -> Result<Vec<AttributePaths>> {
Expand All @@ -31,16 +31,17 @@ pub fn find_attribute_paths(

// Create a scope so env_vars isn't needlessly mutable
let env_vars: HashMap<String, String> = {
let mut res = HashMap::from([
("TARGET_FLAKE_REF".to_owned(), flake_ref.as_ref().to_owned()),
("TARGET_SYSTEM".to_owned(), system.as_ref().to_owned()),
]);
let mut res =
HashMap::from([("TARGET_FLAKE_REF".to_owned(), flake_ref.as_ref().to_owned())]);
if let Some(attribute_path) = attribute_path {
res.insert(
"TARGET_ATTRIBUTE_PATH".to_owned(),
attribute_path.as_ref().to_owned(),
);
}
if let Some(system) = system {
res.insert("TARGET_SYSTEM".to_owned(), system.as_ref().to_owned());
}
res
};

Expand Down

0 comments on commit 5d6ef7b

Please sign in to comment.