1+ {-# LANGUAGE GADTs #-}
12{-# LANGUAGE OverloadedStrings #-}
23
34module System.Nix.Store.ReadOnly
@@ -8,11 +9,14 @@ module System.Nix.Store.ReadOnly
89 ) where
910
1011import Control.Monad.State (StateT , execStateT , modify )
11- import Crypto.Hash (Context , Digest , SHA256 )
12+ import Crypto.Hash (Context , Digest , SHA256 , HashAlgorithm )
1213import Data.ByteString (ByteString )
14+ import Data.Constraint.Extras (Has (has ))
15+ import Data.Dependent.Sum (DSum ((:=>) ))
1316import Data.HashSet (HashSet )
17+ import Data.Some (Some (Some ))
1418import System.Nix.ContentAddress (ContentAddressMethod (.. ))
15- import System.Nix.Hash (BaseEncoding (Base16 ), NamedAlgo ( algoName ))
19+ import System.Nix.Hash (BaseEncoding (Base16 ), HashAlgo ( .. ))
1620import System.Nix.Store.Types (PathFilter , RepairMode )
1721import System.Nix.StorePath (StoreDir , StorePath , StorePathName )
1822
@@ -45,22 +49,20 @@ instance Monoid References where
4549 }
4650
4751makeStorePath
48- :: forall hashAlgo
49- . (NamedAlgo hashAlgo )
50- => StoreDir
52+ :: StoreDir
5153 -> ByteString
52- -> Digest hashAlgo
54+ -> DSum HashAlgo Digest
5355 -> StorePathName
5456 -> StorePath
55- makeStorePath storeDir ty h nm =
57+ makeStorePath storeDir ty (hashAlgo :=> (digest :: Digest a )) nm =
5658 System.Nix.StorePath. unsafeMakeStorePath storeHash nm
5759 where
58- storeHash = System.Nix.StorePath. mkStorePathHashPart @ hashAlgo s
60+ storeHash = has @ HashAlgorithm hashAlgo $ System.Nix.StorePath. mkStorePathHashPart @ a s
5961 s =
6062 Data.ByteString. intercalate " :" $
6163 ty: fmap Data.Text.Encoding. encodeUtf8
62- [ algoName @ hashAlgo
63- , System.Nix.Hash. encodeDigestWith Base16 h
64+ [ System.Nix.Hash. algoToText hashAlgo
65+ , System.Nix.Hash. encodeDigestWith Base16 digest
6466 , Data.Text. pack . Data.ByteString.Char8. unpack $ System.Nix.StorePath. unStoreDir storeDir
6567 , System.Nix.StorePath. unStorePathName nm
6668 ]
@@ -80,42 +82,41 @@ makeType storeDir ty refs =
8082 self = [" self" | references_self refs]
8183
8284makeTextPath
83- :: NamedAlgo _SHA256
84- => StoreDir
85- -> Digest _SHA256 -- TODO enforce its it again
85+ :: StoreDir
86+ -> Digest SHA256
8687 -> HashSet StorePath
8788 -> StorePathName
8889 -> StorePath
89- makeTextPath storeDir h refs nm = makeStorePath storeDir ty h nm
90+ makeTextPath storeDir h refs nm = makeStorePath storeDir ty ( HashAlgo_SHA256 :=> h) nm
9091 where
9192 ty = makeType storeDir " text" $ References
9293 { references_others = refs
9394 , references_self = False
9495 }
9596
9697makeFixedOutputPath
97- :: forall hashAlgo
98- . NamedAlgo hashAlgo
99- => StoreDir
98+ :: StoreDir
10099 -> ContentAddressMethod
101- -> Digest hashAlgo
100+ -> DSum HashAlgo Digest
102101 -> References
103102 -> StorePathName
104103 -> StorePath
105- makeFixedOutputPath storeDir method h refs =
104+ makeFixedOutputPath storeDir method digest @ (hashAlgo :=> h) refs =
106105 case method of
107106 ContentAddressMethod_Text ->
108- makeTextPath storeDir h $ references_others refs
107+ case hashAlgo of
108+ HashAlgo_SHA256 -> makeTextPath storeDir h $ references_others refs
109+ _ -> error " unsupported" -- TODO do better; maybe we'll just remove this restriction too?
109110 _ ->
110111 if method == ContentAddressMethod_NixArchive
111- && (algoName @ hashAlgo ) == " sha256 "
112- then makeStorePath storeDir (makeType storeDir " source" refs) h
113- else makeStorePath storeDir " output:out" h'
112+ && Some hashAlgo == Some HashAlgo_SHA256
113+ then makeStorePath storeDir (makeType storeDir " source" refs) digest
114+ else makeStorePath storeDir " output:out" ( HashAlgo_SHA256 :=> h')
114115 where
115116 h' =
116117 Crypto.Hash. hash @ ByteString @ SHA256
117118 $ " fixed:out:"
118- <> Data.Text.Encoding. encodeUtf8 (algoName @ hashAlgo )
119+ <> Data.Text.Encoding. encodeUtf8 (System.Nix.Hash. algoToText hashAlgo)
119120 <> (if method == ContentAddressMethod_NixArchive then " :r:" else " :" )
120121 <> Data.Text.Encoding. encodeUtf8 (System.Nix.Hash. encodeDigestWith Base16 h)
121122 <> " :"
@@ -161,4 +162,4 @@ computeStorePathForPath
161162 -> IO StorePath
162163computeStorePathForPath storeDir name pth method pathFilter repair = do
163164 selectedHash <- digestPath pth method pathFilter repair
164- pure $ makeFixedOutputPath storeDir method selectedHash mempty name
165+ pure $ makeFixedOutputPath storeDir method ( HashAlgo_SHA256 :=> selectedHash) mempty name
0 commit comments