diff --git a/hnix-store-core/src/System/Nix/Internal/StorePath.hs b/hnix-store-core/src/System/Nix/Internal/StorePath.hs index e307098..8d6d820 100644 --- a/hnix-store-core/src/System/Nix/Internal/StorePath.hs +++ b/hnix-store-core/src/System/Nix/Internal/StorePath.hs @@ -14,8 +14,8 @@ module System.Nix.Internal.StorePath , StorePath(..) , StorePathName(..) , StorePathSet - , mkStorePathHashPart , StorePathHashPart(..) + , mkStorePathHashPart , ContentAddressableAddress(..) , NarHashMode(..) , -- * Manipulating 'StorePathName' @@ -47,6 +47,7 @@ import qualified Data.Attoparsec.Text.Lazy as Parser.Text.Lazy import qualified System.FilePath as FilePath import Crypto.Hash ( SHA256 , Digest + , HashAlgorithm ) -- | A path in a Nix store. @@ -83,11 +84,18 @@ newtype StorePathName = StorePathName } deriving (Eq, Hashable, Ord, Show) -- | The hash algorithm used for store path hashes. -newtype StorePathHashPart = StorePathHashPart ByteString +newtype StorePathHashPart = StorePathHashPart + { -- | Extract the contents of the hash. + unStorePathHashPart :: ByteString + } deriving (Eq, Hashable, Ord, Show) -mkStorePathHashPart :: ByteString -> StorePathHashPart -mkStorePathHashPart = coerce . mkStorePathHash @SHA256 +mkStorePathHashPart + :: forall hashAlgo + . HashAlgorithm hashAlgo + => ByteString + -> StorePathHashPart +mkStorePathHashPart = coerce . mkStorePathHash @hashAlgo -- | A set of 'StorePath's. type StorePathSet = HashSet StorePath diff --git a/hnix-store-core/src/System/Nix/ReadonlyStore.hs b/hnix-store-core/src/System/Nix/ReadonlyStore.hs index acca377..e95a19d 100644 --- a/hnix-store-core/src/System/Nix/ReadonlyStore.hs +++ b/hnix-store-core/src/System/Nix/ReadonlyStore.hs @@ -22,20 +22,20 @@ import Crypto.Hash ( Context makeStorePath - :: forall h - . (NamedAlgo h) + :: forall hashAlgo + . (NamedAlgo hashAlgo) => StoreDir -> ByteString - -> Digest h + -> Digest hashAlgo -> StorePathName -> StorePath -makeStorePath storeDir ty h nm = StorePath (coerce storeHash) nm +makeStorePath storeDir ty h nm = StorePath storeHash nm where - storeHash = mkStorePathHash @h s + storeHash = mkStorePathHashPart @hashAlgo s s = BS.intercalate ":" $ ty:fmap encodeUtf8 - [ algoName @h + [ algoName @hashAlgo , encodeDigestWith Base16 h , toText . Bytes.Char8.unpack $ unStoreDir storeDir , unStorePathName nm diff --git a/hnix-store-core/src/System/Nix/StorePath.hs b/hnix-store-core/src/System/Nix/StorePath.hs index 2932cb8..b414d10 100644 --- a/hnix-store-core/src/System/Nix/StorePath.hs +++ b/hnix-store-core/src/System/Nix/StorePath.hs @@ -7,8 +7,9 @@ module System.Nix.StorePath , StorePath(..) , StorePathName , StorePathSet + , StorePathHashPart , mkStorePathHashPart - , StorePathHashPart(..) + , unStorePathHashPart , ContentAddressableAddress(..) , NarHashMode(..) , -- * Manipulating 'StorePathName' diff --git a/hnix-store-core/tests/Arbitrary.hs b/hnix-store-core/tests/Arbitrary.hs index 3a12f44..dcf40aa 100644 --- a/hnix-store-core/tests/Arbitrary.hs +++ b/hnix-store-core/tests/Arbitrary.hs @@ -30,7 +30,7 @@ instance Arbitrary StorePathName where sn = elements $ alphanum <> "+-._?=" instance Arbitrary StorePathHashPart where - arbitrary = mkStorePathHashPart . BSC.pack <$> arbitrary + arbitrary = mkStorePathHashPart @SHA256 . BSC.pack <$> arbitrary instance Arbitrary (Digest SHA256) where arbitrary = hash . BSC.pack <$> arbitrary diff --git a/hnix-store-core/tests/Hash.hs b/hnix-store-core/tests/Hash.hs index 690f3d5..9a78342 100644 --- a/hnix-store-core/tests/Hash.hs +++ b/hnix-store-core/tests/Hash.hs @@ -37,7 +37,7 @@ spec_hash = do -- The example in question: -- https://nixos.org/nixos/nix-pills/nix-store-paths.html it "produces same base32 as nix pill flat file example" $ do - shouldBe (encodeWith NixBase32 $ coerce $ mkStorePathHashPart "source:sha256:2bfef67de873c54551d884fdab3055d84d573e654efa79db3c0d7b98883f9ee3:/nix/store:myfile") + shouldBe (encodeWith NixBase32 $ unStorePathHashPart $ mkStorePathHashPart @SHA256 "source:sha256:2bfef67de873c54551d884fdab3055d84d573e654efa79db3c0d7b98883f9ee3:/nix/store:myfile") "xv2iccirbrvklck36f1g7vldn5v58vck" where cmp :: String -> BaseEncoding -> (ByteString -> Digest a) -> ByteString -> Text -> SpecWith () @@ -52,7 +52,8 @@ prop_nixBase32Roundtrip = forAllShrink nonEmptyString genericShrink $ -- | API variants prop_nixBase16Roundtrip :: StorePathHashPart -> Property -prop_nixBase16Roundtrip x = pure (coerce x) === decodeWith Base16 (encodeWith Base16 $ coerce x) +prop_nixBase16Roundtrip x = + pure (unStorePathHashPart x) === decodeWith Base16 (encodeWith Base16 $ unStorePathHashPart x) -- | Hash encoding conversion ground-truth. -- Similiar to nix/tests/hash.sh diff --git a/hnix-store-remote/src/System/Nix/Store/Remote.hs b/hnix-store-remote/src/System/Nix/Store/Remote.hs index 0cdcbdb..a2f18c5 100644 --- a/hnix-store-remote/src/System/Nix/Store/Remote.hs +++ b/hnix-store-remote/src/System/Nix/Store/Remote.hs @@ -290,7 +290,10 @@ queryPathFromHashPart :: StorePathHashPart -> MonadStore StorePath queryPathFromHashPart storePathHash = do runOpArgs QueryPathFromHashPart $ putByteStringLen - $ encodeUtf8 (encodeWith NixBase32 $ coerce storePathHash) + $ encodeUtf8 + $ encodeWith NixBase32 + $ System.Nix.StorePath.unStorePathHashPart + storePathHash sockGetPath queryMissing diff --git a/hnix-store-remote/tests/NixDaemon.hs b/hnix-store-remote/tests/NixDaemon.hs index fae3579..60781c4 100644 --- a/hnix-store-remote/tests/NixDaemon.hs +++ b/hnix-store-remote/tests/NixDaemon.hs @@ -166,7 +166,7 @@ dummy = do invalidPath :: StorePath invalidPath = let name = Data.Either.fromRight (error "impossible") $ makeStorePathName "invalid" - in StorePath (mkStorePathHashPart "invalid") name + in StorePath (mkStorePathHashPart @SHA256 "invalid") name withBuilder :: (StorePath -> MonadStore a) -> MonadStore a withBuilder action = do