|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 3 | +{-# LANGUAGE TypeApplications #-} |
| 4 | + |
1 | 5 | {-| |
2 | 6 | Description : Metadata about Nix store paths. |
3 | 7 | -} |
4 | 8 | module System.Nix.StorePathMetadata where |
5 | 9 |
|
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) |
8 | 12 | import Data.Set (Set) |
9 | 13 | import Data.Time (UTCTime) |
| 14 | +import Data.Text as T |
10 | 15 | import Data.Word (Word64) |
11 | 16 | import System.Nix.Signature (NarSignature) |
12 | 17 |
|
@@ -47,3 +52,42 @@ data StorePathTrust |
47 | 52 | | -- | It was built elsewhere (and substituted or similar) and so |
48 | 53 | -- is less trusted |
49 | 54 | 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