{-# language AllowAmbiguousTypes #-}
{-# language CPP #-}
{-# language DefaultSignatures #-}
{-# language TypeFamilies #-}
{-# language DataKinds #-}
{-# language GeneralizedNewtypeDeriving #-}
{-# language UndecidableInstances #-}
{-# language PackageImports #-} -- 2021-07-05: Due to hashing Haskell IT system situation, in HNix we currently ended-up with 2 hash package dependencies @{hashing, cryptonite}@

{-# options_ghc -Wno-orphans #-}


module Nix.Effects where

import           Nix.Prelude             hiding ( putStrLn
                                                , print
                                                )
import qualified Nix.Prelude                   as Prelude
import           GHC.Exception                  ( ErrorCall(ErrorCall) )
import qualified Data.HashSet                  as HS
import qualified Data.Text                     as Text
import           Network.HTTP.Client     hiding ( path, Proxy )
import           Network.HTTP.Client.TLS
import           Network.HTTP.Types
import qualified "cryptonite" Crypto.Hash      as Hash
import           Nix.Utils.Fix1
import           Nix.Expr.Types.Annotated
import           Nix.Frames              hiding ( Proxy )
import           Nix.Parser
import           Nix.Render
import           Nix.Value
import qualified Paths_hnix
import           System.Exit
import qualified System.Info
import           System.Process

import qualified System.Nix.Store.Remote       as Store.Remote
import qualified System.Nix.StorePath          as Store

-- | A path into the nix store
newtype StorePath = StorePath Path


-- All of the following type classes defer to the underlying 'm'.

-- * @class MonadEffects t f m@

class
  ( MonadFile m
  , MonadStore m
  , MonadPutStr m
  , MonadHttp m
  , MonadEnv m
  , MonadPaths m
  , MonadInstantiate m
  , MonadExec m
  , MonadIntrospect m
  )
  => MonadEffects t f m where

  -- | Determine the absolute path in the current context.
  toAbsolutePath :: Path -> m Path
  findEnvPath :: String -> m Path

  -- | Having an explicit list of sets corresponding to the @NIX_PATH@ and a file path try to find an existing path.
  findPath :: [NValue t f m] -> Path -> m Path

  importPath :: Path -> m (NValue t f m)
  pathToDefaultNix :: Path -> m Path

  derivationStrict :: NValue t f m -> m (NValue t f m)

  --  2021-04-01: for trace, so leaving String here
  traceEffect :: String -> m ()


-- ** Instances

instance
  ( MonadFix1T t m
  , MonadStore m
  )
  => MonadStore (Fix1T t m)
 where
  addToStore :: StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> Fix1T t m (Either ErrorCall StorePath)
addToStore StorePathName
a Path
b RecursiveFlag
c RecursiveFlag
d = m (Either ErrorCall StorePath)
-> Fix1T t m (Either ErrorCall StorePath)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ErrorCall StorePath)
 -> Fix1T t m (Either ErrorCall StorePath))
-> m (Either ErrorCall StorePath)
-> Fix1T t m (Either ErrorCall StorePath)
forall a b. (a -> b) -> a -> b
$ StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadStore m =>
StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
addToStore StorePathName
a Path
b RecursiveFlag
c RecursiveFlag
d
  addTextToStore' :: StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> Fix1T t m (Either ErrorCall StorePath)
addTextToStore' StorePathName
a StorePathName
b StorePathSet
c RecursiveFlag
d = m (Either ErrorCall StorePath)
-> Fix1T t m (Either ErrorCall StorePath)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ErrorCall StorePath)
 -> Fix1T t m (Either ErrorCall StorePath))
-> m (Either ErrorCall StorePath)
-> Fix1T t m (Either ErrorCall StorePath)
forall a b. (a -> b) -> a -> b
$ StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadStore m =>
StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
addTextToStore' StorePathName
a StorePathName
b StorePathSet
c RecursiveFlag
d

-- * @class MonadIntrospect m@

class
  Monad m
  => MonadIntrospect m
 where
  recursiveSize :: a -> m Word
  default recursiveSize :: (MonadTrans t, MonadIntrospect m', m ~ t m') => a -> m Word
  recursiveSize = m' Word -> t m' Word
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' Word -> t m' Word) -> (a -> m' Word) -> a -> t m' Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m' Word
forall (m :: * -> *) a. MonadIntrospect m => a -> m Word
recursiveSize


-- ** Instances

instance MonadIntrospect IO where
  recursiveSize :: a -> IO Word
recursiveSize =
#ifdef MIN_VERSION_ghc_datasize
    recursiveSize
#else
    IO Word -> a -> IO Word
forall a b. a -> b -> a
const (IO Word -> a -> IO Word) -> IO Word -> a -> IO Word
forall a b. (a -> b) -> a -> b
$ Word -> IO Word
forall (f :: * -> *) a. Applicative f => a -> f a
pure Word
0
#endif

deriving
  instance
    MonadIntrospect (t (Fix1 t))
    => MonadIntrospect (Fix1 t)

deriving
  instance
    MonadIntrospect (t (Fix1T t m) m)
    => MonadIntrospect (Fix1T t m)


-- * @class MonadExec m@

class
  Monad m
  => MonadExec m where

    exec' :: [Text] -> m (Either ErrorCall NExprLoc)
    default exec' :: (MonadTrans t, MonadExec m', m ~ t m')
                  => [Text] -> m (Either ErrorCall NExprLoc)
    exec' = m' (Either ErrorCall NExprLoc) -> t m' (Either ErrorCall NExprLoc)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Either ErrorCall NExprLoc)
 -> t m' (Either ErrorCall NExprLoc))
-> ([StorePathName] -> m' (Either ErrorCall NExprLoc))
-> [StorePathName]
-> t m' (Either ErrorCall NExprLoc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [StorePathName] -> m' (Either ErrorCall NExprLoc)
forall (m :: * -> *).
MonadExec m =>
[StorePathName] -> m (Either ErrorCall NExprLoc)
exec'


-- ** Instances

instance MonadExec IO where
  exec' :: [StorePathName] -> IO (Either ErrorCall NExprLoc)
exec' = \case
    []            -> Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc))
-> Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc)
forall a b. (a -> b) -> a -> b
$ ErrorCall -> Either ErrorCall NExprLoc
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall NExprLoc)
-> ErrorCall -> Either ErrorCall NExprLoc
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall String
"exec: missing program"
    (StorePathName
prog : [StorePathName]
args) -> do
      (ExitCode
exitCode, String
out, String
_) <- IO (ExitCode, String, String) -> IO (ExitCode, String, String)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ExitCode, String, String) -> IO (ExitCode, String, String))
-> IO (ExitCode, String, String) -> IO (ExitCode, String, String)
forall a b. (a -> b) -> a -> b
$ String -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode (StorePathName -> String
forall a. ToString a => a -> String
toString StorePathName
prog) (StorePathName -> String
forall a. ToString a => a -> String
toString (StorePathName -> String) -> [StorePathName] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [StorePathName]
args) String
forall a. Monoid a => a
mempty
      let
        t :: StorePathName
