about summary refs log tree commit diff
path: root/tvix/nix-compat/src/derivation/utils.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-02-01T10·32+0100
committerclbot <clbot@tvl.fyi>2023-02-01T13·43+0000
commit366d2c5e92dd36c3bb81397c8031c6e0227de3ce (patch)
tree41da409d9013dcbd3c09fc36aedc14eda63ecd30 /tvix/nix-compat/src/derivation/utils.rs
parent2551adb4f1b127fd5bef4c2bb96fd361e4fcc0df (diff)
refactor(tvix/nix-compat/derivation): remove module inception r/5805
The module `src/derivation/derivation.rs` is a sign of module inception.

Move the Derivation struct definiton up into `src/derivation/mod.rs`,
and some of the helpers in a `util.rs`.

Change-Id: Ib24a5f8a27bdd45df8b1fa2b3482a79b33cab8d5
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7997
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/derivation/utils.rs')
-rw-r--r--tvix/nix-compat/src/derivation/utils.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/derivation/utils.rs b/tvix/nix-compat/src/derivation/utils.rs
new file mode 100644
index 000000000000..77ac94b42462
--- /dev/null
+++ b/tvix/nix-compat/src/derivation/utils.rs
@@ -0,0 +1,78 @@
+use crate::derivation::DerivationError;
+use crate::nixbase32;
+use crate::store_path::{self, StorePath};
+use sha2::{Digest, Sha256};
+
+/// compress_hash takes an arbitrarily long sequence of bytes (usually
+/// a hash digest), and returns a sequence of bytes of length
+/// output_size.
+///
+/// It's calculated by rotating through the bytes in the output buffer
+/// (zero- initialized), and XOR'ing with each byte of the passed
+/// input. It consumes 1 byte at a time, and XOR's it with the current
+/// value in the output buffer.
+///
+/// This mimics equivalent functionality in C++ Nix.
+fn compress_hash(input: &[u8], output_size: usize) -> Vec<u8> {
+    let mut output: Vec<u8> = vec![0; output_size];
+
+    for (ii, ch) in input.iter().enumerate() {
+        output[ii % output_size] ^= ch;
+    }
+
+    output
+}
+
+/// This returns a store path, either of a derivation or a regular output.
+/// The string is hashed with sha256, its digest is compressed to 20 bytes, and
+/// nixbase32-encoded (32 characters)
+pub(super) fn build_store_path(
+    is_derivation: bool,
+    fingerprint: &str,
+    name: &str,
+) -> Result<StorePath, DerivationError> {
+    let digest = {
+        let mut hasher = Sha256::new();
+        hasher.update(fingerprint);
+        hasher.finalize()
+    };
+    let compressed = compress_hash(&digest, 20);
+    if is_derivation {
+        StorePath::from_string(format!("{}-{}.drv", nixbase32::encode(&compressed), name).as_str())
+    } else {
+        StorePath::from_string(format!("{}-{}", nixbase32::encode(&compressed), name,).as_str())
+    }
+    .map_err(|_e| DerivationError::InvalidOutputName(name.to_string()))
+    // Constructing the StorePath can only fail if the passed output name was
+    // 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<S: AsRef<str>, I: IntoIterator<Item = S>, C: AsRef<[u8]>>(
+    name: &str,
+    content: C,
+    references: I,
+) -> Result<StorePath, DerivationError> {
+    let mut s = String::from("text");
+
+    for reference in references {
+        s.push(':');
+        s.push_str(reference.as_ref());
+    }
+
+    let content_digest = {
+        let mut hasher = Sha256::new();
+        hasher.update(content);
+        hasher.finalize()
+    };
+
+    s.push_str(&format!(
+        ":sha256:{:x}:{}:{}",
+        content_digest,
+        store_path::STORE_DIR,
+        name
+    ));
+
+    build_store_path(false, &s, name)
+}