Skip to content

Commit 30f090f

Browse files
committed
Support more types of store paths
1 parent 43fc6d4 commit 30f090f

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

hnix-store-readonly/src/System/Nix/Store/ReadOnly.hs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

33
module System.Nix.Store.ReadOnly
4-
( makeStorePath
4+
( References(..)
5+
, makeStorePath
56
, makeFixedOutputPath
67
, computeStorePathForPath
78
) where
@@ -26,6 +27,23 @@ import qualified System.Nix.Hash
2627
import qualified System.Nix.Nar
2728
import qualified System.Nix.StorePath
2829

30+
data References = References
31+
{ references_others :: HashSet StorePath
32+
, references_self :: Bool
33+
}
34+
35+
instance Semigroup References where
36+
a <> b = References
37+
{ references_others = references_others a <> references_others b
38+
, references_self = references_self a || references_self b
39+
}
40+
41+
instance Monoid References where
42+
mempty = References
43+
{ references_others = mempty
44+
, references_self = False
45+
}
46+
2947
makeStorePath
3048
:: forall hashAlgo
3149
. (NamedAlgo hashAlgo)
@@ -47,6 +65,20 @@ makeStorePath storeDir ty h nm =
4765
, System.Nix.StorePath.unStorePathName nm
4866
]
4967

68+
makeType
69+
:: StoreDir
70+
-> ByteString
71+
-> References
72+
-> ByteString
73+
makeType storeDir ty refs =
74+
Data.ByteString.intercalate ":" $ ty : (others ++ self)
75+
where
76+
others = Data.List.sort
77+
$ fmap (System.Nix.StorePath.storePathToRawFilePath storeDir)
78+
$ Data.HashSet.toList
79+
$ references_others refs
80+
self = ["self" | references_self refs]
81+
5082
makeTextPath
5183
:: NamedAlgo _SHA256
5284
=> StoreDir
@@ -56,29 +88,28 @@ makeTextPath
5688
-> StorePath
5789
makeTextPath storeDir h refs nm = makeStorePath storeDir ty h nm
5890
where
59-
ty =
60-
Data.ByteString.intercalate ":"
61-
$ "text"
62-
: Data.List.sort
63-
(System.Nix.StorePath.storePathToRawFilePath storeDir
64-
<$> Data.HashSet.toList refs)
91+
ty = makeType storeDir "text" $ References
92+
{ references_others = refs
93+
, references_self = False
94+
}
6595

6696
makeFixedOutputPath
6797
:: forall hashAlgo
6898
. NamedAlgo hashAlgo
6999
=> StoreDir
70100
-> ContentAddressMethod
71101
-> Digest hashAlgo
72-
-> HashSet StorePath
102+
-> References
73103
-> StorePathName
74104
-> StorePath
75105
makeFixedOutputPath storeDir method h refs =
76106
case method of
77-
ContentAddressMethod_Text -> makeTextPath storeDir h refs
107+
ContentAddressMethod_Text ->
108+
makeTextPath storeDir h $ references_others refs
78109
_ ->
79110
if method == ContentAddressMethod_NixArchive
80111
&& (algoName @hashAlgo) == "sha256"
81-
then makeStorePath storeDir "source" h
112+
then makeStorePath storeDir (makeType storeDir "source" refs) h
82113
else makeStorePath storeDir "output:out" h'
83114
where
84115
h' =

hnix-store-readonly/tests/ReadOnlySpec.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ spec = do
7474
def
7575
ContentAddressMethod_Text
7676
testDigest
77-
(Data.HashSet.fromList [ testPath, testPath2 ])
77+
(References
78+
{ references_others = Data.HashSet.fromList [ testPath, testPath2 ]
79+
, references_self = False
80+
})
7881
testName
7982
)
8083
`shouldBe`
@@ -117,7 +120,10 @@ spec = do
117120
def
118121
ContentAddressMethod_Text
119122
(Crypto.Hash.hash ("test" :: ByteString) :: Digest SHA256)
120-
(Data.HashSet.fromList [ testPath ])
123+
(References
124+
{ references_others = Data.HashSet.fromList [ testPath ]
125+
, references_self = False
126+
})
121127
testName
122128
)
123129
`shouldBe`

0 commit comments

Comments
 (0)