about summary refs log tree commit diff
path: root/tvix/nix-compat/src/store_path
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-12-21T15·05+0200
committerflokli <flokli@flokli.de>2023-12-21T16·36+0000
commit329a7d30a7e11fabe46ab22cdb96ba800f75d49a (patch)
treeef72bafb5a3ed3769308ca4e63835808cac17fd9 /tvix/nix-compat/src/store_path
parent88adaea12babf9b32578e86a75b5f2eef79ba4a3 (diff)
refactor(nix-compat/store_path): centralize self_reference check r/7238
self_reference being set to true is only allowed for
`CAHash::Nar(NixHash::Sha256(_))`, so we can handle this in a check at
the front.

Change-Id: Ic363ade4789a7767cbe26a6959b143bb53e50e5a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10391
Reviewed-by: edef <edef@edef.eu>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/store_path')
-rw-r--r--tvix/nix-compat/src/store_path/utils.rs28
1 files changed, 12 insertions, 16 deletions
diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs
index 2e10c33dcdaa..af593d27affd 100644
--- a/tvix/nix-compat/src/store_path/utils.rs
+++ b/tvix/nix-compat/src/store_path/utils.rs
@@ -61,29 +61,28 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
     references: I,
     self_reference: bool,
 ) -> Result<StorePathRef<'a>, BuildStorePathError> {
-    let (ty, hash) = match &ca_hash {
-        CAHash::Text(ref digest) => {
-            if self_reference {
-                return Err(BuildStorePathError::InvalidReference());
-            }
+    // self references are only allowed for CAHash::Nar(NixHash::Sha256(_)).
+    if self_reference {
+        let CAHash::Nar(NixHash::Sha256(_)) = ca_hash else {
+            return Err(BuildStorePathError::InvalidReference());
+        };
+    }
 
-            (
-                make_references_string("text", references, false),
-                NixHash::Sha256(*digest),
-            )
-        }
+    let (ty, hash) = match &ca_hash {
+        CAHash::Text(ref digest) => (
+            make_references_string("text", references, false),
+            NixHash::Sha256(*digest),
+        ),
         CAHash::Nar(NixHash::Sha256(ref digest)) => (
             make_references_string("source", references, self_reference),
             NixHash::Sha256(*digest),
         ),
+
         // for all other CAHash::Nar, another custom scheme is used.
         CAHash::Nar(ref hash) => {
             if references.into_iter().next().is_some() {
                 return Err(BuildStorePathError::InvalidReference());
             }
-            if self_reference {
-                return Err(BuildStorePathError::InvalidReference());
-            }
 
             (
                 "output:out".to_string(),
@@ -99,9 +98,6 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
             if references.into_iter().next().is_some() {
                 return Err(BuildStorePathError::InvalidReference());
             }
-            if self_reference {
-                return Err(BuildStorePathError::InvalidReference());
-            }
 
             (
                 "output:out".to_string(),