Skip to content

Commit c095d12

Browse files
committed
remote: move flags to Types, wrap in newtype
1 parent b1e5906 commit c095d12

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

hnix-store-remote/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ main = do
2525
roots <- findRoots
2626
liftIO $ print roots
2727

28-
res <- addTextToStore "hnix-store" "test" mempty False
28+
res <- addTextToStore "hnix-store" "test" mempty dontRepair
2929
liftIO $ print res
3030
```

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ import System.Nix.Store.Remote.Util
7171
import Crypto.Hash ( SHA256 )
7272
import System.Nix.Nar ( NarSource )
7373

74-
type RepairFlag = Bool
75-
type CheckFlag = Bool
76-
type SubstituteFlag = Bool
7774

7875
-- | Pack `Nar` and add it to the store.
7976
addToStore
@@ -84,7 +81,10 @@ addToStore
8481
-> Bool -- ^ Add target directory recursively
8582
-> RepairFlag -- ^ Only used by local store backend
8683
-> MonadStore StorePath
87-
addToStore name source recursive _repair = do
84+
addToStore name source recursive repair = do
85+
when (unRepairFlag repair)
86+
$ error "repairing is not supported when building through the Nix daemon"
87+
8888
runOpArgsIO AddToStore $ \yield -> do
8989
yield $ toStrict $ Data.Binary.Put.runPut $ do
9090
putText $ System.Nix.StorePath.unStorePathName name
@@ -105,8 +105,9 @@ addTextToStore
105105
-> RepairFlag -- ^ Repair flag, must be `False` in case of remote backend
106106
-> MonadStore StorePath
107107
addTextToStore name text references' repair = do
108-
when repair
108+
when (unRepairFlag repair)
109109
$ error "repairing is not supported when building through the Nix daemon"
110+
110111
storeDir <- getStoreDir
111112
runOpArgs AddTextToStore $ do
112113
putText name
@@ -204,7 +205,7 @@ queryValidPaths ps substitute = do
204205
storeDir <- getStoreDir
205206
runOpArgs QueryValidPaths $ do
206207
putPaths storeDir ps
207-
putBool substitute
208+
putBool (unSubstituteFlag substitute)
208209
sockGetPaths
209210

210211
queryAllValidPaths :: MonadStore StorePathSet
@@ -321,5 +322,5 @@ syncWithGC = void $ simpleOp SyncWithGC
321322
-- returns True on errors
322323
verifyStore :: CheckFlag -> RepairFlag -> MonadStore Bool
323324
verifyStore check repair = simpleOpArgs VerifyStore $ do
324-
putBool check
325-
putBool repair
325+
putBool $ unCheckFlag check
326+
putBool $ unRepairFlag repair

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
module System.Nix.Store.Remote.Types
55
( MonadStore
66
, StoreConfig(..)
7+
, CheckFlag
8+
, doCheck
9+
, dontCheck
10+
, unCheckFlag
11+
, RepairFlag
12+
, doRepair
13+
, dontRepair
14+
, unRepairFlag
15+
, SubstituteFlag
16+
, doSubstitute
17+
, dontSubstitute
18+
, unSubstituteFlag
719
, Logger(..)
820
, Field(..)
921
, mapStoreDir
@@ -29,6 +41,31 @@ data StoreConfig = StoreConfig
2941
, storeSocket :: Socket
3042
}
3143

44+
-- | Check flag, used by @verifyStore@
45+
newtype CheckFlag = CheckFlag { unCheckFlag :: Bool }
46+
deriving (Eq, Ord, Show)
47+
48+
doCheck, dontCheck :: CheckFlag
49+
doCheck = CheckFlag True
50+
dontCheck = CheckFlag False
51+
52+
-- | Repair flag, used by @addToStore@, @addTextToStore@
53+
-- and @verifyStore@
54+
newtype RepairFlag = RepairFlag { unRepairFlag :: Bool }
55+
deriving (Eq, Ord, Show)
56+
57+
doRepair, dontRepair :: RepairFlag
58+
doRepair = RepairFlag True
59+
dontRepair = RepairFlag False
60+
61+
-- | Substitute flag, used by @queryValidPaths@
62+
newtype SubstituteFlag = SubstituteFlag { unSubstituteFlag :: Bool }
63+
deriving (Eq, Ord, Show)
64+
65+
doSubstitute, dontSubstitute :: SubstituteFlag
66+
doSubstitute = SubstituteFlag True
67+
dontSubstitute = SubstituteFlag False
68+
3269
type MonadStore a
3370
= ExceptT
3471
String

hnix-store-remote/tests/NixDaemon.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ itLefts name action = it name action isLeft
154154

155155
withPath :: (StorePath -> MonadStore a) -> MonadStore a
156156
withPath action = do
157-
path <- addTextToStore "hnix-store" "test" (HS.fromList []) False
157+
path <- addTextToStore "hnix-store" "test" mempty dontRepair
158158
action path
159159

160160
-- | dummy path, adds <tmp>/dummpy with "Hello World" contents
161161
dummy :: MonadStore StorePath
162162
dummy = do
163163
let name = Data.Either.fromRight (error "impossible") $ makeStorePathName "dummy"
164-
addToStore @SHA256 name (dumpPath "dummy") False False
164+
addToStore @SHA256 name (dumpPath "dummy") False dontRepair
165165

166166
invalidPath :: StorePath
167167
invalidPath =
@@ -170,7 +170,7 @@ invalidPath =
170170

171171
withBuilder :: (StorePath -> MonadStore a) -> MonadStore a
172172
withBuilder action = do
173-
path <- addTextToStore "builder" builderSh (HS.fromList []) False
173+
path <- addTextToStore "builder" builderSh mempty dontRepair
174174
action path
175175

176176
builderSh :: Text
@@ -186,14 +186,14 @@ spec_protocol = Hspec.around withNixDaemon $
186186

187187
context "verifyStore" $ do
188188
itRights "check=False repair=False" $
189-
verifyStore False False `shouldReturn` False
189+
verifyStore dontCheck dontRepair `shouldReturn` False
190190

191191
itRights "check=True repair=False" $
192-
verifyStore True False `shouldReturn` False
192+
verifyStore doCheck dontRepair `shouldReturn` False
193193

194194
--privileged
195195
itRights "check=True repair=True" $
196-
verifyStore True True `shouldReturn` False
196+
verifyStore doCheck doRepair `shouldReturn` False
197197

198198
context "addTextToStore" $
199199
itRights "adds text to store" $ withPath pure
@@ -252,7 +252,7 @@ spec_protocol = Hspec.around withNixDaemon $
252252
itRights "adds file to store" $ do
253253
fp <- liftIO $ writeSystemTempFile "addition" "lal"
254254
let name = Data.Either.fromRight (error "impossible") $ makeStorePathName "tmp-addition"
255-
res <- addToStore @SHA256 name (dumpPath fp) False False
255+
res <- addToStore @SHA256 name (dumpPath fp) False dontRepair
256256
liftIO $ print res
257257

258258
context "with dummy" $ do

0 commit comments

Comments
 (0)