Skip to content

Commit 59f1e00

Browse files
committed
Merge branch 'srk/readOnlyDsum' into content-address-method-cleanup
2 parents 30f090f + a41c941 commit 59f1e00

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
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: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE GADTs #-}
12
{-# LANGUAGE OverloadedStrings #-}
23

34
module System.Nix.Store.ReadOnly
@@ -8,11 +9,14 @@ module System.Nix.Store.ReadOnly
89
) where
910

1011
import Control.Monad.State (StateT, execStateT, modify)
11-
import Crypto.Hash (Context, Digest, SHA256)
12+
import Crypto.Hash (Context, Digest, SHA256, HashAlgorithm)
1213
import Data.ByteString (ByteString)
14+
import Data.Constraint.Extras (Has(has))
15+
import Data.Dependent.Sum (DSum((:=>)))
1316
import Data.HashSet (HashSet)
17+
import Data.Some (Some(Some))
1418
import System.Nix.ContentAddress (ContentAddressMethod (..))
15-
import System.Nix.Hash (BaseEncoding(Base16), NamedAlgo(algoName))
19+
import System.Nix.Hash (BaseEncoding(Base16), HashAlgo(..))
1620
import System.Nix.Store.Types (PathFilter, RepairMode)
1721
import System.Nix.StorePath (StoreDir, StorePath, StorePathName)
1822

@@ -45,22 +49,20 @@ instance Monoid References where
4549
}
4650

4751
makeStorePath
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

8284
makeTextPath
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

9697
makeFixedOutputPath
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
162163
computeStorePathForPath 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

hnix-store-readonly/tests/ReadOnlySpec.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ module ReadOnlySpec where
55
import Data.Default.Class (Default(def))
66
import Test.Hspec (Spec, describe, it, shouldBe, pendingWith)
77

8-
import Crypto.Hash (hash, Digest, SHA256(..))
8+
import Crypto.Hash (hash, Digest)
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.ContentAddress (ContentAddressMethod(..))
1214

@@ -15,8 +17,8 @@ import qualified System.Nix.StorePath
1517

1618
import System.Nix.Store.ReadOnly
1719

18-
testDigest :: Digest SHA256
19-
testDigest = Crypto.Hash.hash @ByteString "testDigest"
20+
testDigest :: DSum HashAlgo Digest
21+
testDigest = HashAlgo_SHA256 :=> Crypto.Hash.hash @ByteString "testDigest"
2022

2123
testName :: StorePathName
2224
testName =
@@ -119,7 +121,7 @@ spec = do
119121
$ makeFixedOutputPath
120122
def
121123
ContentAddressMethod_Text
122-
(Crypto.Hash.hash ("test" :: ByteString) :: Digest SHA256)
124+
(HashAlgo_SHA256 :=> Crypto.Hash.hash ("test" :: ByteString))
123125
(References
124126
{ references_others = Data.HashSet.fromList [ testPath ]
125127
, references_self = False

0 commit comments

Comments
 (0)