about summary refs log tree commit diff
path: root/tvix/derivation/src/derivation.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-01-23T12·43+0300
committertazjin <tazjin@tvl.su>2023-01-23T12·55+0000
commit5d856ac2e9b24f7b5a72346d8206cc366e1663ae (patch)
treea55159f4c677c5fbc660a9b4cc5a3926e08df877 /tvix/derivation/src/derivation.rs
parentbe7f64ac87ee00aa13b0629ef4b927e14cc30e05 (diff)
feat(tvix/derivation): add `path_with_references` r/5737
This allows the calculation of a store path for a plain string that
potentially contains references. These paths are used for
`builtins.toFile` (and potentially other features of C++ Nix).

Change-Id: Ic507c7f264f362b5e6e628255869e5a4fbe4d788
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7906
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/derivation/src/derivation.rs')
-rw-r--r--tvix/derivation/src/derivation.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs
index 74e68ce1d7c5..1f9ed6146bbd 100644
--- a/tvix/derivation/src/derivation.rs
+++ b/tvix/derivation/src/derivation.rs
@@ -75,6 +75,32 @@ fn build_store_path(
     // invalid, so map errors to a [DerivationError::InvalidOutputName].
 }
 
+/// Build a store path for a literal text file in the store that may
+/// contain references.
+pub fn path_with_references<'a, I: IntoIterator<Item = &'a str>, C: AsRef<[u8]>>(
+    name: &str,
+    content: C,
+    references: I,
+) -> Result<StorePath, DerivationError> {
+    let mut hasher = Sha256::new();
+    hasher.update(content);
+    let content_hash = hasher.finalize_reset();
+
+    hasher.update("text");
+    for reference in references {
+        hasher.update(":");
+        hasher.update(reference);
+    }
+
+    hasher.update(&format!(":sha256:{:x}:", content_hash));
+    hasher.update(STORE_DIR);
+    hasher.update(&format!(":{}", name));
+
+    let digest = hasher.finalize();
+
+    build_store_path(false, &digest, name)
+}
+
 impl Derivation {
     pub fn serialize(&self, writer: &mut impl Write) -> Result<(), fmt::Error> {
         writer.write_str(write::DERIVATION_PREFIX)?;