t    = StorePathName -> StorePathName
Text.strip (StorePathName -> StorePathName) -> StorePathName -> StorePathName
forall a b. (a -> b) -> a -> b
$ String -> StorePathName
forall a. IsString a => String -> a
fromString String
out
        emsg :: StorePathName
emsg = StorePathName
"program[" StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> StorePathName
prog StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> StorePathName
"] args=" StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> [StorePathName] -> StorePathName
forall b a. (Show a, IsString b) => a -> b
show [StorePathName]
args
      case ExitCode
exitCode of
        ExitCode
ExitSuccess ->
          Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc))
-> Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc)
forall a b. (a -> b) -> a -> b
$
          if StorePathName -> RecursiveFlag
Text.null StorePathName
t
            then ErrorCall -> Either ErrorCall NExprLoc
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall NExprLoc)
-> ErrorCall -> Either ErrorCall NExprLoc
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ StorePathName -> String
forall a. ToString a => a -> String
toString (StorePathName -> String) -> StorePathName -> String
forall a b. (a -> b) -> a -> b
$ StorePathName
"exec has no output :" StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> StorePathName
emsg
            else
              (Doc Void -> Either ErrorCall NExprLoc)
-> (NExprLoc -> Either ErrorCall NExprLoc)
-> Either (Doc Void) NExprLoc
-> Either ErrorCall NExprLoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
                (\ Doc Void
err -> ErrorCall -> Either ErrorCall NExprLoc
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall NExprLoc)
-> ErrorCall -> Either ErrorCall NExprLoc
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ StorePathName -> String
forall a. ToString a => a -> String
toString (StorePathName -> String) -> StorePathName -> String
forall a b. (a -> b) -> a -> b
$ StorePathName
"Error parsing output of exec: " StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> Doc Void -> StorePathName
forall b a. (Show a, IsString b) => a -> b
show Doc Void
err StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> StorePathName
" " StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> StorePathName
emsg)
                NExprLoc -> Either ErrorCall NExprLoc
