remote: drop relude

I prefer explicit imports vs magic and this causes
an unused packages warning to pop (with no workaround or fix in sight).
This commit is contained in:
Richard Marko 2023-11-15 16:01:17 +01:00
parent 40838bd6dd
commit 319fd00b91
10 changed files with 133 additions and 77 deletions

View File

@ -44,11 +44,6 @@ common commons
, ViewPatterns
build-depends:
base >=4.12 && <5
, relude
mixins:
base hiding (Prelude)
, relude (Relude as Prelude)
, relude
default-language: Haskell2010
common tests
@ -122,11 +117,16 @@ test-suite remote
build-depends:
hnix-store-core
, hnix-store-remote
, bytestring
, cereal
, text
, time
, hspec
, tasty
, tasty-hspec
, tasty-quickcheck
, quickcheck-instances
, unordered-containers
test-suite remote-io
import: tests
@ -143,15 +143,16 @@ test-suite remote-io
NixDaemon
, Spec
build-depends:
bytestring
, hnix-store-core
hnix-store-core
, hnix-store-remote
, bytestring
, containers
, cryptonite
, directory
, process
, filepath
, hspec-expectations-lifted
, text
, tasty
, hspec
, tasty-hspec

View File

@ -35,7 +35,13 @@ module System.Nix.Store.Remote
)
where
import Prelude hiding ( putText )
import Data.HashSet (HashSet)
import Data.Map (Map)
import Data.Text (Text)
import qualified Control.Monad
import qualified Data.ByteString.Lazy
import qualified Data.Text.Encoding
--
import qualified Data.ByteString.Lazy as BSL
import Nix.Derivation ( Derivation )
@ -82,11 +88,11 @@ addToStore
-> RepairFlag -- ^ Only used by local store backend
-> MonadStore StorePath
addToStore name source recursive repair = do
when (unRepairFlag repair)
Control.Monad.when (unRepairFlag repair)
$ error "repairing is not supported when building through the Nix daemon"
runOpArgsIO AddToStore $ \yield -> do
yield $ toStrict $ Data.Binary.Put.runPut $ do
yield $ BSL.toStrict $ Data.Binary.Put.runPut $ do
putText $ System.Nix.StorePath.unStorePathName name
putBool $ not $ System.Nix.Hash.algoName @a == "sha256" && (unRecursive recursive)
putBool (unRecursive recursive)
@ -105,7 +111,7 @@ addTextToStore
-> RepairFlag -- ^ Repair flag, must be `False` in case of remote backend
-> MonadStore StorePath
addTextToStore name text references' repair = do
when (unRepairFlag repair)
Control.Monad.when (unRepairFlag repair)
$ error "repairing is not supported when building through the Nix daemon"
storeDir <- getStoreDir
@ -118,14 +124,14 @@ addTextToStore name text references' repair = do
addSignatures :: StorePath -> [BSL.ByteString] -> MonadStore ()
addSignatures p signatures = do
storeDir <- getStoreDir
void $ simpleOpArgs AddSignatures $ do
Control.Monad.void $ simpleOpArgs AddSignatures $ do
putPath storeDir p
putByteStrings signatures
addIndirectRoot :: StorePath -> MonadStore ()
addIndirectRoot pn = do
storeDir <- getStoreDir
void $ simpleOpArgs AddIndirectRoot $ putPath storeDir pn
Control.Monad.void $ simpleOpArgs AddIndirectRoot $ putPath storeDir pn
-- | Add temporary garbage collector root.
--
@ -133,7 +139,7 @@ addIndirectRoot pn = do
addTempRoot :: StorePath -> MonadStore ()
addTempRoot pn = do
storeDir <- getStoreDir
void $ simpleOpArgs AddTempRoot $ putPath storeDir pn
Control.Monad.void $ simpleOpArgs AddTempRoot $ putPath storeDir pn
-- | Build paths if they are an actual derivations.
--
@ -141,7 +147,7 @@ addTempRoot pn = do
buildPaths :: HashSet StorePath -> BuildMode -> MonadStore ()
buildPaths ps bm = do
storeDir <- getStoreDir
void $ simpleOpArgs BuildPaths $ do
Control.Monad.void $ simpleOpArgs BuildPaths $ do
putPaths storeDir ps
putInt $ fromEnum bm
@ -167,7 +173,7 @@ buildDerivation p drv buildMode = do
ensurePath :: StorePath -> MonadStore ()
ensurePath pn = do
storeDir <- getStoreDir
void $ simpleOpArgs EnsurePath $ putPath storeDir pn
Control.Monad.void $ simpleOpArgs EnsurePath $ putPath storeDir pn
-- | Find garbage collector roots.
findRoots :: MonadStore (Map BSL.ByteString StorePath)
@ -178,7 +184,7 @@ findRoots = do
getSocketIncremental
$ getMany
$ (,)
<$> (fromStrict <$> getByteStringLen)
<$> (BSL.fromStrict <$> getByteStringLen)
<*> getPath sd
r <- catRights res
@ -226,17 +232,17 @@ queryPathInfoUncached path = do
putPath storeDir path
valid <- sockGetBool
unless valid $ error "Path is not valid"
Control.Monad.unless valid $ error "Path is not valid"
deriverPath <- sockGetPathMay
narHashText <- decodeUtf8 <$> sockGetStr
narHashText <- Data.Text.Encoding.decodeUtf8 <$> sockGetStr
let
narHash =
case
decodeDigestWith @SHA256 NixBase32 narHashText
of
Left e -> error $ fromString e
Left e -> error e
Right x -> SomeDigest x
references <- sockGetPaths
@ -255,7 +261,7 @@ queryPathInfoUncached path = do
case
System.Nix.Store.Remote.Parsers.parseContentAddressableAddress caString
of
Left e -> error $ fromString e
Left e -> error e
Right x -> Just x
trust = if ultimate then BuiltLocally else BuiltElsewhere
@ -290,7 +296,8 @@ queryPathFromHashPart :: StorePathHashPart -> MonadStore StorePath
queryPathFromHashPart storePathHash = do
runOpArgs QueryPathFromHashPart
$ putByteStringLen
$ encodeUtf8
$ Data.ByteString.Lazy.fromStrict
$ Data.Text.Encoding.encodeUtf8
$ encodeWith NixBase32
$ System.Nix.StorePath.unStorePathHashPart
storePathHash
@ -317,10 +324,10 @@ queryMissing ps = do
pure (willBuild, willSubstitute, unknown, downloadSize', narSize')
optimiseStore :: MonadStore ()
optimiseStore = void $ simpleOp OptimiseStore
optimiseStore = Control.Monad.void $ simpleOp OptimiseStore
syncWithGC :: MonadStore ()
syncWithGC = void $ simpleOp SyncWithGC
syncWithGC = Control.Monad.void $ simpleOp SyncWithGC
-- returns True on errors
verifyStore :: CheckFlag -> RepairFlag -> MonadStore Bool

