Skip to content

Commit 3052f1f

Browse files
committed
Add NarInfo
1 parent 6e4b72f commit 3052f1f

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module System.Nix.Hash (
1313

1414
, HNix.encodeBase32
1515
, HNix.encodeBase16
16+
, HNix.encodeSomeDigest
1617
) where
1718

1819
import qualified System.Nix.Internal.Hash as HNix

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ encodeBase32 (Digest bs) = Base32.encode bs
103103
encodeBase16 :: Digest a -> T.Text
104104
encodeBase16 (Digest bs) = T.decodeUtf8 (Base16.encode bs)
105105

106+
-- | Encode a 'Digest' in algorithm:hash format.
107+
encodeDigest :: forall a. NamedAlgo a => Digest a -> Text
108+
encodeDigest d =
109+
algoName @a <> ":" <> encodeBase16 d
110+
111+
-- | Encode a 'Digest' in algorithm:hash format.
112+
encodeSomeDigest :: SomeNamedDigest -> Text
113+
encodeSomeDigest (SomeDigest d) =
114+
encodeDigest d
115+
106116
-- | Uses "Crypto.Hash.MD5" from cryptohash-md5.
107117
instance ValidAlgo 'MD5 where
108118
type AlgoCtx 'MD5 = MD5.Ctx

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
{-# LANGUAGE TypeApplications #-}
4+
15
{-|
26
Description : Metadata about Nix store paths.
37
-}
48
module System.Nix.StorePathMetadata where
59

6-
import System.Nix.StorePath (StorePath, StorePathSet, ContentAddressableAddress)
7-
import System.Nix.Hash (SomeNamedDigest)
10+
import System.Nix.StorePath (ContentAddressableAddress (..), NarHashMode (..), StorePath, StorePathSet, storePathName, unStorePathName)
11+
import System.Nix.Hash (Digest, NamedAlgo, SomeNamedDigest, algoName, encodeBase16, encodeSomeDigest)
812
import Data.Set (Set)
913
import Data.Time (UTCTime)
14+
import Data.Text as T
1015
import Data.Word (Word64)
1116
import System.Nix.Signature (NarSignature)
1217

@@ -47,3 +52,42 @@ data StorePathTrust
4752
| -- | It was built elsewhere (and substituted or similar) and so
4853
-- is less trusted
4954
BuiltElsewhere
55+
56+
data NarInfo
57+
= NarInfo
58+
{ -- | URL to compressed Nar file.
59+
narUrl :: Text
60+
, -- | Form of compression used on Nar file.
61+
narCompression :: Text
62+
, -- | The hash of the nar serialization of the path.
63+
fileHash :: !SomeNamedDigest
64+
, -- | The size of the nar the path, in bytes.
65+
fileBytes :: !(Maybe Word64)
66+
}
67+
68+
-- | Serialize to .narinfo contents
69+
encodeNarInfo
70+
:: StorePathMetadata storeDir
71+
-> NarInfo
72+
-> Text
73+
encodeNarInfo storePathMetadata narInfo =
74+
T.unlines
75+
[ "StorePath: " <> unStorePathName (storePathName (path storePathMetadata))
76+
, "URL: " <> narUrl narInfo
77+
, "Compression: " <> narCompression narInfo
78+
, "FileHash: " <> encodeSomeDigest (fileHash narInfo)
79+
, "FileSize: " <> foldMap (T.pack . show) (fileBytes narInfo)
80+
, "NarHash: " <> encodeSomeDigest (narHash storePathMetadata)
81+
, "NarSize: " <> foldMap (T.pack . show) (narBytes storePathMetadata)
82+
, "References: "
83+
, "CA: " <> foldMap encodeContentAddressableAddress (contentAddressableAddress storePathMetadata)
84+
]
85+
where
86+
encodeContentAddressableAddress (Text d) =
87+
"text:sha256:" <> encodeBase16 d
88+
encodeContentAddressableAddress (Fixed m d) =
89+
"fixed:" <> encodeNarHashMode m <> ":" <> encodeSomeDigest d
90+
encodeNarHashMode RegularFile =
91+
""
92+
encodeNarHashMode Recursive =
93+
"r:"

0 commit comments

Comments
 (0)