forall (f :: * -> *) a. Applicative f => a -> f a
pure
                (StorePathName -> Either (Doc Void) NExprLoc
parseNixTextLoc StorePathName
t)
        ExitCode
err -> Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc))
-> Either ErrorCall NExprLoc -> IO (Either ErrorCall NExprLoc)
forall a b. (a -> b) -> a -> b
$ ErrorCall -> Either ErrorCall NExprLoc
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall NExprLoc)
-> ErrorCall -> Either ErrorCall NExprLoc
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ StorePathName -> String
forall a. ToString a => a -> String
toString (StorePathName -> String) -> StorePathName -> String
forall a b. (a -> b) -> a -> b
$ StorePathName
"exec  failed: " StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> ExitCode -> StorePathName
forall b a. (Show a, IsString b) => a -> b
show ExitCode
err StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> StorePathName
" " StorePathName -> StorePathName -> StorePathName
forall a. Semigroup a => a -> a -> a
<> StorePathName
emsg

deriving
  instance
    MonadExec (t (Fix1 t))
    => MonadExec (Fix1 t)

deriving
  instance
    MonadExec (t (Fix1T t m) m)
    => MonadExec (Fix1T t m)


-- * @class MonadInstantiate m@

class
  Monad m
  => MonadInstantiate m where

    instantiateExpr :: Text -> m (Either ErrorCall NExprLoc)
    default instantiateExpr :: (MonadTrans t, MonadInstantiate m', m ~ t m') => Text -> m (Either ErrorCall NExprLoc)
    instantiateExpr = m' (Either ErrorCall NExprLoc) -> t m' (Either ErrorCall NExprLoc)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Either ErrorCall NExprLoc)
 -> t m' (Either ErrorCall NExprLoc))
-> (StorePathName -> m' (Either ErrorCall NExprLoc))
-> StorePathName
-> t m' (Either ErrorCall NExprLoc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePathName -> m' (Either ErrorCall NExprLoc)
forall (m :: * -> *).
MonadInstantiate m =>
StorePathName -> m (Either ErrorCall NExprLoc)
instantiateExpr


-- ** Instances

instance MonadInstantiate IO where

  instantiateExpr :: StorePathName -> IO (Either ErrorCall NExprLoc)
instantiateExpr StorePathName
expr =
    do
      String -> IO ()
forall (m :: * -> *). Monad m => String -> m ()
traceM (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$
        String
"Executing: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [StorePathName] -> String
forall b a. (Show a, IsString b) => a -> b
show [StorePathName
"nix-instantiate", StorePathName
"--eval", StorePathName
"--expr ", StorePathName
expr]

      (ExitCode
exitCode, String
out, String
err) <-
        String -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode
          String
"nix-instantiate"
          [String
"--eval", String
"--expr", StorePathName -> String
forall a. ToString a => a -> String
toString StorePathName
expr]
          String
forall a. Monoid a => a
mempty

      pure $
        case ExitCode
exitCode of
          ExitCode
ExitSuccess ->
            (Doc Void -> Either ErrorCall NExprLoc)
-> (NExprLoc -> Either ErrorCall NExprLoc)
-> Either (Doc Void) NExprLoc
-> Either ErrorCall NExprLoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
              (\ Doc Void
e -> ErrorCall -> Either ErrorCall NExprLoc
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall NExprLoc)
-> ErrorCall -> Either ErrorCall NExprLoc
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Error parsing output of nix-instantiate: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Doc Void -> String
forall b a. (Show a, IsString b) => a -> b
show Doc Void
e)
              NExprLoc -> Either ErrorCall NExprLoc
forall (f :: * -> *) a. Applicative f => a -> f a
pure
              (StorePathName -> Either (Doc Void) NExprLoc
parseNixTextLoc (StorePathName -> Either (Doc Void) NExprLoc)
-> StorePathName -> Either (Doc Void) NExprLoc
forall a b. (a -> b) -> a -> b
$ String -> StorePathName
forall a. IsString a => String -> a
fromString String
out)
          ExitCode
status -> ErrorCall -> Either ErrorCall NExprLoc
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall NExprLoc)
-> ErrorCall -> Either ErrorCall NExprLoc
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"nix-instantiate failed: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ExitCode -> String
forall b a. (Show a, IsString b) => a -> b
show ExitCode
status String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
err