View File

@ -4,6 +4,8 @@ Maintainer : srk <srk@48.io>
|-}
module System.Nix.Store.Remote.Binary where
import Control.Monad
import Data.ByteString (ByteString)
import Data.Binary.Get
import Data.Binary.Put
import qualified Data.ByteString.Lazy as BSL
@ -45,7 +47,7 @@ getByteStringLen = do
when (len `mod` 8 /= 0) $ do
pads <- unpad $ fromIntegral $ 8 - (len `mod` 8)
unless (all (== 0) pads) $ fail $ "No zeroes" <> show (st, len, pads)
pure $ toStrict st
pure $ BSL.toStrict st
where unpad x = replicateM x getWord8
getByteStrings :: Get [ByteString]

View File

@ -8,8 +8,10 @@ module System.Nix.Store.Remote.Logger
where
import Prelude hiding ( Last )
import Control.Monad.Except ( throwError )
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (asks)
import Control.Monad.State.Strict (get)
import Data.Binary.Get
import Network.Socket.ByteString ( recv )
@ -19,6 +21,8 @@ import System.Nix.Store.Remote.Types
import System.Nix.Store.Remote.Util
import qualified Control.Monad
controlParser :: Get Logger
controlParser = do
ctrl <- getInt
@ -70,12 +74,12 @@ processOutput = go decoder
chunk <- liftIO (Just <$> recv soc 8)
go (k chunk)
go (Fail _leftover _consumed msg) = error $ fromString msg
go (Fail _leftover _consumed msg) = error msg
getFields :: Get [Field]
getFields = do
cnt <- getInt
replicateM cnt getField
Control.Monad.replicateM cnt getField
getField :: Get Field
getField = do

View File

