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-03-30T12·01+0200
committerclbot <clbot@tvl.fyi>2023-03-30T14·03+0000
commit5f2c2e79e1778cda877d6122dd24be08740c8720 (patch)
treeb87dba0063a193a39c1bf57a2b1e3513039e144c /tvix/nix-compat/src/derivation/utils.rs
parent4ab180b1ebf411d7d7ebd98ae17c25945b17e2c2 (diff)
refactor(tvix/nix-compat): move build_store_path out of derivation r/6060
This doesn't have anything to do with ATerms, we just happen to be using
the aterm representation of a Derivation as contents.

Moving this into store_path/utils.rs makes these things much cleaner -
Have a build_store_path_from_references function, and a
build_store_path_from_fingerprint helper function that makes use of it.

build_store_path_from_references is invoked from the derivation module
which can be used to calculate the derivation path.

In the derivation module, we also invoke
build_store_path_from_fingerprint during the output path calculation.

Change-Id: Ia8d61a5e8e5d3f396f93593676ed3f5d1a3f1d66
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8367
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/derivation/utils.rs')
-rw-r--r--tvix/nix-compat/src/derivation/utils.rs54
1 files changed, 0 insertions, 54 deletions
diff --git a/tvix/nix-compat/src/derivation/utils.rs b/tvix/nix-compat/src/derivation/utils.rs
deleted file mode 100644
index 5c41fa6e55d2..000000000000
--- a/tvix/nix-compat/src/derivation/utils.rs
+++ /dev/null
@@ -1,54 +0,0 @@
-use crate::derivation::DerivationError;
-use crate::nixbase32;
-use crate::store_path::StorePath;
-use crate::texthash::text_hash_string;
-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(
-    fingerprint: &str,
-    name: &str,
-) -> Result<StorePath, DerivationError> {
-    let digest = {
-        let hasher = Sha256::new_with_prefix(fingerprint);
-        hasher.finalize()
-    };
-    let compressed = compress_hash(&digest, 20);
-    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 text_hash_str = text_hash_string(name, content, references);
-    build_store_path(&text_hash_str, name)
-}