deriving
  instance
    MonadInstantiate (t (Fix1 t))
    => MonadInstantiate (Fix1 t)

deriving
  instance
    MonadInstantiate (t (Fix1T t m) m)
    => MonadInstantiate (Fix1T t m)


-- * @class MonadEnv m@

class
  Monad m
  => MonadEnv m where

  getEnvVar :: Text -> m (Maybe Text)
  default getEnvVar :: (MonadTrans t, MonadEnv m', m ~ t m') => Text -> m (Maybe Text)
  getEnvVar = m' (Maybe StorePathName) -> t m' (Maybe StorePathName)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Maybe StorePathName) -> t m' (Maybe StorePathName))
-> (StorePathName -> m' (Maybe StorePathName))
-> StorePathName
-> t m' (Maybe StorePathName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePathName -> m' (Maybe StorePathName)
forall (m :: * -> *).
MonadEnv m =>
StorePathName -> m (Maybe StorePathName)
getEnvVar

  getCurrentSystemOS :: m Text
  default getCurrentSystemOS :: (MonadTrans t, MonadEnv m', m ~ t m') => m Text
  getCurrentSystemOS = m' StorePathName -> t m' StorePathName
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m' StorePathName
forall (m :: * -> *). MonadEnv m => m StorePathName
getCurrentSystemOS

  getCurrentSystemArch :: m Text
  default getCurrentSystemArch :: (MonadTrans t, MonadEnv m', m ~ t m') => m Text
  getCurrentSystemArch = m' StorePathName -> t m' StorePathName
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m' StorePathName
forall (m :: * -> *). MonadEnv m => m StorePathName
getCurrentSystemArch


-- ** Instances

instance MonadEnv IO where
  getEnvVar :: StorePathName -> IO (Maybe StorePathName)
getEnvVar            = (String -> StorePathName)
-> IO (Maybe String) -> IO (Maybe StorePathName)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
(<<$>>) String -> StorePathName
forall a. IsString a => String -> a
fromString (IO (Maybe String) -> IO (Maybe StorePathName))
-> (StorePathName -> IO (Maybe String))
-> StorePathName
-> IO (Maybe StorePathName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO (Maybe String)
forall (m :: * -> *). MonadIO m => String -> m (Maybe String)
lookupEnv (String -> IO (Maybe String))
-> (StorePathName -> String) -> StorePathName -> IO (Maybe String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePathName -> String
forall a. ToString a => a -> String
toString

  getCurrentSystemOS :: IO StorePathName
getCurrentSystemOS   = StorePathName -> IO StorePathName
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StorePathName -> IO StorePathName)
-> StorePathName -> IO StorePathName
forall a b. (a -> b) -> a -> b
$ String -> StorePathName
forall a. IsString a => String -> a
fromString String
System.Info.os

  -- Invert the conversion done by GHC_CONVERT_CPU in GHC's aclocal.m4
  getCurrentSystemArch :: IO StorePathName
getCurrentSystemArch = StorePathName -> IO StorePathName
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StorePathName -> IO StorePathName)
-> StorePathName -> IO StorePathName
forall a b. (a -> b) -> a -> b
$ String -> StorePathName
forall a. IsString a => String -> a
fromString (String -> StorePathName) -> String -> StorePathName
forall a b. (a -> b) -> a -> b
$ case String
System.Info.arch of
    String
"i386" -> String
"i686"
    String
arch   -> String
arch

deriving
  instance
    MonadEnv (t (Fix1 t))
    => MonadEnv (Fix1 t)

deriving
  instance
    MonadEnv (t (Fix1T t m) m)
    => MonadEnv (Fix1T t m)


-- * @class MonadPaths m@

class
  Monad m
  => MonadPaths m where
  getDataDir :: m Path
  default getDataDir :: (MonadTrans t, MonadPaths m', m ~ t m') => m Path
  getDataDir = m' Path -> t m' Path
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m' Path
forall (m :: * -> *). MonadPaths m => m Path
getDataDir


-- ** Instances

instance MonadPaths IO where
  getDataDir :: IO Path
getDataDir = IO String -> IO Path
coerce IO String
Paths_hnix.getDataDir

deriving
  instance
    MonadPaths (t (Fix1 t))
    => MonadPaths (Fix1 t)

deriving
  instance
    MonadPaths (t (Fix1T t m) m)
    => MonadPaths (Fix1T t m)


-- * @class MonadHttp m@

class
  Monad m
  => MonadHttp m where

  getURL :: Text -> m (Either ErrorCall StorePath)
  default getURL :: (MonadTrans t, MonadHttp m', m ~ t m') => Text -> m (Either ErrorCall StorePath)
  getURL = m' (Either ErrorCall StorePath)
-> t m' (Either ErrorCall StorePath)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Either ErrorCall StorePath)
 -> t m' (Either ErrorCall StorePath))
-> (StorePathName -> m' (Either ErrorCall StorePath))
-> StorePathName
-> t m' (Either ErrorCall StorePath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePathName -> m' (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadHttp m =>
StorePathName -> m (Either ErrorCall StorePath)
getURL


-- ** Instances

instance MonadHttp IO where
  getURL :: StorePathName -> IO (Either ErrorCall StorePath)
getURL StorePathName
url = do
    let urlstr :: String
urlstr = StorePathName -> String
forall a. ToString a => a -> String
toString StorePathName
url
    String -> IO ()
forall (m :: * -> *). Monad m => String -> m ()
traceM (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"fetching HTTP URL: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
urlstr
    Request
req     <- String -> IO Request
forall (m :: * -> *). MonadThrow m => String -> m Request
parseRequest String
urlstr
    Manager
manager <-
      if Request -> RecursiveFlag
secure Request
req
        then IO Manager
forall (m :: * -> *). MonadIO m => m Manager
newTlsManager
        else ManagerSettings -> IO Manager
newManager ManagerSettings
defaultManagerSettings
    -- print req
    Response ByteString
response <- Request -> Manager -> IO (Response ByteString)
httpLbs (Request
req { method :: Method
method = Method
"GET" }) Manager
manager
    let status :: Int
status = Status -> Int
statusCode (Status -> Int) -> Status -> Int
forall a b. (a -> b) -> a -> b
$ Response ByteString -> Status
forall body. Response body -> Status
responseStatus Response ByteString
response
    pure $ ErrorCall -> Either ErrorCall StorePath
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall StorePath)
-> ErrorCall -> Either ErrorCall StorePath
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ if Int
status Int -> Int -> RecursiveFlag
forall a. Eq a => a -> a -> RecursiveFlag
/= Int
200
      then
        String
"fail, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall b a. (Show a, IsString b) => a -> b
show Int
status String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" when fetching url:" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
urlstr
      else
        -- do
        -- let bstr = responseBody response
        String
"success in downloading but hnix-store is not yet ready; url = " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
urlstr

deriving
  instance
    MonadHttp (t (Fix1 t))
    => MonadHttp (Fix1 t)

deriving
  instance
    MonadHttp (t (Fix1T t m) m)
    => MonadHttp (Fix1T t m)


-- * @class MonadPutStr m@

class
  (Monad m, MonadIO m)
  => MonadPutStr m where

  --TODO: Should this be used *only* when the Nix to be evaluated invokes a
  --`trace` operation?
  --  2021-04-01: Due to trace operation here, leaving it as String.
  putStr :: String -> m ()
  default putStr :: (MonadTrans t, MonadPutStr m', m ~ t m') => String -> m ()
  putStr = m' () -> t m' ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' () -> t m' ()) -> (String -> m' ()) -> String -> t m' ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m' ()
forall (m :: * -> *). MonadIO m => String -> m ()
Prelude.putStr


-- ** Instances

instance MonadPutStr IO where
  putStr :: String -> IO ()
putStr = String -> IO ()
forall (m :: * -> *). MonadIO m => String -> m ()
Prelude.putStr

deriving
  instance
    MonadPutStr (t (Fix1 t))
    => MonadPutStr (Fix1 t)

deriving
  instance
    MonadPutStr (t (Fix1T t m) m)
    => MonadPutStr (Fix1T t m)


-- ** Functions

putStrLn :: MonadPutStr m => String -> m ()
putStrLn :: String -> m ()
putStrLn = String -> m ()
forall (m :: * -> *). MonadPutStr m => String -> m ()
Nix.Effects.putStr (String -> m ()) -> (String -> String) -> String -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\n")

print :: (MonadPutStr m, Show a) => a -> m ()
print :: a -> m ()
print = String -> m ()
forall (m :: * -> *). MonadPutStr m => String -> m ()
putStrLn (String -> m ()) -> (a -> String) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall b a. (Show a, IsString b) => a -> b
show

-- * Store effects

-- ** Data type synonyms

type RecursiveFlag = Bool
type RepairFlag = Bool
type StorePathName = Text
type PathFilter m = Path -> m Bool
type StorePathSet = HS.HashSet StorePath

-- ** @class MonadStore m@

class
  Monad m
  => MonadStore m where

  -- | Copy the contents of a local path to the store.  The resulting store
  -- path is returned.  Note: This does not support yet support the expected
  -- `filter` function that allows excluding some files.
  addToStore :: StorePathName -> Path -> RecursiveFlag -> RepairFlag -> m (Either ErrorCall StorePath)
  default addToStore :: (MonadTrans t, MonadStore m', m ~ t m') => StorePathName -> Path -> RecursiveFlag -> RepairFlag -> m (Either ErrorCall StorePath)
  addToStore StorePathName
a Path
b RecursiveFlag
c RecursiveFlag
d = m' (Either ErrorCall StorePath)
-> t m' (Either ErrorCall StorePath)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Either ErrorCall StorePath)
 -> t m' (Either ErrorCall StorePath))
-> m' (Either ErrorCall StorePath)
-> t m' (Either ErrorCall StorePath)
forall a b. (a -> b) -> a -> b
$ StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> m' (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadStore m =>
StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
addToStore StorePathName
a Path
b RecursiveFlag
c RecursiveFlag
d

  -- | Like addToStore, but the contents written to the output path is a
  -- regular file containing the given string.
  addTextToStore' :: StorePathName -> Text -> Store.StorePathSet -> RepairFlag -> m (Either ErrorCall StorePath)
  default addTextToStore' :: (MonadTrans t, MonadStore m', m ~ t m') => StorePathName -> Text -> Store.StorePathSet -> RepairFlag -> m (Either ErrorCall StorePath)
  addTextToStore' StorePathName
a StorePathName
b StorePathSet
c RecursiveFlag
d = m' (Either ErrorCall StorePath)
-> t m' (Either ErrorCall StorePath)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Either ErrorCall StorePath)
 -> t m' (Either ErrorCall StorePath))
-> m' (Either ErrorCall StorePath)
-> t m' (Either ErrorCall StorePath)
forall a b. (a -> b) -> a -> b
$ StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> m' (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadStore m =>
StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
addTextToStore' StorePathName
a StorePathName
b StorePathSet
c RecursiveFlag
d


-- *** Instances

instance MonadStore IO where

  addToStore :: StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> IO (Either ErrorCall StorePath)
addToStore StorePathName
name Path
path RecursiveFlag
recursive RecursiveFlag
repair =
    (String -> IO (Either ErrorCall StorePath))
-> (StorePathName -> IO (Either ErrorCall StorePath))
-> Either String StorePathName
-> IO (Either ErrorCall StorePath)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
      (\ String
err -> Either ErrorCall StorePath -> IO (Either ErrorCall StorePath)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ErrorCall StorePath -> IO (Either ErrorCall StorePath))
-> Either ErrorCall StorePath -> IO (Either ErrorCall StorePath)
forall a b. (a -> b) -> a -> b
$ ErrorCall -> Either ErrorCall StorePath
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall StorePath)
-> ErrorCall -> Either ErrorCall StorePath
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"String '" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> StorePathName -> String
forall b a. (Show a, IsString b) => a -> b
show StorePathName
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"' is not a valid path name: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
err)
      (\ StorePathName
pathName ->
        do
          -- TODO: redesign the filter parameter
          (Either String StorePath, [Logger])
res <- MonadStore StorePath -> IO (Either String StorePath, [Logger])
forall a. MonadStore a -> IO (Either String a, [Logger])
Store.Remote.runStore (MonadStore StorePath -> IO (Either String StorePath, [Logger]))
-> MonadStore StorePath -> IO (Either String StorePath, [Logger])
forall a b. (a -> b) -> a -> b
$ StorePathName
-> String
-> RecursiveFlag
-> (String -> RecursiveFlag)
-> RecursiveFlag
-> MonadStore StorePath
forall a.
NamedAlgo a =>
StorePathName
-> String
-> RecursiveFlag
-> (String -> RecursiveFlag)
-> RecursiveFlag
-> MonadStore StorePath
Store.Remote.addToStore @Hash.SHA256 StorePathName
pathName (Path -> String
coerce Path
path) RecursiveFlag
recursive (RecursiveFlag -> String -> RecursiveFlag
forall a b. a -> b -> a
const RecursiveFlag
False) RecursiveFlag
repair
          (ErrorCall -> Either ErrorCall StorePath)
-> (StorePath -> Either ErrorCall StorePath)
-> Either ErrorCall StorePath
-> Either ErrorCall StorePath
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
            ErrorCall -> Either ErrorCall StorePath
forall a b. a -> Either a b
Left -- err
            (StorePath -> Either ErrorCall StorePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StorePath -> Either ErrorCall StorePath)
-> (StorePath -> StorePath)
-> StorePath
-> Either ErrorCall StorePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> StorePath
StorePath (Path -> StorePath)
-> (StorePath -> Path) -> StorePath -> StorePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Path
coerce (String -> Path) -> (StorePath -> String) -> StorePath -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConvertUtf8 String Method => Method -> String
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 @FilePath @ByteString (Method -> String) -> (StorePath -> Method) -> StorePath -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePath -> Method
Store.storePathToRawFilePath) -- store path
            (Either ErrorCall StorePath -> Either ErrorCall StorePath)
-> IO (Either ErrorCall StorePath)
-> IO (Either ErrorCall StorePath)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StorePathName
-> (Either String StorePath, [Logger])
-> IO (Either ErrorCall StorePath)
forall (m :: * -> *) a.
Monad m =>
StorePathName
-> (Either String a, [Logger]) -> m (Either ErrorCall a)
parseStoreResult StorePathName
"addToStore" (Either String StorePath, [Logger])
res
      )
      (StorePathName -> Either String StorePathName
Store.makeStorePathName StorePathName
name)

  addTextToStore' :: StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> IO (Either ErrorCall StorePath)
