Skip to content

Commit 9b24233

Browse files
authored
Merge pull request #291 from haskell-nix/srk/readOnlyDsum
Readonly use `DSum` instead of `NamedAlgo`
2 parents 517bbde + a41c941 commit 9b24233

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

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

Lines changed: 3 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
@@ -57,5 +59,6 @@ test-suite readonly
5759
, bytestring
5860
, crypton
5961
, data-default-class
62+
, dependent-sum
6063
, hspec
6164
, 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 =

hnix-store-readonly/tests/ReadOnlySpec.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Test.Hspec (Spec, describe, it, shouldBe, pendingWith)
77

88
import Crypto.Hash (hash, Digest, SHA256(..))
99
import Data.ByteString (ByteString)
10+
import Data.Dependent.Sum (DSum(..))
11+
import System.Nix.Hash (HashAlgo(..))
1012
import System.Nix.StorePath (StorePath, StorePathName)
1113
import System.Nix.Store.Types (FileIngestionMethod(..))
1214

@@ -18,6 +20,9 @@ import System.Nix.Store.ReadOnly
1820
testDigest :: Digest SHA256
1921
testDigest = Crypto.Hash.hash @ByteString "testDigest"
2022

23+
testDigest' :: DSum HashAlgo Digest
24+
testDigest' = HashAlgo_SHA256 :=> testDigest
25+
2126
testName :: StorePathName
2227
testName =
2328
either undefined id
@@ -45,7 +50,7 @@ spec = do
4550
$ makeStorePath
4651
def
4752
"test"
48-
testDigest
53+
testDigest'
4954
testName
5055
)
5156
`shouldBe`
@@ -86,7 +91,7 @@ spec = do
8691
$ makeFixedOutputPath
8792
def
8893
FileIngestionMethod_FileRecursive
89-
testDigest
94+
testDigest'
9095
testName
9196
)
9297
`shouldBe`
@@ -99,7 +104,7 @@ spec = do
99104
$ makeFixedOutputPath
100105
def
101106
FileIngestionMethod_Flat
102-
testDigest
107+
testDigest'
103108
testName
104109
)
105110
`shouldBe`

0 commit comments

Comments
 (0)