Skip to content

Commit a7120f6

Browse files
committed
Reflect type level hash algo to value
1 parent 7bd991a commit a7120f6

File tree

5 files changed

+76
-14
lines changed

5 files changed

+76
-14
lines changed

hnix-store-core/src/System/Nix/Hash.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Maintainer : Shea Levy <[email protected]>; Greg Hale <[email protected]>
1313
module System.Nix.Hash (
1414
HNix.Digest
1515

16-
, HNix.HashAlgorithm(..)
16+
, HNix.HashAlgorithm
17+
, HNix.HashAlgorithm'(..)
18+
, HNix.AlgoVal(..)
1719
, HNix.HasDigest(..)
1820
, HNix.hash
1921
, HNix.hashLazy

hnix-store-core/src/System/Nix/Internal/Hash.hs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Maintainer : Greg Hale <[email protected]>
1111
{-# LANGUAGE KindSignatures #-}
1212
{-# LANGUAGE ScopedTypeVariables #-}
1313
{-# LANGUAGE TypeApplications #-}
14+
{-# LANGUAGE TypeInType #-}
1415

1516
module System.Nix.Internal.Hash where
1617

@@ -23,6 +24,7 @@ import Data.Bits (xor)
2324
import qualified Data.ByteString as BS
2425
import qualified Data.ByteString.Lazy as BSL
2526
import qualified Data.Hashable as DataHashable
27+
import Data.Kind (Type)
2628
import Data.List (foldl')
2729
import Data.Proxy (Proxy(Proxy))
2830
import qualified Data.Text as T
@@ -34,12 +36,31 @@ import GHC.TypeLits
3436
-- | A tag for different hashing algorithms
3537
-- Also used as a type-level tag for hash digests
3638
-- (e.g. @Digest SHA256@ is the type for a sha256 hash)
37-
data HashAlgorithm
39+
--
40+
-- When used at the type level, `n` is `Nat`
41+
data HashAlgorithm' n
3842
= MD5
3943
| SHA1
4044
| SHA256
41-
| Truncated Nat HashAlgorithm
45+
| Truncated n (HashAlgorithm' n)
46+
deriving (Eq, Show)
47+
48+
type HashAlgorithm = HashAlgorithm' Nat
49+
50+
class AlgoVal (a :: HashAlgorithm) where
51+
algoVal :: HashAlgorithm' Integer
52+
53+
instance AlgoVal MD5 where
54+
algoVal = MD5
55+
56+
instance AlgoVal SHA1 where
57+
algoVal = SHA1
58+
59+
instance AlgoVal SHA256 where
60+
algoVal = SHA256
4261

62+
instance forall a n.(AlgoVal a, KnownNat n) => AlgoVal (Truncated n a) where
63+
algoVal = Truncated (natVal (Proxy @n)) (algoVal @a)
4364

4465
-- | Types with kind @HashAlgorithm@ may be a @HasDigest@ instance
4566
-- if they are able to hash bytestrings via the init/update/finalize
@@ -49,7 +70,7 @@ data HashAlgorithm
4970
-- monomorphic hashing libraries, such as `cryptohash-sha256`.
5071
class HasDigest (a :: HashAlgorithm) where
5172

52-
type AlgoCtx a :: *
73+
type AlgoCtx a :: Type
5374

5475
initialize :: AlgoCtx a
5576
update :: AlgoCtx a -> BS.ByteString -> AlgoCtx a

hnix-store-core/src/System/Nix/Path.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module System.Nix.Path
1818
) where
1919

2020
import System.Nix.Hash (Digest(..),
21-
HashAlgorithm(Truncated, SHA256))
21+
HashAlgorithm'(Truncated, SHA256))
2222
import qualified Data.ByteString as BS
2323
import qualified Data.ByteString.Char8 as BSC
2424
import Data.Hashable (Hashable (..), hashPtrWithSalt)

hnix-store-remote/app/Main.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{-# LANGUAGE OverloadedStrings #-}
22
import qualified Data.ByteString.Lazy as LBS
3-
import qualified Data.HashSet as HS
4-
import qualified System.Nix.GC as GC
5-
import System.Nix.Store.Remote
6-
import System.Nix.Store.Remote.Util
7-
import Data.Maybe
8-
import Control.Monad.Reader
3+
import qualified Data.HashSet as HS
4+
import Data.Maybe
5+
import Control.Monad.Reader
6+
import Text.Pretty.Simple
97

10-
import Text.Pretty.Simple
8+
import qualified System.Nix.GC as GC
9+
import System.Nix.Store.Remote
10+
import System.Nix.Store.Remote.Util
1111

1212
noSuchPath = fromJust $ mkPath "blah"
1313

hnix-store-remote/src/System/Nix/Store/Remote.hs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE KindSignatures #-}
14
{-# LANGUAGE OverloadedStrings #-}
5+
{-# LANGUAGE RankNTypes #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE TypeApplications #-}
28
module System.Nix.Store.Remote (
39
runStore
410
, isValidPathUncached
@@ -32,6 +38,7 @@ module System.Nix.Store.Remote (
3238
import Data.Maybe
3339
import qualified Data.ByteString.Lazy as LBS
3440
import qualified Data.Map.Strict as M
41+
import Data.Proxy (Proxy(Proxy))
3542

3643
import Control.Monad
3744

@@ -40,6 +47,7 @@ import qualified System.Nix.Derivation as Drv
4047
import qualified System.Nix.GC as GC
4148
import System.Nix.Hash (Digest, HashAlgorithm)
4249
import System.Nix.Path
50+
import System.Nix.Hash
4351
import System.Nix.Util
4452

4553
import System.Nix.Store.Remote.Types
@@ -159,9 +167,40 @@ type Source = () -- abstract binary source
159167
addToStoreNar :: ValidPathInfo -> Source -> RepairFlag -> CheckSigsFlag -> MonadStore ()
160168
addToStoreNar = undefined -- XXX
161169

170+
171+
-- class BaseHashAlgorithm (a :: HashAlgorithm) where
172+
-- baseHashAlgorithm :: Bool
173+
174+
-- instance BaseHashAlgorithm MD5 where
175+
-- baseHashAlgorithm = MD5
176+
177+
-- instance BaseHashAlgorithm SHA1 where
178+
-- baseHashAlgorithm = SHA1
179+
180+
-- instance BaseHashAlgorithm SHA256 where
181+
-- baseHashAlgorithm = SHA256
182+
183+
-- instance forall n a.BaseHashAlgorithm a => BaseHashAlgorithm (Truncated n a) where
184+
-- baseHashAlgorithm = baseHashAlgorithm @a
185+
162186
type PathFilter = Path -> Bool
163-
addToStore :: LBS.ByteString -> Path -> Bool -> HashAlgorithm -> PathFilter -> RepairFlag -> MonadStore Path
164-
addToStore name pth recursive hashAlgo pfilter repair = undefined -- XXX
187+
addToStore
188+
:: forall a. AlgoVal a
189+
=> LBS.ByteString
190+
-> Path
191+
-> Bool
192+
-> Proxy a
193+
-> PathFilter
194+
-> RepairFlag
195+
-> MonadStore Path
196+
addToStore name pth recursive algoProxy pfilter repair = do
197+
runOpArgs AddToStore $ do
198+
putByteStringLen name
199+
putByteStringLen $ if algoVal @a == SHA256 && recursive then 0 else 1
200+
putByteStringLen $ if recursive then 0 else 1
201+
putByteStringLen name
202+
fmap (fromMaybe "TODO: Error") sockGetPath
203+
165204

166205
addTextToStore :: LBS.ByteString -> LBS.ByteString -> PathSet -> RepairFlag -> MonadStore (Maybe Path)
167206
addTextToStore name text references' repair = do

0 commit comments

Comments
 (0)