addTextToStore' StorePathName
name StorePathName
text StorePathSet
references RecursiveFlag
repair =
    do
      (Either String StorePath, [Logger])
res <- MonadStore StorePath -> IO (Either String StorePath, [Logger])
forall a. MonadStore a -> IO (Either String a, [Logger])
Store.Remote.runStore (MonadStore StorePath -> IO (Either String StorePath, [Logger]))
-> MonadStore StorePath -> IO (Either String StorePath, [Logger])
forall a b. (a -> b) -> a -> b
$ StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> MonadStore StorePath
Store.Remote.addTextToStore StorePathName
name StorePathName
text StorePathSet
references RecursiveFlag
repair
      (ErrorCall -> Either ErrorCall StorePath)
-> (StorePath -> Either ErrorCall StorePath)
-> Either ErrorCall StorePath
-> Either ErrorCall StorePath
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
        ErrorCall -> Either ErrorCall StorePath
forall a b. a -> Either a b
Left -- err
        (StorePath -> Either ErrorCall StorePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StorePath -> Either ErrorCall StorePath)
-> (StorePath -> StorePath)
-> StorePath
-> Either ErrorCall StorePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> StorePath
StorePath (Path -> StorePath)
-> (StorePath -> Path) -> StorePath -> StorePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Path
coerce (String -> Path) -> (StorePath -> String) -> StorePath -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConvertUtf8 String Method => Method -> String
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 @FilePath @ByteString (Method -> String) -> (StorePath -> Method) -> StorePath -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePath -> Method
Store.storePathToRawFilePath) -- path
        (Either ErrorCall StorePath -> Either ErrorCall StorePath)
