feat(tvix/eval): implement builtins.toPath

This commit implements builtins.toPath.  Like OP_ADD, it currently
does not handle string contexts.

This commit allows the
tests::nix_eval_okay_src_tests_nix_tests_eval_okay_pathexists_nix
test to pass.

Signed-off-by: Adam Joseph <adam@westernsemico.com>
Change-Id: Iadd4f7605f8f297adbd0dba187b8481c21370b6e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6996
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
This commit is contained in:
Adam Joseph 2022-10-12 23:13:10 -07:00
parent f868e730be
commit ad71fdaa75

View file

@ -21,12 +21,11 @@ use self::versions::{VersionPart, VersionPartsIter};
pub mod impure; pub mod impure;
pub mod versions; pub mod versions;
/// Coerce a Nix Value to a plain path, e.g. in order to access the file it /// Coerce a Nix Value to a plain path, e.g. in order to access the
/// points to in an I/O builtin. This coercion can _never_ be performed in /// file it points to via either `builtins.toPath` or an impure
/// a Nix program directly (i.e. the trick `path: /. + path` to convert from /// builtin. This coercion can _never_ be performed in a Nix program
/// a string to a path wouldn't hit this code), so the target file /// without using builtins (i.e. the trick `path: /. + path` to
/// doesn't need to be realised or imported into the Nix store. /// convert from a string to a path wouldn't hit this code).
#[allow(dead_code)] // TODO(sterni): remove this once the function is in use
pub fn coerce_value_to_path(v: &Value, vm: &mut VM) -> Result<PathBuf, ErrorKind> { pub fn coerce_value_to_path(v: &Value, vm: &mut VM) -> Result<PathBuf, ErrorKind> {
let value = v.force(vm)?; let value = v.force(vm)?;
match &*value { match &*value {
@ -513,6 +512,10 @@ fn pure_builtins() -> Vec<Builtin> {
} }
Ok(Value::attrs(NixAttrs::from_map(res))) Ok(Value::attrs(NixAttrs::from_map(res)))
}), }),
Builtin::new("toPath", &[false], |args: Vec<Value>, vm: &mut VM| {
let path: Value = crate::value::canon_path(coerce_value_to_path(&args[0], vm)?).into();
Ok(path.coerce_to_string(CoercionKind::Weak, vm)?.into())
}),
Builtin::new("typeOf", &[false], |args: Vec<Value>, vm: &mut VM| { Builtin::new("typeOf", &[false], |args: Vec<Value>, vm: &mut VM| {
// We force manually here because it also unwraps the Thunk // We force manually here because it also unwraps the Thunk
// representation, if any. // representation, if any.