about summary refs log tree commit diff
path: root/tvix/eval/src
diff options
context:
space:
mode:
authorAdam Joseph <adam@westernsemico.com>2022-10-13T06·13-0700
committerAdam Joseph <adam@westernsemico.com>2022-10-14T09·23+0000
commitad71fdaa757709d4c24fb7e1512fd6a9db1a311e (patch)
tree256c0fc41fb2f4ea429caa34dcc3b835a4a2307e /tvix/eval/src
parentf868e730befb174b500c9e7325be9b931474bd5d (diff)
feat(tvix/eval): implement builtins.toPath r/5130
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
Diffstat (limited to 'tvix/eval/src')
-rw-r--r--tvix/eval/src/builtins/mod.rs15
1 files 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<PathBuf, ErrorKind> {
     let value = v.force(vm)?;
     match &*value {
@@ -513,6 +512,10 @@ fn pure_builtins() -> Vec<Builtin> {
             }
             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| {
             // We force manually here because it also unwraps the Thunk
             // representation, if any.