Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: use pushScope to pass results from --eval -E #860

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import qualified Nix.Eval as Eval
import Nix.Fresh.Basic
import Nix.Json
import Nix.Options.Parser
import Nix.Scope
import Nix.Standard
import Nix.Thunk.Basic
import qualified Nix.Type.Env as Env
Expand Down Expand Up @@ -102,7 +103,19 @@ main = do
if evaluate opts
then do
val <- Nix.nixEvalExprLoc mpath expr
withNixContext mempty (Repl.main' $ pure val)
fromValueMay val >>= \case
-- val is an AttrsSet, use pushScope to pass it to repl
-- XXX: be explicit about this? infered via pushScope
--Just (as :: AttrSet (StdValue (StandardT (StdIdT IO)))) -> do
Just as ->
withNixContext mempty
$ pushScope as
$ Repl.main
Nothing ->
-- pass value as `input`
withNixContext mempty
$ pushScope (M.singleton "input" val)
$ Repl.main
else withNixContext mempty Repl.main

process opts mpath expr
Expand Down
17 changes: 4 additions & 13 deletions main/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

module Repl
( main
, main'
) where

import Nix hiding ( exec
Expand All @@ -30,7 +29,6 @@ import Nix.Utils
import Nix.Value.Monad (demand)

import qualified Data.List
import qualified Data.Maybe
import qualified Data.HashMap.Lazy
import Data.Text (Text)
import qualified Data.Text
Expand Down Expand Up @@ -65,13 +63,7 @@ import qualified System.IO.Error

-- | Repl entry point
main :: (MonadNix e t f m, MonadIO m, MonadMask m) => m ()
main = main' Nothing

-- | Principled version allowing to pass initial value for context.
--
-- Passed value is stored in context with "input" key.
main' :: (MonadNix e t f m, MonadIO m, MonadMask m) => Maybe (NValue t f m) -> m ()
main' iniVal = initState iniVal >>= \s -> flip evalStateT s
main = initState >>= \s -> flip evalStateT s
$ System.Console.Repline.evalRepl
banner
cmd
Expand Down Expand Up @@ -148,17 +140,16 @@ defReplConfig = ReplConfig
}

-- | Create initial IState for REPL
initState :: MonadNix e t f m => Maybe (NValue t f m) -> m (IState t f m)
initState mIni = do
initState :: MonadNix e t f m => m (IState t f m)
initState = do

builtins <- evalText "builtins"

opts :: Nix.Options <- asks (view hasLens)

pure $ IState
Nothing
(Data.HashMap.Lazy.fromList
$ ("builtins", builtins) : fmap ("input",) (Data.Maybe.maybeToList mIni))
(Data.HashMap.Lazy.fromList $ pure ("builtins", builtins))
defReplConfig
{ cfgStrict = strict opts
, cfgValues = values opts
Expand Down