@ -8,12 +8,16 @@ module System.Nix.Store.Remote.Parsers
)
where
import Control.Applicative ((<|>))
import Data.ByteString (ByteString)
import Data.Text (Text)
import Data.Attoparsec.ByteString.Char8
import System.Nix.Hash
import System.Nix.StorePath ( ContentAddressableAddress(..)
, NarHashMode(..)
)
import Crypto.Hash ( SHA256 )
import qualified Data.Text.Encoding
-- | Parse `ContentAddressableAddress` from `ByteString`
parseContentAddressableAddress
@ -45,7 +49,10 @@ parseTypedDigest = mkNamedDigest <$> parseHashType <*> parseHash
parseHashType :: Parser Text
parseHashType =
decodeUtf8 <$> ("sha256" <|> "sha512" <|> "sha1" <|> "md5") <* (":" <|> "-")
Data.Text.Encoding.decodeUtf8
<$> ("sha256" <|> "sha512" <|> "sha1" <|> "md5") <* (":" <|> "-")
parseHash :: Parser Text
parseHash = decodeUtf8 <$> takeWhile1 (/= ':')
parseHash =
Data.Text.Encoding.decodeUtf8
<$> takeWhile1 (/= ':')

View File

@ -16,15 +16,18 @@ module System.Nix.Store.Remote.Protocol
)
where
import qualified Relude.Unsafe as Unsafe
import qualified Control.Monad
import Control.Exception ( bracket )
import Control.Monad.Except
import Control.Monad.Reader (asks, runReaderT)
import Control.Monad.State.Strict
import qualified Data.Bool
import Data.Binary.Get
import Data.Binary.Put
import qualified Data.ByteString
import qualified Data.ByteString.Char8
import qualified Data.ByteString.Lazy
import Network.Socket ( SockAddr(SockAddrUnix) )
import qualified Network.Socket as S
@ -123,28 +126,28 @@ opNum QueryMissing = 40
simpleOp :: WorkerOp -> MonadStore Bool
simpleOp op = simpleOpArgs op pass
simpleOp op = simpleOpArgs op $ pure ()
simpleOpArgs :: WorkerOp -> Put -> MonadStore Bool
simpleOpArgs op args = do
runOpArgs op args
err <- gotError
bool
Data.Bool.bool
sockGetBool
(do
Error _num msg <- Unsafe.head <$> getError
Error _num msg <- head <$> getError
throwError $ Data.ByteString.Char8.unpack msg
)
err
runOp :: WorkerOp -> MonadStore ()
runOp op = runOpArgs op pass
runOp op = runOpArgs op $ pure ()
runOpArgs :: WorkerOp -> Put -> MonadStore ()
runOpArgs op args =
runOpArgsIO
op
(\encode -> encode $ toStrict $ runPut args)
(\encode -> encode $ Data.ByteString.Lazy.toStrict $ runPut args)
runOpArgsIO
:: WorkerOp
@ -160,8 +163,8 @@ runOpArgsIO op encoder = do
out <- processOutput
modify (\(a, b) -> (a, b <> out))
err <- gotError
when err $ do
Error _num msg <- Unsafe.head <$> getError
Control.Monad.when err $ do
Error _num msg <- head <$> getError
throwError $ Data.ByteString.Char8.unpack msg
runStore :: MonadStore a -> IO (Either String a, [Logger])
@ -198,11 +201,11 @@ runStoreOpts' sockFamily sockAddr storeRootDir code =
vermagic <- liftIO $ recv soc 16
let
(magic2, _daemonProtoVersion) =
flip runGet (fromStrict vermagic)
flip runGet (Data.ByteString.Lazy.fromStrict vermagic)
$ (,)
<$> (getInt :: Get Int)
<*> (getInt :: Get Int)
unless (magic2 == workerMagic2) $ error "Worker magic 2 mismatch"
Control.Monad.unless (magic2 == workerMagic2) $ error "Worker magic 2 mismatch"
sockPut $ putInt protoVersion -- clientVersion
sockPut $ putInt (0 :: Int) -- affinity

View File

