diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 0b19a89aa..cabdaf007 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -100,6 +100,11 @@ fn pure_builtins() -> Vec { Ok(Value::List(NixList::construct(output.len(), output))) }), + Builtin::new("baseNameOf", &[true], |args: Vec, vm: &mut VM| { + let s = args[0].coerce_to_string(CoercionKind::Weak, vm)?; + let result: String = s.rsplit_once('/').map(|(_, x)| x).unwrap_or(&s).into(); + Ok(result.into()) + }), Builtin::new("bitAnd", &[true, true], |args: Vec, _: &mut VM| { Ok(Value::Integer(args[0].as_int()? & args[1].as_int()?)) }), diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp new file mode 100644 index 000000000..60a773f4a --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp @@ -0,0 +1 @@ +[ "bar" "foo" "" "bar" "." "" "" "" ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix new file mode 100644 index 000000000..bc59613f5 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix @@ -0,0 +1,10 @@ +[ + (builtins.baseNameOf /foo/bar) + (builtins.baseNameOf "foo") + (builtins.baseNameOf "foo///") + (builtins.baseNameOf "foo/bar") + (builtins.baseNameOf "./.") + (builtins.baseNameOf "") + (builtins.baseNameOf /.) + (builtins.toString (builtins.baseNameOf /.)) +]