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 clap::{
builder::PossibleValue, value_parser, Arg, ArgMatches, ColorChoice, Command as ClapCommand,
ValueEnum,
builder::PossibleValue, value_parser, Arg, ArgAction, ArgMatches, ColorChoice,
Command as ClapCommand, ValueEnum,
};
use clap_complete::Shell;
use const_format::concatcp;
@ -160,6 +160,17 @@ pub fn build_cli(include_internal: bool) -> ClapCommand {
.long_help("Passes --impure to Nix commands")
.global(true)
.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")
.long("color")
.help("When to colorize the output")

View file

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

View file

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

View file

@ -244,17 +244,8 @@ pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
log::info!("Using flake: {}", flake.uri());
let hive_path = HivePath::Flake(flake);
let mut hive = Hive::new(hive_path).await?;
if args.get_flag("show-trace") {
hive.set_show_trace(true);
}
if args.get_flag("impure") {
hive.set_impure(true);
}
return Ok(hive);
return hive_from_path(hive_path, args).await;
}
fpath
@ -263,6 +254,11 @@ pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
};
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 {
HivePath::Legacy(p) => {
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);
}
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)
}