Skip to content

Commit

Permalink
imp: hidden option to swap eval.nix
Browse files Browse the repository at this point in the history
- this is useful for custom evaluators which implement custom
  hive or flake schemata.

- as long as they satisfy the `hive` contract (currently a bit implicit)

closes: zhaofengli#81
  • Loading branch information
blaggacao committed May 26, 2022
1 parent 429a0f5 commit 0f7e2f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ For more details, read the manual at <{}>.
format!(r#"If this argument is not specified, Colmena will search upwards from the current working directory for a file named "flake.nix" or "hive.nix". This behavior is disabled if --config/-f is given explicitly.
For a sample configuration, check the manual at <{}>.
"#, MANUAL_URL)
};

static ref EVAL_HELP: String = {
format!(
r#"Specify a custom eval.nix file that can consume either a hive.nix or a flake.nix and extract the Hive's contract out of it.
If this argument is not specified, Colmena will use a builtin eval.nix.
If this argument is specified, the aspects of the official manual about the colmena nix schema & contracts
might not be true any more.
For more details, read the manual at <{}>.
"#, MANUAL_URL)
};
}
Expand Down Expand Up @@ -111,6 +122,13 @@ pub fn build_cli(include_internal: bool) -> ClapCommand<'static> {
// is explicitly supplied by the user (occurrences_of > 0).
.default_value("hive.nix")
.global(true))
.arg(Arg::new("eval")
.long("eval")
.hide(true)
.value_name("EVAL")
.help("Path to a custom nix evaluator expression that satisfies the Hive contract")
.long_help(Some(EVAL_HELP.as_str()))
.global(true))
.arg(Arg::new("show-trace")
.long("show-trace")
.help("Show debug information for Nix commands")
Expand Down
19 changes: 15 additions & 4 deletions src/nix/hive/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::io::Write;
use std::io::{Read, Write};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::convert::AsRef;

Expand Down Expand Up @@ -53,7 +54,7 @@ pub struct Hive {
/// or "flake.nix".
context_dir: Option<PathBuf>,

/// Path to temporary file containing eval.nix.
/// Path to a temporary file containing the builtin eval.nix or to the user supplied eval.nix.
eval_nix: TempPath,

/// Path to temporary file containing options.nix.
Expand Down Expand Up @@ -117,14 +118,24 @@ impl HivePath {
}

impl Hive {
pub fn new(path: HivePath) -> ColmenaResult<Self> {
pub fn new(path: HivePath, eval: Option<&Path>) -> ColmenaResult<Self> {
let mut eval_nix = NamedTempFile::new()?;
let mut options_nix = NamedTempFile::new()?;
let mut modules_nix = NamedTempFile::new()?;
eval_nix.write_all(HIVE_EVAL).unwrap();
options_nix.write_all(HIVE_OPTIONS).unwrap();
modules_nix.write_all(HIVE_MODULES).unwrap();

if let Some(p) = eval {
let mut f = File::open(p)?;
let mut buffer = Vec::new();

// read the whole file
f.read_to_end(&mut buffer)?;
eval_nix.write_all(&buffer).unwrap();
} else {
eval_nix.write_all(HIVE_EVAL).unwrap();
}

let context_dir = path.context_dir();

Ok(Self {
Expand Down
12 changes: 9 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::convert::TryFrom;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Stdio;

use async_trait::async_trait;
Expand Down Expand Up @@ -184,6 +184,12 @@ impl CommandExt for CommandExecution {
}

pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
let eval = match args.value_of("eval") {
Some(s) => {
Some(Path::new(s))
}
None => None,
};
let path = match args.occurrences_of("config") {
0 => {
// traverse upwards until we find hive.nix
Expand Down Expand Up @@ -227,7 +233,7 @@ pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
// Treat as flake URI
let flake = Flake::from_uri(path).await?;
let hive_path = HivePath::Flake(flake);
let mut hive = Hive::new(hive_path)?;
let mut hive = Hive::new(hive_path, eval)?;

if args.is_present("show-trace") {
hive.set_show_trace(true);
Expand All @@ -250,7 +256,7 @@ pub async fn hive_from_args(args: &ArgMatches) -> ColmenaResult<Hive> {
}
}

let mut hive = Hive::new(hive_path)?;
let mut hive = Hive::new(hive_path, eval)?;

if args.is_present("show-trace") {
hive.set_show_trace(true);
Expand Down

0 comments on commit 0f7e2f5

Please sign in to comment.