-> IO (Either ErrorCall StorePath)
-> IO (Either ErrorCall StorePath)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StorePathName
-> (Either String StorePath, [Logger])
-> IO (Either ErrorCall StorePath)
forall (m :: * -> *) a.
Monad m =>
StorePathName
-> (Either String a, [Logger]) -> m (Either ErrorCall a)
parseStoreResult StorePathName
"addTextToStore" (Either String StorePath, [Logger])
res


-- ** Functions

parseStoreResult :: Monad m => Text -> (Either String a, [Store.Remote.Logger]) -> m (Either ErrorCall a)
parseStoreResult :: StorePathName
-> (Either String a, [Logger]) -> m (Either ErrorCall a)
parseStoreResult StorePathName
name (Either String a, [Logger])
res =
  Either ErrorCall a -> m (Either ErrorCall a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ErrorCall a -> m (Either ErrorCall a))
-> Either ErrorCall a -> m (Either ErrorCall a)
forall a b. (a -> b) -> a -> b
$ (String -> Either ErrorCall a)
-> (a -> Either ErrorCall a)
-> Either String a
-> Either ErrorCall a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
    (\ String
msg -> ErrorCall -> Either ErrorCall a
forall a b. a -> Either a b
Left (ErrorCall -> Either ErrorCall a)
-> ErrorCall -> Either ErrorCall a
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Failed to execute '" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> StorePathName -> String
forall a. ToString a => a -> String
toString StorePathName
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"': " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
msg String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\n" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [Logger] -> String
forall b a. (Show a, IsString b) => a -> b
show [Logger]
logs)
    a -> Either ErrorCall a
