Skip to content

Commit 679ee20

Browse files
committed
Add NarInfo data structure
1 parent f60ebd5 commit 679ee20

File tree

2 files changed

+52
-30
lines changed
  • hnix-store-core/src/System/Nix
  • hnix-store-s3/src/System/Nix/Store

2 files changed

+52
-30
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module System.Nix.Path
1313
, PathSet
1414
, SubstitutablePathInfo(..)
1515
, ValidPathInfo(..)
16+
, NarInfo(..)
17+
, narInfoToString
1618
, PathName(..)
1719
, filePathPart
1820
, pathName
@@ -111,6 +113,38 @@ data ValidPathInfo = ValidPathInfo
111113
ca :: !Text
112114
} deriving (Eq, Ord, Show)
113115

116+
-- | Information for .narinfo files
117+
data NarInfo = NarInfo
118+
{ -- | NarInfo is a ValidPathInfo
119+
pathInfoNI :: !ValidPathInfo
120+
, -- | URL
121+
urlNI :: !Text
122+
, -- | File hash
123+
fileHash :: !Text
124+
, -- | File size
125+
fileSizeNI :: !Integer
126+
, -- | Compression format
127+
compression :: !Text
128+
}
129+
130+
-- | Serialize to .narinfo contents
131+
narInfoToString
132+
:: Text
133+
-> NarInfo
134+
-> Text
135+
narInfoToString storeDir narInfo =
136+
T.unlines
137+
[ "StorePath: " <> pathToText storeDir (path $ pathInfoNI narInfo)
138+
, "URL: " <> urlNI narInfo
139+
, "Compression: " <> compression narInfo
140+
, "FileHash: " <> fileHash narInfo
141+
, "FileSize: " <> T.pack (show $ fileSizeNI narInfo)
142+
, "NarHash: " <> narHash (pathInfoNI narInfo)
143+
, "NarSize: " <> T.pack (show . narSizeVP $ pathInfoNI narInfo)
144+
, "References: "
145+
, "CA: " <> ca (pathInfoNI narInfo)
146+
]
147+
114148
-- | A valid filename or directory name
115149
newtype FilePathPart = FilePathPart { unFilePathPart :: BSC.ByteString }
116150
deriving (Eq, Ord, Show)

hnix-store-s3/src/System/Nix/Store/S3.hs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,6 @@ upsertFile
4040
upsertFile bucketName pth d mimeType =
4141
send (putObject bucketName (ObjectKey pth) (toBody d) & poContentType ?~ mimeType)
4242

43-
data NarInfo
44-
= NarInfo Path BS.ByteString BS.ByteString (Digest 'SHA256) Int (Digest 'SHA256) Int Text
45-
46-
narInfoToString
47-
:: Text
48-
-> NarInfo
49-
-> BS.ByteString
50-
narInfoToString storeDir (NarInfo pth url compression fileHash fileSize narHash' narSize' ca') =
51-
BS.unlines
52-
[ "StorePath: " <> E.encodeUtf8 (pathToText storeDir pth)
53-
, "URL: " <> url
54-
, "Compression: " <> compression
55-
, "FileHash: " <> E.encodeUtf8 (digestText32 fileHash)
56-
, "FileSize: " <> fromString (show fileSize)
57-
, "NarHash: " <> E.encodeUtf8 (digestText32 narHash')
58-
, "NarSize: " <> fromString (show narSize')
59-
, "References: "
60-
, "CA: " <> E.encodeUtf8 ca'
61-
]
62-
6343
narInfoFileFor
6444
:: Path
6545
-> Text
@@ -97,20 +77,28 @@ addToStore storeDir bucketName name pth recursive pfilter repair = do
9777
let
9878
pth' = makeFixedOutputPath storeDir recursive h . E.decodeUtf8 $ LBS.toStrict name
9979
narCompressed = nar & lazy %~ compress
100-
url = "nar/" <> printAsBase32 fileHash <> ".nar.xz"
101-
fileHash = hash @'SHA256 narCompressed
80+
fileHash' = hash @'SHA256 narCompressed
81+
url = "nar/" <> printAsBase32 fileHash' <> ".nar.xz"
82+
validPathInfo =
83+
ValidPathInfo
84+
pth'
85+
Nothing
86+
(digestText32 $ hash @'SHA256 nar)
87+
mempty
88+
0
89+
(fromIntegral $ BS.length nar)
90+
False
91+
[]
92+
(makeFixedOutputCA recursive h)
10293
narInfo =
10394
NarInfo
104-
pth'
105-
(E.encodeUtf8 url)
95+
validPathInfo
96+
url
97+
(digestText32 fileHash')
98+
(fromIntegral $ BS.length narCompressed)
10699
"xz"
107-
fileHash
108-
(BS.length narCompressed)
109-
(hash @'SHA256 nar)
110-
(BS.length nar)
111-
(makeFixedOutputCA recursive h)
112100

113101
void . lift $ upsertFile bucketName url narCompressed "application/x-nix-nar"
114-
void . lift $ upsertFile bucketName (narInfoFileFor pth') (narInfoToString storeDir narInfo) "text/x-nix-narinfo"
102+
void . lift $ upsertFile bucketName (narInfoFileFor pth') (E.encodeUtf8 $ narInfoToString storeDir narInfo) "text/x-nix-narinfo"
115103

116104
pure pth'

0 commit comments

Comments
 (0)