@ -4,16 +4,21 @@ Maintainer : srk <srk@48.io>
|-}
module System.Nix.Store.Remote.Serialize.Prim where
import Data.ByteString (ByteString)
import Data.Fixed (Uni)
import Data.HashSet (HashSet)
import Data.Serialize.Get (Get)
import Data.Serialize.Put (Putter)
import Data.Text (Text)
import Data.Time (NominalDiffTime, UTCTime)
import System.Nix.StorePath (StoreDir, StorePath, InvalidPathError)
import qualified Control.Monad
import qualified Data.HashSet
import qualified Data.Serialize.Get
import qualified Data.Serialize.Put
import qualified Data.ByteString
import qualified Data.Text.Encoding
import qualified Data.Time.Clock.POSIX
import qualified System.Nix.StorePath
@ -80,7 +85,7 @@ putTime =
getMany :: Get a -> Get [a]
getMany parser = do
count <- getInt
replicateM count parser
Control.Monad.replicateM count parser
-- | Serialize a list
putMany :: Foldable t => Putter a -> Putter (t a)
@ -96,11 +101,13 @@ getByteString :: Get ByteString
getByteString = do
len <- getInt
st <- Data.Serialize.Get.getByteString len
when (len `mod` 8 /= 0) $ do
Control.Monad.when (len `mod` 8 /= 0) $ do
pads <- unpad $ fromIntegral $ 8 - (len `mod` 8)
unless (all (== 0) pads) $ fail $ "No zeroes" <> show (st, len, pads)
Control.Monad.unless
(all (== 0) pads)
$ fail $ "No zeroes" <> show (st, len, pads)
pure st
where unpad x = replicateM x Data.Serialize.Get.getWord8
where unpad x = Control.Monad.replicateM x Data.Serialize.Get.getWord8
-- | Serialize @ByteString@ using length
-- prefixed string packing with padding to 8 bytes
@ -108,11 +115,13 @@ putByteString :: Putter ByteString
putByteString x = do
putInt len
Data.Serialize.Put.putByteString x
when (len `mod` 8 /= 0) $ pad $ 8 - (len `mod` 8)
Control.Monad.when
(len `mod` 8 /= 0)
$ pad $ 8 - (len `mod` 8)
where
len :: Int
len = fromIntegral $ Data.ByteString.length x
pad count = replicateM_ count (Data.Serialize.Put.putWord8 0)
pad count = Control.Monad.replicateM_ count (Data.Serialize.Put.putWord8 0)
-- | Deserialize a list of @ByteString@s
getByteStrings :: Get [ByteString]
@ -126,19 +135,19 @@ putByteStrings = putMany putByteString
-- | Deserialize @Text@
getText :: Get Text
getText = decodeUtf8 <$> getByteString
getText = Data.Text.Encoding.decodeUtf8 <$> getByteString
-- | Serialize @Text@
putText :: Putter Text
putText = putByteString . encodeUtf8
putText = putByteString . Data.Text.Encoding.encodeUtf8
-- | Deserialize a list of @Text@s
getTexts :: Get [Text]
getTexts = fmap decodeUtf8 <$> getByteStrings
getTexts = fmap Data.Text.Encoding.decodeUtf8 <$> getByteStrings
-- | Serialize a list of @Text@s
putTexts :: (Functor f, Foldable f) => Putter (f Text)
putTexts = putByteStrings . fmap encodeUtf8
putTexts = putByteStrings . fmap Data.Text.Encoding.encodeUtf8
-- * StorePath

View File

