core: don't (re)export StorePathHashPart constructor

This commit is contained in:
Richard Marko 2023-11-14 12:35:38 +01:00
parent c095d12427
commit 3b3752d758
7 changed files with 29 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -7,8 +7,9 @@ module System.Nix.StorePath
, StorePath(..)
, StorePathName
, StorePathSet
, StorePathHashPart
, mkStorePathHashPart
, StorePathHashPart(..)
, unStorePathHashPart
, ContentAddressableAddress(..)
, NarHashMode(..)
, -- * Manipulating 'StorePathName'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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