cli: Add --nix-option to allow passing arbitrary nix options

This commit is contained in:
ThinkChaos 2022-11-10 12:51:32 -05:00 committed by Zhaofeng Li
parent 5432fe488e
commit 19bf776e8e
4 changed files with 52 additions and 12 deletions

View file

@ -3,8 +3,8 @@
use std::env; use std::env;
use clap::{ use clap::{
builder::PossibleValue, value_parser, Arg, ArgMatches, ColorChoice, Command as ClapCommand, builder::PossibleValue, value_parser, Arg, ArgAction, ArgMatches, ColorChoice,
ValueEnum, Command as ClapCommand, ValueEnum,
}; };
use clap_complete::Shell; use clap_complete::Shell;
use const_format::concatcp; use const_format::concatcp;
@ -160,6 +160,17 @@ pub fn build_cli(include_internal: bool) -> ClapCommand {
.long_help("Passes --impure to Nix commands") .long_help("Passes --impure to Nix commands")
.global(true) .global(true)
.num_args(0)) .num_args(0))
.arg(Arg::new("nix-option")
.long("nix-option")
.help("Passes an arbitrary option to Nix commands")
.long_help(r#"Passes arbitrary options to Nix commands
This only works when building locally.
"#)
.global(true)
.num_args(2)
.value_names(["NAME", "VALUE"])
.action(ArgAction::Append))
.arg(Arg::new("color") .arg(Arg::new("color")
.long("color") .long("color")
.help("When to colorize the output") .help("When to colorize the output")

View file

@ -52,6 +52,9 @@ pub struct Hive {
/// Whether to pass --impure in Nix commands. /// Whether to pass --impure in Nix commands.
impure: bool, impure: bool,
/// Options to pass as --option name value.
nix_options: HashMap<String, String>,
meta_config: OnceCell<MetaConfig>, meta_config: OnceCell<MetaConfig>,
} }
@ -104,6 +107,7 @@ impl Hive {
assets, assets,
show_trace: false, show_trace: false,
impure: false, impure: false,
nix_options: HashMap::new(),
meta_config: OnceCell::new(), meta_config: OnceCell::new(),
}) })
} }
@ -131,12 +135,17 @@ impl Hive {
self.impure = impure; self.impure = impure;
} }
pub fn add_nix_option(&mut self, name: String, value: String) {
self.nix_options.insert(name, value);
}
/// Returns Nix options to set for this Hive. /// Returns Nix options to set for this Hive.
pub fn nix_options(&self) -> NixOptions { pub fn nix_options(&self) -> NixOptions {
let mut options = NixOptions::default(); let mut options = NixOptions::default();
options.set_show_trace(self.show_trace); options.set_show_trace(self.show_trace);
options.set_pure_eval(self.path.is_flake()); options.set_pure_eval(self.path.is_flake());
options.set_impure(self.impure); options.set_impure(self.impure);
options.set_options(self.nix_options.clone());
options options
} }

View file

@ -111,6 +111,9 @@ pub struct NixOptions {
/// - `@/path/to/machines` /// - `@/path/to/machines`
/// - `builder@host.tld riscv64-linux /home/nix/.ssh/keys/builder.key 8 1 kvm` /// - `builder@host.tld riscv64-linux /home/nix/.ssh/keys/builder.key 8 1 kvm`
builders: Option<String>, builders: Option<String>,
/// Options to pass as --option name value.
options: HashMap<String, String>,
} }
impl NodeName { impl NodeName {
@ -205,6 +208,10 @@ impl NixOptions {
self.builders = builders; self.builders = builders;
} }
pub fn set_options(&mut self, options: HashMap<String, String>) {
self.options = options;
}
pub fn to_args(&self) -> Vec<String> { pub fn to_args(&self) -> Vec<String> {
let mut options = Vec::new(); let mut options = Vec::new();
@ -228,6 +235,12 @@ impl NixOptions {
options.push("--impure".to_string()); options.push("--impure".to_string());
} }
for (name, value) in self.options.iter() {
options.push("--option".to_string());
options.push(name.to_string());
options.push(value.to_string());
}
options options
} }
} }

View file

@ -244,17 +244,8 @@ pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
log::info!("Using flake: {}", flake.uri()); log::info!("Using flake: {}", flake.uri());
let hive_path = HivePath::Flake(flake); let hive_path = HivePath::Flake(flake);
let mut hive = Hive::new(hive_path).await?;
if args.get_flag("show-trace") { return hive_from_path(hive_path, args).await;
hive.set_show_trace(true);
}
if args.get_flag("impure") {
hive.set_impure(true);
}
return Ok(hive);
} }
fpath fpath
@ -263,6 +254,11 @@ pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
}; };
let hive_path = HivePath::from_path(path).await?; let hive_path = HivePath::from_path(path).await?;
hive_from_path(hive_path, args).await
}
pub async fn hive_from_path(hive_path: HivePath, args: &ArgMatches) -> ColmenaResult<Hive> {
match &hive_path { match &hive_path {
HivePath::Legacy(p) => { HivePath::Legacy(p) => {
log::info!("Using configuration: {}", p.to_string_lossy()); log::info!("Using configuration: {}", p.to_string_lossy());
@ -282,6 +278,17 @@ pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
hive.set_impure(true); hive.set_impure(true);
} }
if let Some(opts) = args.get_many::<String>("nix-option") {
let iter = opts.into_iter();
let names = iter.clone().step_by(2);
let values = iter.clone().skip(1).step_by(2);
for (name, value) in names.zip(values) {
hive.add_nix_option(name.to_owned(), value.to_owned());
}
}
Ok(hive) Ok(hive)
} }