Skip to content

Commit b3cffef

Browse files
committed
Propagate Nar streaming to the remote store
1 parent 474725b commit b3cffef

File tree

3 files changed

+18
-110
lines changed

3 files changed

+18
-110
lines changed

hnix-store-remote/src/System/Nix/Store/Remote.hs

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
module System.Nix.Store.Remote
1111
(
1212
addToStore
13-
, addToStoreNar
1413
, addTextToStore
1514
, addSignatures
1615
, addIndirectRoot
@@ -45,7 +44,6 @@ import Data.Text (Text)
4544
import Nix.Derivation (Derivation)
4645
import System.Nix.Build (BuildMode, BuildResult)
4746
import System.Nix.Hash (Digest, NamedAlgo, ValidAlgo, SomeNamedDigest(..))
48-
import System.Nix.Nar (Nar)
4947
import System.Nix.StorePath (StorePath, StorePathName, StorePathSet, StorePathHashAlgo)
5048
import System.Nix.StorePathMetadata (StorePathMetadata(..), StorePathTrust(..))
5149

@@ -83,74 +81,22 @@ addToStore :: forall a. (ValidAlgo a, NamedAlgo a)
8381
-> MonadStore StorePath
8482
addToStore name pth recursive _pathFilter _repair = do
8583

86-
nar :: ByteString <- Control.Monad.IO.Class.liftIO
87-
$ Data.Binary.Put.runPut . System.Nix.Nar.putNar
88-
<$> System.Nix.Nar.localPackNar System.Nix.Nar.narEffectsIO pth
84+
runOpArgsIO AddToStore $ \yield -> do
85+
yield $ Data.ByteString.Lazy.toStrict $ Data.Binary.Put.runPut $ do
86+
putText $ System.Nix.StorePath.unStorePathName name
8987

90-
runOpArgs AddToStore $ do
91-
putText $ System.Nix.StorePath.unStorePathName name
88+
putBool
89+
$ not
90+
$ System.Nix.Hash.algoName @a == "sha256" && recursive
9291

93-
putBool
94-
$ not
95-
$ System.Nix.Hash.algoName @a == "sha256" && recursive
92+
putBool recursive
9693

97-
putBool recursive
94+
putText $ System.Nix.Hash.algoName @a
9895

99-
putText $ System.Nix.Hash.algoName @a
100-
101-
Data.Binary.Put.putLazyByteString nar
96+
System.Nix.Nar.streamNarIO yield System.Nix.Nar.narEffectsIO pth
10297

10398
sockGetPath
10499

105-
-- | Add `Nar` to the store.
106-
--
107-
addToStoreNar :: StorePathMetadata
108-
-> Nar
109-
-> RepairFlag
110-
-> CheckSigsFlag
111-
-> MonadStore ()
112-
addToStoreNar StorePathMetadata{..} nar repair checkSigs = do
113-
-- after the command, protocol asks for data via Read message
114-
-- so we provide it here
115-
let n = Data.Binary.Put.runPut $ System.Nix.Nar.putNar nar
116-
setData n
117-
118-
void $ runOpArgs AddToStoreNar $ do
119-
putPath path
120-
maybe (putText "") (putPath) deriverPath
121-
let putNarHash :: SomeNamedDigest -> Data.Binary.Put.PutM ()
122-
putNarHash (SomeDigest hash) = putByteStringLen
123-
$ Data.ByteString.Lazy.fromStrict
124-
$ Data.Text.Encoding.encodeUtf8
125-
$ System.Nix.Hash.encodeBase32 hash
126-
127-
putNarHash narHash
128-
putPaths references
129-
putTime registrationTime
130-
131-
-- XXX: StorePathMetadata defines this as Maybe
132-
-- `putInt 0` instead of error?
133-
maybe (error "NO NAR BYTES") putInt narBytes
134-
135-
putBool (trust == BuiltLocally)
136-
137-
-- XXX: signatures need pubkey from config
138-
putTexts [""]
139-
140-
maybe
141-
(putText "")
142-
(putText
143-
. Data.Text.Lazy.toStrict
144-
. System.Nix.Store.Remote.Builders.buildContentAddressableAddress
145-
-- this calls for changing the type of addToStoreNar
146-
-- to forall a . (Valid/Named)Algo and a type app
147-
@'System.Nix.Hash.SHA256
148-
)
149-
contentAddressableAddress
150-
151-
putBool repair
152-
putBool (not checkSigs)
153-
154100
-- | Add text to store.
155101
--
156102
-- Reference accepts repair but only uses it

hnix-store-remote/src/System/Nix/Store/Remote/Protocol.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module System.Nix.Store.Remote.Protocol (
77
, simpleOpArgs
88
, runOp
99
, runOpArgs
10+
, runOpArgsIO
1011
, runStore
1112
, runStoreOpts) where
1213

@@ -17,12 +18,13 @@ import Control.Monad.State
1718

1819
import Data.Binary.Get
1920
import Data.Binary.Put
21+
import qualified Data.ByteString
2022
import qualified Data.ByteString.Char8
2123
import qualified Data.ByteString.Lazy
2224

2325
import Network.Socket (SockAddr(SockAddrUnix))
2426
import qualified Network.Socket
25-
import Network.Socket.ByteString (recv)
27+
import Network.Socket.ByteString (recv, sendAll)
2628

2729
import System.Nix.Store.Remote.Binary
2830
import System.Nix.Store.Remote.Logger
@@ -131,17 +133,16 @@ runOp :: WorkerOp -> MonadStore ()
131133
runOp op = runOpArgs op $ return ()
132134

133135
runOpArgs :: WorkerOp -> Put -> MonadStore ()
134-
runOpArgs op args = do
136+
runOpArgs op args = runOpArgsIO op (\encode -> encode $ Data.ByteString.Lazy.toStrict $ runPut args)
135137

136-
-- Temporary hack for printing the messages destined for nix-daemon socket
137-
when False $
138-
liftIO $ Data.ByteString.Lazy.writeFile "mytestfile2" $ runPut $ do
139-
putInt $ opNum op
140-
args
138+
runOpArgsIO :: WorkerOp -> ((Data.ByteString.ByteString -> MonadStore ()) -> MonadStore ()) -> MonadStore ()
139+
runOpArgsIO op encoder = do
141140

142141
sockPut $ do
143142
putInt $ opNum op
144-
args
143+
144+
soc <- storeSocket <$> ask
145+
encoder (liftIO . sendAll soc)
145146

146147
out <- processOutput
147148
modify (\(a, b) -> (a, b++out))

hnix-store-remote/tests/NixDaemon.hs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -163,39 +163,6 @@ invalidPath =
163163
let Right n = makeStorePathName "invalid"
164164
in StorePath (hash "invalid") n "no_such_root"
165165

166-
withNar act = do
167-
nar <- liftIO $ localPackNar narEffectsIO "dummy"
168-
now <- liftIO $ getCurrentTime
169-
170-
let narContents = runPut $ putNar nar
171-
narHash = hashLazy @SHA256 narContents
172-
-- narSize vs narBytes
173-
narBytes = BSL.length narContents
174-
175-
deriver <- addTextToStore "some-deriver" "" (HS.fromList []) False
176-
177-
sd <- getStoreDir
178-
let Right n = makeStorePathName "nar-path"
179-
path = makeFixedOutputPath sd False narHash n
180-
181-
addTempRoot path
182-
183-
let vp = VP.StorePathMetadata
184-
{ VP.path = path
185-
, VP.deriverPath = Just deriver
186-
, VP.narHash = SomeDigest narHash
187-
, VP.references = HS.empty
188-
, VP.registrationTime = now
189-
, VP.narBytes = Just $ fromIntegral narBytes
190-
, VP.trust = VP.BuiltLocally
191-
, VP.sigs = S.empty -- []
192-
, VP.contentAddressableAddress = Nothing
193-
}
194-
195-
addToStoreNar vp nar False False
196-
197-
act path
198-
199166
withBuilder action = do
200167
path <- addTextToStore "builder" builderSh (HS.fromList []) False
201168
action path
@@ -275,12 +242,6 @@ spec_protocol = Hspec.around withNixDaemon $ do
275242
let pathSet = HS.fromList [path]
276243
queryMissing pathSet `shouldReturn` (HS.empty, HS.empty, HS.empty, 0, 0)
277244

278-
context "addToStoreNar" $ do
279-
itRights "simple" $ withNar $ const return ()
280-
itRights "valid" $ withNar $ \narPath -> do
281-
liftIO $ print narPath
282-
(isValidPathUncached narPath) `shouldReturn` True
283-
284245
context "addToStore" $ do
285246
itRights "adds file to store" $ do
286247
fp <- liftIO $ writeSystemTempFile "addition" "lal"

0 commit comments

Comments
 (0)