Skip to content

Commit 10f5d3c

Browse files
committed
wip: readonly use dsum instead of NamedAlgo
1 parent 517bbde commit 10f5d3c

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

hnix-store-readonly/hnix-store-readonly.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ library
3434
, hnix-store-core >= 0.8
3535
, hnix-store-nar >= 0.1
3636
, bytestring
37+
, constraints-extras
3738
, crypton
39+
, dependent-sum > 0.7
3840
, mtl
3941
, text
4042
, unordered-containers

hnix-store-readonly/src/System/Nix/Store/ReadOnly.hs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ module System.Nix.Store.ReadOnly
99
) where
1010

1111
import Control.Monad.State (StateT, execStateT, modify)
12-
import Crypto.Hash (Context, Digest, SHA256)
12+
import Crypto.Hash (Context, Digest, SHA256, HashAlgorithm)
1313
import Data.ByteString (ByteString)
14+
import Data.Constraint.Extras (Has(has))
15+
import Data.Dependent.Sum (DSum((:=>)))
1416
import Data.HashSet (HashSet)
15-
import System.Nix.Hash (BaseEncoding(Base16), NamedAlgo(algoName))
17+
import Data.Some (Some(Some))
18+
import System.Nix.Hash (BaseEncoding(Base16), HashAlgo(..))
1619
import System.Nix.Store.Types (FileIngestionMethod(..), PathFilter, RepairMode)
1720
import System.Nix.StorePath (StoreDir, StorePath, StorePathName)
1821

@@ -28,22 +31,20 @@ import qualified System.Nix.Nar
2831
import qualified System.Nix.StorePath
2932

3033
makeStorePath
31-
:: forall hashAlgo
32-
. (NamedAlgo hashAlgo)
33-
=> StoreDir
34+
:: StoreDir
3435
-> ByteString
35-
-> Digest hashAlgo
36+
-> DSum HashAlgo Digest
3637
-> StorePathName
3738
-> StorePath
38-
makeStorePath storeDir ty h nm =
39+
makeStorePath storeDir ty (hashAlgo :=> (digest :: Digest a)) nm =
3940
System.Nix.StorePath.unsafeMakeStorePath storeHash nm
4041
where
41-
storeHash = System.Nix.StorePath.mkStorePathHashPart @hashAlgo s
42+
storeHash = has @HashAlgorithm hashAlgo $ System.Nix.StorePath.mkStorePathHashPart @a s
4243
s =
4344
Data.ByteString.intercalate ":" $
4445
ty:fmap Data.Text.Encoding.encodeUtf8
45-
[ algoName @hashAlgo
46-
, System.Nix.Hash.encodeDigestWith Base16 h
46+
[ System.Nix.Hash.algoToText hashAlgo
47+
, System.Nix.Hash.encodeDigestWith Base16 digest
4748
, Data.Text.pack . Data.ByteString.Char8.unpack $ System.Nix.StorePath.unStoreDir storeDir
4849
, System.Nix.StorePath.unStorePathName nm
4950
]
@@ -54,7 +55,7 @@ makeTextPath
5455
-> Digest SHA256
5556
-> HashSet StorePath
5657
-> StorePath
57-
makeTextPath storeDir nm h refs = makeStorePath storeDir ty h nm
58+
makeTextPath storeDir nm h refs = makeStorePath storeDir ty (HashAlgo_SHA256 :=> h) nm
5859
where
5960
ty =
6061
Data.ByteString.intercalate
@@ -65,25 +66,23 @@ makeTextPath storeDir nm h refs = makeStorePath storeDir ty h nm
6566
<$> Data.HashSet.toList refs)
6667

6768
makeFixedOutputPath
68-
:: forall hashAlgo
69-
. NamedAlgo hashAlgo
70-
=> StoreDir
69+
:: StoreDir
7170
-> FileIngestionMethod
72-
-> Digest hashAlgo
71+
-> DSum HashAlgo Digest
7372
-> StorePathName
7473
-> StorePath
75-
makeFixedOutputPath storeDir recursive h =
74+
makeFixedOutputPath storeDir recursive algoDigest@(hashAlgo :=> digest) =
7675
if recursive == FileIngestionMethod_FileRecursive
77-
&& (algoName @hashAlgo) == "sha256"
78-
then makeStorePath storeDir "source" h
79-
else makeStorePath storeDir "output:out" h'
76+
&& Some hashAlgo == Some HashAlgo_SHA256
77+
then makeStorePath storeDir "source" algoDigest
78+
else makeStorePath storeDir "output:out" (HashAlgo_SHA256 :=> h')
8079
where
8180
h' =
8281
Crypto.Hash.hash @ByteString @SHA256
8382
$ "fixed:out:"
84-
<> Data.Text.Encoding.encodeUtf8 (algoName @hashAlgo)
83+
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.algoToText hashAlgo)
8584
<> (if recursive == FileIngestionMethod_FileRecursive then ":r:" else ":")
86-
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.encodeDigestWith Base16 h)
85+
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.encodeDigestWith Base16 digest)
8786
<> ":"
8887

8988
computeStorePathForText
@@ -108,7 +107,7 @@ computeStorePathForPath storeDir name pth recursive _pathFilter _repair = do
108107
if recursive == FileIngestionMethod_FileRecursive
109108
then recursiveContentHash
110109
else flatContentHash
111-
pure $ makeFixedOutputPath storeDir recursive selectedHash name
110+
pure $ makeFixedOutputPath storeDir recursive (HashAlgo_SHA256 :=> selectedHash) name
112111
where
113112
recursiveContentHash :: IO (Digest SHA256)
114113
recursiveContentHash =

0 commit comments

Comments
 (0)