2022-10-03 10:26:32 +02:00
|
|
|
use std::{
|
2022-10-04 22:58:21 +02:00
|
|
|
cell::RefCell,
|
2022-10-04 17:27:49 +02:00
|
|
|
collections::{BTreeMap, HashMap},
|
2022-10-10 06:47:45 +02:00
|
|
|
env,
|
2022-10-10 06:47:25 +02:00
|
|
|
fs::File,
|
|
|
|
io::{self, Read},
|
2022-10-04 17:27:49 +02:00
|
|
|
rc::Rc,
|
2022-10-03 10:26:32 +02:00
|
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2022-10-04 17:27:49 +02:00
|
|
|
errors::ErrorKind,
|
|
|
|
observer::NoOpObserver,
|
2022-10-15 20:30:29 +02:00
|
|
|
value::{Builtin, NixAttrs, NixString, Thunk},
|
2022-10-04 17:27:49 +02:00
|
|
|
vm::VM,
|
|
|
|
SourceCode, Value,
|
2022-10-03 10:26:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
fn impure_builtins() -> Vec<Builtin> {
|
2022-10-10 06:03:17 +02:00
|
|
|
vec![
|
2022-10-15 20:30:29 +02:00
|
|
|
Builtin::new("getEnv", &[true], |args: Vec<Value>, _: &mut VM| {
|
|
|
|
Ok(env::var(args[0].to_str()?)
|
2022-10-10 06:47:45 +02:00
|
|
|
.unwrap_or_else(|_| "".into())
|
|
|
|
.into())
|
|
|
|
}),
|
2022-10-10 06:03:17 +02:00
|
|
|
Builtin::new("pathExists", &[true], |args: Vec<Value>, vm: &mut VM| {
|
|
|
|
Ok(super::coerce_value_to_path(&args[0], vm)?.exists().into())
|
|
|
|
}),
|
|
|
|
Builtin::new("readDir", &[true], |args: Vec<Value>, vm: &mut VM| {
|
2022-09-06 22:56:40 +02:00
|
|
|
let path = super::coerce_value_to_path(&args[0], vm)?;
|
|
|
|
let mk_err = |err: io::Error| ErrorKind::IO {
|
|
|
|
path: Some(path.clone()),
|
|
|
|
error: Rc::new(err),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut res = BTreeMap::new();
|
|
|
|
for entry in path.read_dir().map_err(mk_err)? {
|
|
|
|
let entry = entry.map_err(mk_err)?;
|
|
|
|
let file_type = entry
|
|
|
|
.metadata()
|
|
|
|
.map_err(|err| ErrorKind::IO {
|
|
|
|
path: Some(entry.path()),
|
|
|
|
error: Rc::new(err),
|
|
|
|
})?
|
|
|
|
.file_type();
|
|
|
|
let val = if file_type.is_dir() {
|
|
|
|
"directory"
|
|
|
|
} else if file_type.is_file() {
|
|
|
|
"regular"
|
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
"symlink"
|
|
|
|
} else {
|
|
|
|
"unknown"
|
|
|
|
};
|
|
|
|
res.insert(
|
|
|
|
entry.file_name().to_string_lossy().as_ref().into(),
|
|
|
|
val.into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(Value::attrs(NixAttrs::from_map(res)))
|
2022-10-10 06:03:17 +02:00
|
|
|
}),
|
2022-10-10 06:47:25 +02:00
|
|
|
Builtin::new("readFile", &[true], |args: Vec<Value>, vm: &mut VM| {
|
|
|
|
let mut buf = String::new();
|
|
|
|
File::open(&super::coerce_value_to_path(&args[0], vm)?)?.read_to_string(&mut buf)?;
|
|
|
|
Ok(buf.into())
|
|
|
|
}),
|
2022-10-10 06:03:17 +02:00
|
|
|
]
|
2022-10-03 10:26:32 +02:00
|
|
|
}
|
2022-09-18 22:34:41 +02:00
|
|
|
|
2022-10-04 17:27:49 +02:00
|
|
|
/// Return all impure builtins, that is all builtins which may perform I/O
|
|
|
|
/// outside of the VM and so cannot be used in all contexts (e.g. WASM).
|
2022-10-03 10:26:32 +02:00
|
|
|
pub(super) fn builtins() -> BTreeMap<NixString, Value> {
|
|
|
|
let mut map: BTreeMap<NixString, Value> = impure_builtins()
|
|
|
|
.into_iter()
|
|
|
|
.map(|b| (b.name().into(), Value::Builtin(b)))
|
|
|
|
.collect();
|
|
|
|
|
2022-10-02 19:15:51 +02:00
|
|
|
// currentTime pins the time at which evaluation was started
|
|
|
|
{
|
|
|
|
let seconds = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
|
|
|
Ok(dur) => dur.as_secs() as i64,
|
|
|
|
|
|
|
|
// This case is hit if the system time is *before* epoch.
|
|
|
|
Err(err) => -(err.duration().as_secs() as i64),
|
|
|
|
};
|
|
|
|
|
|
|
|
map.insert(NixString::from("currentTime"), Value::Integer(seconds));
|
|
|
|
}
|
|
|
|
|
2022-10-03 10:26:32 +02:00
|
|
|
map
|
2022-09-18 22:34:41 +02:00
|
|
|
}
|
2022-10-04 17:27:49 +02:00
|
|
|
|
|
|
|
/// Constructs and inserts the `import` builtin. This builtin is special in that
|
|
|
|
/// it needs to capture the [crate::SourceCode] structure to correctly track
|
|
|
|
/// source code locations while invoking a compiler.
|
|
|
|
// TODO: need to be able to pass through a CompilationObserver, too.
|
2022-10-04 22:58:21 +02:00
|
|
|
pub fn builtins_import(
|
|
|
|
globals: Rc<RefCell<HashMap<&'static str, Value>>>,
|
|
|
|
source: SourceCode,
|
|
|
|
) -> Builtin {
|
2022-10-04 17:27:49 +02:00
|
|
|
Builtin::new(
|
|
|
|
"import",
|
|
|
|
&[true],
|
2022-10-05 16:01:07 +02:00
|
|
|
move |mut args: Vec<Value>, vm: &mut VM| {
|
2022-10-08 19:48:08 +02:00
|
|
|
let mut path = super::coerce_value_to_path(&args.pop().unwrap(), vm)?;
|
|
|
|
if path.is_dir() {
|
|
|
|
path.push("default.nix");
|
|
|
|
}
|
2022-10-04 17:27:49 +02:00
|
|
|
|
|
|
|
let contents =
|
|
|
|
std::fs::read_to_string(&path).map_err(|err| ErrorKind::ReadFileError {
|
|
|
|
path: path.clone(),
|
|
|
|
error: Rc::new(err),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let parsed = rnix::ast::Root::parse(&contents);
|
|
|
|
let errors = parsed.errors();
|
|
|
|
|
2022-10-06 16:22:17 +02:00
|
|
|
let file = source.add_file(path.to_string_lossy().to_string(), contents);
|
|
|
|
|
2022-10-04 17:27:49 +02:00
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(ErrorKind::ImportParseError {
|
|
|
|
path,
|
2022-10-06 16:22:17 +02:00
|
|
|
file,
|
2022-10-04 17:27:49 +02:00
|
|
|
errors: errors.to_vec(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = crate::compile(
|
|
|
|
&parsed.tree().expr().unwrap(),
|
|
|
|
Some(path.clone()),
|
|
|
|
file,
|
2022-10-04 22:58:21 +02:00
|
|
|
globals.clone(),
|
2022-10-04 17:27:49 +02:00
|
|
|
&mut NoOpObserver::default(),
|
|
|
|
)
|
|
|
|
.map_err(|err| ErrorKind::ImportCompilerError {
|
|
|
|
path: path.clone(),
|
|
|
|
errors: vec![err],
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if !result.errors.is_empty() {
|
|
|
|
return Err(ErrorKind::ImportCompilerError {
|
|
|
|
path,
|
|
|
|
errors: result.errors,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-05 16:01:07 +02:00
|
|
|
for warning in result.warnings {
|
|
|
|
vm.push_warning(warning);
|
|
|
|
}
|
2022-10-04 17:27:49 +02:00
|
|
|
|
|
|
|
// Compilation succeeded, we can construct a thunk from whatever it spat
|
|
|
|
// out and return that.
|
2022-10-10 22:50:28 +02:00
|
|
|
Ok(Value::Thunk(Thunk::new(result.lambda, vm.current_span())))
|
2022-10-04 17:27:49 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|