forall (f :: * -> *) a. Applicative f => a -> f a
pure -- result
    ((Either String a, [Logger]) -> Either String a
forall a b. (a, b) -> a
fst (Either String a, [Logger])
res)
 where
  logs :: [Logger]
logs = (Either String a, [Logger]) -> [Logger]
forall a b. (a, b) -> b
snd (Either String a, [Logger])
res

addTextToStore :: (Framed e m, MonadStore m) => StorePathName -> Text -> Store.StorePathSet -> RepairFlag -> m StorePath
addTextToStore :: StorePathName
-> StorePathName -> StorePathSet -> RecursiveFlag -> m StorePath
addTextToStore StorePathName
a StorePathName
b StorePathSet
c RecursiveFlag
d =
  (ErrorCall -> m StorePath)
-> (StorePath -> m StorePath)
-> Either ErrorCall StorePath
-> m StorePath
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
    ErrorCall -> m StorePath
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
    StorePath -> m StorePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (Either ErrorCall StorePath -> m StorePath)
-> m (Either ErrorCall StorePath) -> m StorePath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadStore m =>
StorePathName
-> StorePathName
-> StorePathSet
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
addTextToStore' StorePathName
a StorePathName
b StorePathSet
c RecursiveFlag
d

--  2021-10-30: NOTE: Misleading name, please rename.
-- | Add @Path@ into the Nix Store
addPath :: (Framed e m, MonadStore m) => Path -> m StorePath
addPath :: Path -> m StorePath
addPath Path
p =
  (ErrorCall -> m StorePath)
