77{-# LANGUAGE TypeApplications #-}
88module System.Nix.Store.Remote (
99 runStore
10- , isValidPathUncached
11- , queryValidPaths
12- , queryAllValidPaths
13- , querySubstitutablePaths
14- , querySubstitutablePathInfos
15- , queryPathInfoUncached
16- , queryReferrers
17- , queryValidDerivers
18- , queryDerivationOutputs
19- , queryDerivationOutputNames
20- , queryPathFromHashPart
21- , addToStore
22- , addTextToStore
23- , buildPaths
24- , ensurePath
25- , addTempRoot
26- , addIndirectRoot
2710 , syncWithGC
28- , findRoots
29- , collectGarbage
3011 , optimiseStore
3112 , verifyStore
32- , addSignatures
3313 ) where
3414
3515import Control.Monad
36- import Control.Monad.IO.Class (liftIO )
37- import qualified Data.Binary.Put as B
38- import Data.Maybe
39- import qualified Data.ByteString.Lazy as LBS
40- import qualified Data.Map.Strict as M
41- import Data.Proxy (Proxy )
42- import qualified Data.Text.Lazy as T
43- import qualified Data.Text.Lazy.Encoding as T
44-
45- import qualified System.Nix.Build as Build
46- import qualified System.Nix.GC as GC
47- import System.Nix.Path
48- import System.Nix.Hash
49- import System.Nix.Nar (localPackNar , putNar , narEffectsIO )
50- import System.Nix.Util
5116
5217import System.Nix.Store.Remote.Types
5318import System.Nix.Store.Remote.Protocol
5419import System.Nix.Store.Remote.Util
5520
5621type RepairFlag = Bool
5722type CheckFlag = Bool
58- type SubstituteFlag = Bool
59-
60- -- setOptions :: StoreSetting -> MonadStore ()
61-
62- isValidPathUncached :: Path -> MonadStore Bool
63- isValidPathUncached p = simpleOpArgs IsValidPath $ putPath p
64-
65- queryValidPaths :: PathSet -> SubstituteFlag -> MonadStore PathSet
66- queryValidPaths ps substitute = do
67- runOpArgs QueryValidPaths $ do
68- putPaths ps
69- putBool substitute
70- sockGetPaths
71-
72- queryAllValidPaths :: MonadStore PathSet
73- queryAllValidPaths = do
74- runOp QueryAllValidPaths
75- sockGetPaths
76-
77- querySubstitutablePaths :: PathSet -> MonadStore PathSet
78- querySubstitutablePaths ps = do
79- runOpArgs QuerySubstitutablePaths $ do
80- putPaths ps
81- sockGetPaths
82-
83- querySubstitutablePathInfos :: PathSet -> MonadStore [SubstitutablePathInfo ]
84- querySubstitutablePathInfos ps = do
85- runOpArgs QuerySubstitutablePathInfos $ do
86- putPaths ps
87-
88- cnt <- sockGetInt
89- forM (take cnt $ cycle [(0 :: Int )]) $ pure $ do
90- _pth <- sockGetPath
91- drv <- sockGetStr
92- refs <- sockGetPaths
93- dlSize <- sockGetInt
94- narSize' <- sockGetInt
95- return $ SubstitutablePathInfo {
96- deriver = mkPath drv
97- , references = refs
98- , downloadSize = dlSize
99- , narSize = narSize'
100- }
101-
102- queryPathInfoUncached :: Path -> MonadStore ValidPathInfo
103- queryPathInfoUncached p = do
104- runOpArgs QueryPathInfo $ do
105- putPath p
106-
107- valid <- sockGetBool
108- unless valid $ error " Path is not valid"
109-
110- drv <- sockGetStr
111- hash' <- lBSToText <$> sockGetStr
112- refs <- sockGetPaths
113- regTime <- sockGetInt
114- size <- sockGetInt
115- ulti <- sockGetBool
116- sigs' <- map lBSToText <$> sockGetStrings
117- ca' <- lBSToText <$> sockGetStr
118- return $ ValidPathInfo {
119- path = p
120- , deriverVP = mkPath drv
121- , narHash = hash'
122- , referencesVP = refs
123- , registrationTime = regTime
124- , narSizeVP = size
125- , ultimate = ulti
126- , sigs = sigs'
127- , ca = ca'
128- }
129-
130- queryReferrers :: Path -> MonadStore PathSet
131- queryReferrers p = do
132- runOpArgs QueryReferrers $ do
133- putPath p
134- sockGetPaths
135-
136- queryValidDerivers :: Path -> MonadStore PathSet
137- queryValidDerivers p = do
138- runOpArgs QueryValidDerivers $ do
139- putPath p
140- sockGetPaths
141-
142- queryDerivationOutputs :: Path -> MonadStore PathSet
143- queryDerivationOutputs p = do
144- runOpArgs QueryDerivationOutputs $
145- putPath p
146- sockGetPaths
147-
148- queryDerivationOutputNames :: Path -> MonadStore PathSet
149- queryDerivationOutputNames p = do
150- runOpArgs QueryDerivationOutputNames $
151- putPath p
152- sockGetPaths
153-
154- -- XXX: this is broken as I don't know how to get hashes from paths (fix mkPath)
155- queryPathFromHashPart :: Digest PathHashAlgo -> MonadStore (Maybe Path )
156- queryPathFromHashPart d = do
157- runOpArgs QueryPathFromHashPart $
158- -- TODO: replace `undefined` with digest encoding function when
159- -- [issue 24](https://github.com/haskell-nix/hnix-store/issues/24) is
160- -- closed
161- putByteStringLen $ LBS. fromStrict $ undefined d
162- sockGetPath
163-
164- type PathFilter = Path -> Bool
165-
166- addToStore
167- :: forall a . (ValidAlgo a , NamedAlgo a )
168- => LBS. ByteString
169- -> FilePath
170- -> Bool
171- -> Proxy a
172- -> PathFilter
173- -> RepairFlag
174- -> MonadStore Path
175- addToStore name pth recursive algoProxy pfilter repair = do
176-
177- -- TODO: Is this lazy enough? We need `B.putLazyByteString bs` to stream `bs`
178- bs :: LBS. ByteString <- liftIO $ B. runPut . putNar <$> localPackNar narEffectsIO pth
179-
180- runOpArgs AddToStore $ do
181- putByteStringLen name
182- putInt 1
183- if recursive
184- then putInt 1
185- else putInt 0
186-
187- putByteStringLen (T. encodeUtf8 . T. toLower . T. fromStrict $ algoName @ a )
188-
189- B. putLazyByteString bs
190-
191- fmap (fromMaybe $ error " TODO: Error" ) sockGetPath
192-
193-
194- addTextToStore :: LBS. ByteString -> LBS. ByteString -> PathSet -> RepairFlag -> MonadStore (Maybe Path )
195- addTextToStore name text references' repair = do
196- runOpArgs AddTextToStore $ do
197- putByteStringLen name
198- putByteStringLen text
199- putPaths references'
200- sockGetPath
201-
202- buildPaths :: PathSet -> Build. BuildMode -> MonadStore ()
203- buildPaths ps bm = void $ simpleOpArgs EnsurePath $ do
204- putPaths ps
205- putInt $ fromEnum bm
206-
207- ensurePath :: Path -> MonadStore ()
208- ensurePath pn = void $ simpleOpArgs EnsurePath $ putPath pn
209-
210- addTempRoot :: Path -> MonadStore ()
211- addTempRoot pn = void $ simpleOpArgs AddTempRoot $ putPath pn
212-
213- addIndirectRoot :: Path -> MonadStore ()
214- addIndirectRoot pn = void $ simpleOpArgs AddIndirectRoot $ putPath pn
21523
21624syncWithGC :: MonadStore ()
21725syncWithGC = void $ simpleOp SyncWithGC
21826
219- findRoots :: MonadStore Roots
220- findRoots = do
221- runOp FindRoots
222- res <- getSocketIncremental (do
223- count <- getInt
224- res <- sequence $ replicate count ((,) <$> getPath <*> getPath)
225- return res
226- )
227-
228- return $ M. fromList $ catMaybesTupled res
229- where
230- catMaybesTupled :: [(Maybe a , Maybe b )] -> [(a , b )]
231- catMaybesTupled ls = map (\ (Just x, Just y) -> (x, y)) $ filter (\ (x,y) -> isJust x && isJust y) ls
232-
233- collectGarbage :: GC. Options -> MonadStore GC. Result
234- collectGarbage opts = do
235- runOpArgs CollectGarbage $ do
236- putInt $ fromEnum $ GC. operation opts
237- putPaths $ GC. pathsToDelete opts
238- putBool $ GC. ignoreLiveness opts
239- putInt $ GC. maxFreed opts
240- forM_ [(0 :: Int ).. 2 ] $ pure $ putInt (0 :: Int ) -- removed options
241-
242- paths <- sockGetPaths
243- freed <- sockGetInt
244- _obsolete <- sockGetInt :: MonadStore Int
245-
246- return $ GC. Result paths freed
247-
24827optimiseStore :: MonadStore ()
24928optimiseStore = void $ simpleOp OptimiseStore
25029
@@ -253,8 +32,3 @@ verifyStore :: CheckFlag -> RepairFlag -> MonadStore Bool
25332verifyStore check repair = simpleOpArgs VerifyStore $ do
25433 putBool check
25534 putBool repair
256-
257- addSignatures :: Path -> [LBS. ByteString ] -> MonadStore ()
258- addSignatures p signatures = void $ simpleOpArgs AddSignatures $ do
259- putPath p
260- putByteStrings signatures
0 commit comments