@ -33,13 +33,21 @@ module System.Nix.Store.Remote.Types
)
where
import Control.Monad.Trans.State.Strict (mapStateT)
import Control.Monad.Trans.Except (mapExceptT)
import Control.Monad.Except (ExceptT)
import Control.Monad.Reader (ReaderT, asks)
import Control.Monad.State.Strict (StateT, gets, modify)
import Data.ByteString (ByteString)
import Network.Socket (Socket)
import qualified Data.ByteString.Lazy as BSL
import Network.Socket ( Socket )
import Control.Monad.Trans.State.Strict (mapStateT)
import Control.Monad.Trans.Except (mapExceptT)
import Control.Monad.Trans.Reader (withReaderT)
import System.Nix.StorePath ( StoreDir )
data StoreConfig = StoreConfig
{ storeDir :: StoreDir
, storeSocket :: Socket

View File

@ -1,11 +1,18 @@
{-# language RecordWildCards #-}
module System.Nix.Store.Remote.Util where
import Prelude hiding ( putText )
import Control.Monad.Except
import Control.Monad.Except (throwError)
import Control.Monad.Reader (asks)
import Control.Monad.IO.Class (MonadIO(..))
import Data.ByteString (ByteString)
import Data.HashSet (HashSet)
import Data.Text (Text)
import Data.Either (rights)
import Data.Binary.Get
import Data.Binary.Put
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import Data.Time
import Data.Time.Clock.POSIX
@ -34,7 +41,7 @@ genericIncremental getsome parser = go decoder
go (Partial k ) = do
chunk <- getsome
go (k chunk)
go (Fail _leftover _consumed msg) = error $ fromString msg
go (Fail _leftover _consumed msg) = error msg
getSocketIncremental :: Get a -> MonadStore a
getSocketIncremental = genericIncremental sockGet8
@ -47,7 +54,7 @@ getSocketIncremental = genericIncremental sockGet8
sockPut :: Put -> MonadStore ()
sockPut p = do
soc <- asks storeSocket
liftIO $ sendAll soc $ toStrict $ runPut p
liftIO $ sendAll soc $ BSL.toStrict $ runPut p
sockGet :: Get a -> MonadStore a
sockGet = getSocketIncremental
@ -89,16 +96,16 @@ sockGetPaths = do
getSocketIncremental (getPaths sd)
bsToText :: ByteString -> Text
bsToText = decodeUtf8
bsToText = T.decodeUtf8
textToBS :: Text -> ByteString
textToBS = encodeUtf8
textToBS = T.encodeUtf8
bslToText :: BSL.ByteString -> Text
bslToText = toText . TL.decodeUtf8
bslToText = TL.toStrict . TL.decodeUtf8
textToBSL :: Text -> BSL.ByteString
textToBSL = TL.encodeUtf8 . toLText
textToBSL = TL.encodeUtf8 . TL.fromStrict
putText :: Text -> Put
putText = putByteStringLen . textToBSL
@ -114,11 +121,11 @@ getPaths sd =
Data.HashSet.fromList . rights . fmap (parsePath sd) <$> getByteStrings
putPath :: StoreDir -> StorePath -> Put
putPath storeDir = putByteStringLen . fromStrict . storePathToRawFilePath storeDir
putPath storeDir = putByteStringLen . BSL.fromStrict . storePathToRawFilePath storeDir
putPaths :: StoreDir -> HashSet StorePath -> Put
putPaths storeDir = putByteStrings . Data.HashSet.toList . Data.HashSet.map
(fromStrict . storePathToRawFilePath storeDir)
(BSL.fromStrict . storePathToRawFilePath storeDir)
putBool :: Bool -> Put
putBool True = putInt (1 :: Int)

View File

@ -1,9 +1,17 @@
{-# language DataKinds #-}
{-# language ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
module NixDaemon where
import qualified System.Environment as Env
import Data.Text (Text)
import Data.Either ( isRight
, isLeft
)
import Data.Bool ( bool )
import Control.Monad ( void )
import Control.Monad.IO.Class ( liftIO )
import qualified System.Environment
import Control.Exception ( bracket )
import Control.Concurrent ( threadDelay )
import qualified Data.ByteString.Char8 as BSC
@ -34,7 +42,7 @@ import System.Nix.Nar ( dumpPath )
createProcessEnv :: FilePath -> String -> [String] -> IO P.ProcessHandle
createProcessEnv fp proc args = do
mPath <- Env.lookupEnv "PATH"
mPath <- System.Environment.lookupEnv "PATH"
(_, _, _, ph) <-
P.createProcess (P.proc proc args)
@ -44,13 +52,13 @@ createProcessEnv fp proc args = do
pure ph
mockedEnv :: Maybe String -> FilePath -> [(String, FilePath)]
mockedEnv mEnvPath fp = (fp </>) <<$>>
[ ("NIX_STORE_DIR" , "store")
, ("NIX_LOCALSTATE_DIR", "var")
, ("NIX_LOG_DIR" , "var" </> "log")
, ("NIX_STATE_DIR" , "var" </> "nix")
, ("NIX_CONF_DIR" , "etc")
, ("HOME" , "home")
mockedEnv mEnvPath fp =
[ ("NIX_STORE_DIR" , fp </> "store")
, ("NIX_LOCALSTATE_DIR", fp </> "var")
, ("NIX_LOG_DIR" , fp </> "var" </> "log")
, ("NIX_STATE_DIR" , fp </> "var" </> "nix")
, ("NIX_CONF_DIR" , fp </> "etc")
, ("HOME" , fp </> "home")
-- , ("NIX_REMOTE", "daemon")
] <> foldMap (\x -> [("PATH", x)]) mEnvPath
@ -60,12 +68,12 @@ waitSocket fp x = do
ex <- doesFileExist fp
bool
(threadDelay 100000 >> waitSocket fp (x - 1))
pass
(pure ())
ex
writeConf :: FilePath -> IO ()
writeConf fp =
writeFileText fp $ unlines
writeFile fp $ unlines
[ "build-users-group = "
, "trusted-users = root"
, "allowed-users = *"
@ -136,7 +144,7 @@ it
-> (a -> Bool)
-> Hspec.SpecWith (m () -> IO (a, b))
it name action check =
Hspec.it name $ \run -> run (action >> pass) `checks` check
Hspec.it name $ \run -> run (void $ action) `checks` check
itRights
:: (Show a, Show b, Show c, Monad m)