diff options
author | Ryan Lahfa <tvl@lahfa.xyz> | 2023-12-26T01·18+0100 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-01-03T18·20+0000 |
commit | 09ec8b6fcf6ea5c04193c4539e4265a5c11a3386 (patch) | |
tree | ca58bccfa032b7a4cf5e8ac54fedb83727ee108e /tvix/eval/src/builtins/mod.rs | |
parent | 96d06031affefeb0ee312e5fa5a6fcea29a7dff9 (diff) |
feat(tvix/eval): implement `getContext` primop r/7331
Change-Id: I2c5068a28f9883a01b0ff80a5e5ab32ba18bfc1a Reviewed-on: https://cl.tvl.fyi/c/depot/+/10437 Reviewed-by: tazjin <tazjin@tvl.su> Autosubmit: raitobezarius <tvl@lahfa.xyz> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/builtins/mod.rs')
-rw-r--r-- | tvix/eval/src/builtins/mod.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 246efff12b77..deb0f84ccc9e 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -23,6 +23,8 @@ use crate::{ value::{CoercionKind, NixAttrs, NixList, NixString, Thunk, Value}, }; +use crate::NixContextElement; + use self::versions::{VersionPart, VersionPartsIter}; mod to_xml; @@ -581,6 +583,47 @@ mod pure_builtins { Ok(Value::Bool(v.has_context())) } + #[builtin("getContext")] + #[allow(non_snake_case)] + async fn builtin_getContext(co: GenCo, e: Value) -> Result<Value, ErrorKind> { + // also forces the value + let span = generators::request_span(&co).await; + let v = e + .coerce_to_string( + co, + CoercionKind { + strong: true, + import_paths: true, + }, + span, + ) + .await?; + let s = v.to_contextful_str()?; + + let elements = s + .iter_context() + .flat_map(|context| context.iter()) + .map(|ctx_element| match ctx_element { + NixContextElement::Plain(spath) => ( + spath.clone(), + Value::attrs(NixAttrs::from_iter([("path", true)])), + ), + NixContextElement::Single { name, derivation } => ( + derivation.clone(), + Value::attrs(NixAttrs::from_iter([( + "outputs", + Value::List(NixList::construct(1, vec![name.clone().into()])), + )])), + ), + NixContextElement::Derivation(drv_path) => ( + drv_path.clone(), + Value::attrs(NixAttrs::from_iter([("allOutputs", true)])), + ), + }); + + Ok(Value::attrs(NixAttrs::from_iter(elements))) + } + #[builtin("hashString")] #[allow(non_snake_case)] async fn builtin_hashString( |