-> (StorePath -> m StorePath)
-> Either ErrorCall StorePath
-> m StorePath
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
    ErrorCall -> m StorePath
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
    StorePath -> m StorePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (Either ErrorCall StorePath -> m StorePath)
-> m (Either ErrorCall StorePath) -> m StorePath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadStore m =>
StorePathName
-> Path
-> RecursiveFlag
-> RecursiveFlag
-> m (Either ErrorCall StorePath)
addToStore (String -> StorePathName
forall a. IsString a => String -> a
fromString (String -> StorePathName) -> String -> StorePathName
forall a b. (a -> b) -> a -> b
$ (Path -> Path) -> Path -> String
coerce Path -> Path
takeFileName Path
p) Path
p RecursiveFlag
True RecursiveFlag
False

toFile_ :: (Framed e m, MonadStore m) => Path -> Text -> m StorePath
toFile_ :: Path -> StorePathName -> m StorePath
toFile_ Path
p StorePathName
contents = StorePathName
-> StorePathName -> StorePathSet -> RecursiveFlag -> m StorePath
forall e (m :: * -> *).
(Framed e m, MonadStore m) =>
StorePathName
-> StorePathName -> StorePathSet -> RecursiveFlag -> m StorePath
addTextToStore (String -> StorePathName
forall a. IsString a => String -> a
fromString (String -> StorePathName) -> String -> StorePathName
forall a b. (a -> b) -> a -> b
$ Path -> String
coerce Path
p) StorePathName
contents StorePathSet
forall a. Monoid a => a
mempty RecursiveFlag
False