From ad71fdaa757709d4c24fb7e1512fd6a9db1a311e Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Wed, 12 Oct 2022 23:13:10 -0700 Subject: 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 Change-Id: Iadd4f7605f8f297adbd0dba187b8481c21370b6e Reviewed-on: https://cl.tvl.fyi/c/depot/+/6996 Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index a0bc9540d3e7..050481eb7ba3 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -21,12 +21,11 @@ use self::versions::{VersionPart, VersionPartsIter}; pub mod impure; pub mod versions; -/// Coerce a Nix Value to a plain path, e.g. in order to access the file it -/// points to in an I/O builtin. This coercion can _never_ be performed in -/// a Nix program directly (i.e. the trick `path: /. + path` to convert from -/// a string to a path wouldn't hit this code), so the target file -/// doesn't need to be realised or imported into the Nix store. -#[allow(dead_code)] // TODO(sterni): remove this once the function is in use +/// Coerce a Nix Value to a plain path, e.g. in order to access the +/// file it points to via either `builtins.toPath` or an impure +/// builtin. This coercion can _never_ be performed in a Nix program +/// without using builtins (i.e. the trick `path: /. + path` to +/// convert from a string to a path wouldn't hit this code). pub fn coerce_value_to_path(v: &Value, vm: &mut VM) -> Result { let value = v.force(vm)?; match &*value { @@ -513,6 +512,10 @@ fn pure_builtins() -> Vec { } Ok(Value::attrs(NixAttrs::from_map(res))) }), + Builtin::new("toPath", &[false], |args: Vec, 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, vm: &mut VM| { // We force manually here because it also unwraps the Thunk // representation, if any. -- cgit 1.4.1