{-# LANGUAGE CPP #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}

{-# OPTIONS_GHC -Wno-missing-signatures #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

module Nix.Builtins (withNixContext, builtins) where

import           Control.Comonad
import           Control.Monad
import           Control.Monad.Catch
import           Control.Monad.ListM            ( sortByM )
import           Control.Monad.Reader           ( asks )
import           Crypto.Hash
import qualified Crypto.Hash.MD5               as MD5
import qualified Crypto.Hash.SHA1              as SHA1
import qualified Crypto.Hash.SHA256            as SHA256
import qualified Crypto.Hash.SHA512            as SHA512
import qualified Data.Aeson                    as A
import           Data.Align                     ( alignWith )
import           Data.Array
import           Data.Bits
import           Data.ByteString                ( ByteString )
import qualified Data.ByteString               as B
import           Data.ByteString.Base16        as Base16
import           Data.Char                      ( isDigit )
import           Data.Fix                       ( foldFix )
import           Data.Foldable                  ( foldrM )
import qualified Data.HashMap.Lazy             as M
import           Data.List
import           Data.Maybe
import           Data.Scientific
import           Data.Set                       ( Set )
import qualified Data.Set                      as S
import           Data.Text                      ( Text )
import qualified Data.Text                     as Text
import           Data.Text.Encoding
import qualified Data.Text.Lazy                as LazyText
import qualified Data.Text.Lazy.Builder        as Builder
import           Data.These                     ( fromThese )
import qualified Data.Time.Clock.POSIX         as Time
import           Data.Traversable               ( for )
import qualified Data.Vector                   as V
import           NeatInterpolation              ( text )
import           Nix.Atoms
import           Nix.Convert
import           Nix.Effects
import           Nix.Effects.Basic              ( fetchTarball )
import qualified Nix.Eval                      as Eval
import           Nix.Exec
import           Nix.Expr.Types
import           Nix.Expr.Types.Annotated
import           Nix.Frames
import           Nix.Json
import           Nix.Normal
import           Nix.Options
import           Nix.Parser              hiding ( nixPath )
import           Nix.Render
import           Nix.Scope
import           Nix.String
import           Nix.String.Coerce
import           Nix.Utils
import           Nix.Value
import           Nix.Value.Equal
import           Nix.Value.Monad
import           Nix.XML
import           System.Nix.Base32              as Base32
import           System.FilePath
import           System.Posix.Files             ( isRegularFile
                                                , isDirectory
                                                , isSymbolicLink
                                                )
import           Text.Read
import           Text.Regex.TDFA

-- | Evaluate a nix expression in the default context
withNixContext
  :: forall e t f m r
   . (MonadNix e t f m, Has e Options)
  => Maybe FilePath
  -> m r
  -> m r
withNixContext :: Maybe FilePath -> m r -> m r
withNixContext Maybe FilePath
mpath m r
action = do
  Scopes m (NValue t f m)
base            <- m (Scopes m (NValue t f m))
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, Scoped (NValue t f m) m) =>
m (Scopes m (NValue t f m))
builtins
  Options
opts :: Options <- (e -> Options) -> m Options
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (FoldLike Options e e Options Options -> e -> Options
forall a s t b. FoldLike a s t a b -> s -> a
view FoldLike Options e e Options Options
forall a b. Has a b => Lens' a b
hasLens)
  let i :: NValue t f m
i = [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList ([NValue t f m] -> NValue t f m) -> [NValue t f m] -> NValue t f m
forall a b. (a -> b) -> a -> b
$ (FilePath -> NValue t f m) -> [FilePath] -> [NValue t f m]
forall a b. (a -> b) -> [a] -> [b]
map
        ( NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr
        (NixString -> NValue t f m)
-> (FilePath -> NixString) -> FilePath -> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
hackyMakeNixStringWithoutContext
        (Text -> NixString) -> (FilePath -> Text) -> FilePath -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Text
Text.pack
        )
        (Options -> [FilePath]
include Options
opts)
  AttrSet (NValue t f m) -> m r -> m r
forall a (m :: * -> *) r. Scoped a m => AttrSet a -> m r -> m r
pushScope (Text -> NValue t f m -> AttrSet (NValue t f m)
forall k v. Hashable k => k -> v -> HashMap k v
M.singleton Text
"__includes" NValue t f m
i) (m r -> m r) -> m r -> m r
forall a b. (a -> b) -> a -> b
$ Scopes m (NValue t f m) -> m r -> m r
forall a (m :: * -> *) r. Scoped a m => Scopes m a -> m r -> m r
pushScopes Scopes m (NValue t f m)
base (m r -> m r) -> m r -> m r
forall a b. (a -> b) -> a -> b
$ case Maybe FilePath
mpath of
    Maybe FilePath
Nothing   -> m r
action
    Just FilePath
path -> do
      FilePath -> m ()
forall (m :: * -> *). Monad m => FilePath -> m ()
traceM (FilePath -> m ()) -> FilePath -> m ()
forall a b. (a -> b) -> a -> b
$ FilePath
"Setting __cur_file = " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
forall a. Show a => a -> FilePath
show FilePath
path
      let ref :: NValue t f m
ref = FilePath -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
FilePath -> NValue t f m
nvPath FilePath
path
      AttrSet (NValue t f m) -> m r -> m r
forall a (m :: * -> *) r. Scoped a m => AttrSet a -> m r -> m r
pushScope (Text -> NValue t f m -> AttrSet (NValue t f m)
forall k v. Hashable k => k -> v -> HashMap k v
M.singleton Text
"__cur_file" NValue t f m
ref) m r
action

builtins :: (MonadNix e t f m, Scoped (NValue t f m) m)
         => m (Scopes m (NValue t f m))
builtins :: m (Scopes m (NValue t f m))
builtins = do
  NValue t f m
ref <- m (NValue t f m) -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => m v -> m v
defer (m (NValue t f m) -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ (HashMap Text (NValue t f m)
 -> HashMap Text SourcePos -> NValue t f m)
-> HashMap Text SourcePos
-> HashMap Text (NValue t f m)
-> NValue t f m
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text SourcePos
forall k v. HashMap k v
M.empty (HashMap Text (NValue t f m) -> NValue t f m)
-> m (HashMap Text (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (HashMap Text (NValue t f m))
buildMap
  [(Text, NValue t f m)]
lst <- ([(Text
"builtins", NValue t f m
ref)] [(Text, NValue t f m)]
-> [(Text, NValue t f m)] -> [(Text, NValue t f m)]
forall a. [a] -> [a] -> [a]
++) ([(Text, NValue t f m)] -> [(Text, NValue t f m)])
-> m [(Text, NValue t f m)] -> m [(Text, NValue t f m)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m [(Text, NValue t f m)]
topLevelBuiltins
  HashMap Text (NValue t f m)
-> m (Scopes m (NValue t f m)) -> m (Scopes m (NValue t f m))
forall a (m :: * -> *) r. Scoped a m => AttrSet a -> m r -> m r
pushScope ([(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [(Text, NValue t f m)]
lst) m (Scopes m (NValue t f m))
forall a (m :: * -> *). Scoped a m => m (Scopes m a)
currentScopes
 where
  buildMap :: m (HashMap Text (NValue t f m))
buildMap         = [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(Text, NValue t f m)] -> HashMap Text (NValue t f m))
-> ([Builtin (NValue t f m)] -> [(Text, NValue t f m)])
-> [Builtin (NValue t f m)]
-> HashMap Text (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Builtin (NValue t f m) -> (Text, NValue t f m))
-> [Builtin (NValue t f m)] -> [(Text, NValue t f m)]
forall a b. (a -> b) -> [a] -> [b]
map Builtin (NValue t f m) -> (Text, NValue t f m)
forall v. Builtin v -> (Text, v)
mapping ([Builtin (NValue t f m)] -> HashMap Text (NValue t f m))
-> m [Builtin (NValue t f m)] -> m (HashMap Text (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m [Builtin (NValue t f m)]
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m [Builtin (NValue t f m)]
builtinsList
  topLevelBuiltins :: m [(Text, NValue t f m)]
topLevelBuiltins = (Builtin (NValue t f m) -> (Text, NValue t f m))
-> [Builtin (NValue t f m)] -> [(Text, NValue t f m)]
forall a b. (a -> b) -> [a] -> [b]
map Builtin (NValue t f m) -> (Text, NValue t f m)
forall v. Builtin v -> (Text, v)
mapping ([Builtin (NValue t f m)] -> [(Text, NValue t f m)])
-> m [Builtin (NValue t f m)] -> m [(Text, NValue t f m)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m [Builtin (NValue t f m)]
fullBuiltinsList

  fullBuiltinsList :: m [Builtin (NValue t f m)]
fullBuiltinsList = (Builtin (NValue t f m) -> Builtin (NValue t f m))
-> [Builtin (NValue t f m)] -> [Builtin (NValue t f m)]
forall a b. (a -> b) -> [a] -> [b]
map Builtin (NValue t f m) -> Builtin (NValue t f m)
forall v. Builtin v -> Builtin v
go ([Builtin (NValue t f m)] -> [Builtin (NValue t f m)])
-> m [Builtin (NValue t f m)] -> m [Builtin (NValue t f m)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m [Builtin (NValue t f m)]
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m [Builtin (NValue t f m)]
builtinsList
   where
    go :: Builtin v -> Builtin v
go b :: Builtin v
b@(Builtin BuiltinType
TopLevel (Text, v)
_) = Builtin v
b
    go (Builtin BuiltinType
Normal (Text
name, v
builtin)) =
      BuiltinType -> (Text, v) -> Builtin v
forall v. BuiltinType -> (Text, v) -> Builtin v
Builtin BuiltinType
TopLevel (Text
"__" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name, v
builtin)

data BuiltinType = Normal | TopLevel
data Builtin v = Builtin
    { Builtin v -> BuiltinType
_kind   :: BuiltinType
    , Builtin v -> (Text, v)
mapping :: (Text, v)
    }

builtinsList :: forall e t f m . MonadNix e t f m => m [Builtin (NValue t f m)]
builtinsList :: m [Builtin (NValue t f m)]
builtinsList = [m (Builtin (NValue t f m))] -> m [Builtin (NValue t f m)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
  [ do
    NValue t f m
version <- NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Text -> NixString
principledMakeNixStringWithoutContext Text
"2.3")
    Builtin (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Builtin (NValue t f m) -> m (Builtin (NValue t f m)))
-> Builtin (NValue t f m) -> m (Builtin (NValue t f m))
forall a b. (a -> b) -> a -> b
$ BuiltinType -> (Text, NValue t f m) -> Builtin (NValue t f m)
forall v. BuiltinType -> (Text, v) -> Builtin v
Builtin BuiltinType
Normal (Text
"nixVersion", NValue t f m
version)
  , do
    NValue t f m
version <- Int -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Int
5 :: Int)
    Builtin (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Builtin (NValue t f m) -> m (Builtin (NValue t f m)))
-> Builtin (NValue t f m) -> m (Builtin (NValue t f m))
forall a b. (a -> b) -> a -> b
$ BuiltinType -> (Text, NValue t f m) -> Builtin (NValue t f m)
forall v. BuiltinType -> (Text, v) -> Builtin v
Builtin BuiltinType
Normal (Text
"langVersion", NValue t f m
version)

  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
Normal   Text
"nixPath"          m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
nixPath
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"abort"            NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
throw_ -- for now
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"add"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
add_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"addErrorContext"  NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
addErrorContext
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"all"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
all_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"any"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
any_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"attrNames"        NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
attrNames
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"attrValues"       NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
attrValues
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"baseNameOf"       NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
baseNameOf
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"bitAnd"           NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
bitAnd
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"bitOr"            NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
bitOr
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"bitXor"           NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
bitXor
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"catAttrs"         NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
catAttrs
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"compareVersions"  NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
compareVersions_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"concatLists"      NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
concatLists
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"concatMap"        NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
concatMap_
  , BuiltinType
-> Text
-> (NixString -> [NixString] -> Prim m NixString)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> Text -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   Text
"concatStringsSep" ((NixString -> [NixString] -> NixString)
-> NixString -> [NixString] -> Prim m NixString
forall a b c. (a -> b -> c) -> a -> b -> Prim m c
arity2 NixString -> [NixString] -> NixString
principledIntercalateNixString)
  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
Normal   Text
"currentSystem"    m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
currentSystem
  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
Normal   Text
"currentTime"      m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
currentTime_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"deepSeq"          NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
deepSeq

    -- This is compiled in so that we only parse and evaluate it once, at
    -- compile-time.
  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
TopLevel Text
"derivation" $(do
      let Success expr = parseNixText [text|
        drvAttrs @ { outputs ? [ "out" ], ... }:

        let

          strict = derivationStrict drvAttrs;

          commonAttrs = drvAttrs
            // (builtins.listToAttrs outputsList)
            // { all = map (x: x.value) outputsList;
                 inherit drvAttrs;
               };

          outputToAttrListElement = outputName:
            { name = outputName;
              value = commonAttrs // {
                outPath = builtins.getAttr outputName strict;
                drvPath = strict.drvPath;
                type = "derivation";
                inherit outputName;
              };
            };

          outputsList = map outputToAttrListElement outputs;

        in (builtins.head outputsList).value|]
      [| foldFix Eval.eval expr |]
    )

  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"derivationStrict" NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
derivationStrict_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"dirOf"            NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
dirOf
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"div"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
div_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"elem"             NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
elem_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"elemAt"           NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
elemAt_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"exec"             NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
exec_
  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
Normal   Text
"false"            (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> NAtom -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Bool -> NAtom
NBool Bool
False)
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"fetchTarball"     NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
fetchTarball
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"fetchurl"         NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
fetchurl
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"filter"           NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
filter_
  , BuiltinType
-> Text
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f
    -> NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add3 BuiltinType
Normal   Text
"foldl'"           NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
foldl'_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"fromJSON"         NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
fromJSON
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"functionArgs"     NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
functionArgs
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"genList"          NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
genList
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"genericClosure"   NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
genericClosure
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"getAttr"          NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
getAttr
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"getEnv"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
getEnv_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"hasAttr"          NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
hasAttr
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"hasContext"       NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
hasContext
  , BuiltinType
-> Text
-> (NixString -> NixString -> Prim m NixString)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> Text -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   Text
"hashString"       (MonadNix e t f m => NixString -> NixString -> Prim m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NixString -> NixString -> Prim m NixString
hashString @e @t @f @m)
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"head"             NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
head_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"import"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
import_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"intersectAttrs"   NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
intersectAttrs
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"isAttrs"          NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isAttrs
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"isBool"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isBool
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"isFloat"          NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isFloat
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"isFunction"       NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isFunction
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"isInt"            NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isInt
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"isList"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isList
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"isNull"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isNull
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"isString"         NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
isString
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"length"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
length_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"lessThan"         NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
lessThan
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"listToAttrs"      NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
listToAttrs
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
TopLevel Text
"map"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
map_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
TopLevel Text
"mapAttrs"         NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
mapAttrs_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"match"            NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
match_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"mul"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
mul_
  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
Normal   Text
"null"             (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant NAtom
NNull)
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"parseDrvName"     NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
parseDrvName
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"partition"        NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
partition_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"pathExists"       NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
pathExists_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"placeholder"      NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
placeHolder
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"readDir"          NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
readDir_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"readFile"         NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
readFile_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"findFile"         NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
findFile_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
TopLevel Text
"removeAttrs"      NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
removeAttrs
  , BuiltinType
-> Text
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f
    -> NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add3 BuiltinType
Normal   Text
"replaceStrings"   NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
replaceStrings
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
TopLevel Text
"scopedImport"     NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
scopedImport
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"seq"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
seq_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"sort"             NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
sort_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"split"            NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
split_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"splitVersion"     NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
splitVersion_
  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
Normal   Text
"storeDir"         (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext Text
"/nix/store")
  , BuiltinType
-> Text -> (NixString -> Prim m Int) -> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> Text -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   Text
"stringLength"     ((NixString -> Int) -> NixString -> Prim m Int
forall a b. (a -> b) -> a -> Prim m b
arity1 ((NixString -> Int) -> NixString -> Prim m Int)
-> (NixString -> Int) -> NixString -> Prim m Int
forall a b. (a -> b) -> a -> b
$ Text -> Int
Text.length (Text -> Int) -> (NixString -> Text) -> NixString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
principledStringIgnoreContext)
  , BuiltinType
-> Text
-> (Integer -> Integer -> Prim m Integer)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> Text -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   Text
"sub"              ((Integer -> Integer -> Integer)
-> Integer -> Integer -> Prim m Integer
forall a b c. (a -> b -> c) -> a -> b -> Prim m c
arity2 ((-) @Integer))
  , BuiltinType
-> Text
-> (Int -> Int -> NixString -> Prim m NixString)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> Text -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   Text
"substring"        (MonadNix e t f m => Int -> Int -> NixString -> Prim m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Int -> Int -> NixString -> Prim m NixString
substring @e @t @f @m)
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"tail"             NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
tail_
  , BuiltinType
-> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) v e.
(MonadValue v f, MonadReader e f, Has e Frames, MonadThrow f) =>
BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
Normal   Text
"true"             (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> NAtom -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Bool -> NAtom
NBool Bool
True)
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"throw"            NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
throw_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"toJSON"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
prim_toJSON
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"toFile"           NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
toFile
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"toPath"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
toPath
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
TopLevel Text
"toString"         NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
toString
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"toXML"            NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
toXML_
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
TopLevel Text
"trace"            NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
trace_
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"tryEval"          NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
tryEval
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"typeOf"           NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
typeOf
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"valueSize"        NValue t f m -> m (NValue t f m)
forall (m :: * -> *) (f :: * -> *) a t.
(MonadIntrospect m, Applicative f) =>
a -> m (NValue t f m)
getRecursiveSize
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"getContext"                 NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
getContext
  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"appendContext"              NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
appendContext

  , BuiltinType
-> Text
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
Normal   Text
"unsafeGetAttrPos"           NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
unsafeGetAttrPos
  , BuiltinType
-> Text
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall t (f :: * -> *) (f :: * -> *) e.
(MonadValue (NValue t f f) f, MonadReader e f, Has e Frames,
 MonadThrow f, MonadThunk t f (NValue t f f), Comonad f,
 Traversable f, Applicative f) =>
BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
Normal   Text
"unsafeDiscardStringContext" NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
unsafeDiscardStringContext
  ]
 where
  wrap :: BuiltinType -> Text -> v -> Builtin v
  wrap :: BuiltinType -> Text -> v -> Builtin v
wrap BuiltinType
t Text
n v
f = BuiltinType -> (Text, v) -> Builtin v
forall v. BuiltinType -> (Text, v) -> Builtin v
Builtin BuiltinType
t (Text
n, v
f)

  arity1 :: forall a b. (a -> b) -> (a -> Prim m b)
  arity1 :: (a -> b) -> a -> Prim m b
arity1 a -> b
f = m b -> Prim m b
forall (m :: * -> *) a. m a -> Prim m a
Prim (m b -> Prim m b) -> (a -> m b) -> a -> Prim m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> m b) -> (a -> b) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  arity2 :: forall a b c. (a -> b -> c) -> (a -> b -> Prim m c)
  arity2 :: (a -> b -> c) -> a -> b -> Prim m c
arity2 a -> b -> c
f = ((m c -> Prim m c
forall (m :: * -> *) a. m a -> Prim m a
Prim (m c -> Prim m c) -> (c -> m c) -> c -> Prim m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> m c
forall (f :: * -> *) a. Applicative f => a -> f a
pure) (c -> Prim m c) -> (b -> c) -> b -> Prim m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((b -> c) -> b -> Prim m c) -> (a -> b -> c) -> a -> b -> Prim m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> c
f

  mkThunk :: Text -> m v -> m v
mkThunk Text
n = m v -> m v
forall v (m :: * -> *). MonadValue v m => m v -> m v
defer (m v -> m v) -> (m v -> m v) -> m v -> m v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixLevel -> ErrorCall -> m v -> m v
forall s e (m :: * -> *) a.
(Framed e m, Exception s) =>
NixLevel -> s -> m a -> m a
withFrame
    NixLevel
Info
    (FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"While calling builtin " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Text -> FilePath
Text.unpack Text
n FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\n")

  add0 :: BuiltinType -> Text -> f v -> f (Builtin v)
add0 BuiltinType
t Text
n f v
v = BuiltinType -> Text -> v -> Builtin v
forall v. BuiltinType -> Text -> v -> Builtin v
wrap BuiltinType
t Text
n (v -> Builtin v) -> f v -> f (Builtin v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f v -> f v
forall v (m :: * -> *) e.
(MonadValue v m, MonadReader e m, Has e Frames, MonadThrow m) =>
Text -> m v -> m v
mkThunk Text
n f v
v
  add :: BuiltinType
-> Text
-> (NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add  BuiltinType
t Text
n NValue t f f -> f (NValue t f f)
v = BuiltinType -> Text -> NValue t f f -> Builtin (NValue t f f)
forall v. BuiltinType -> Text -> v -> Builtin v
wrap BuiltinType
t Text
n (NValue t f f -> Builtin (NValue t f f))
-> f (NValue t f f) -> f (Builtin (NValue t f f))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f (NValue t f f) -> f (NValue t f f)
forall v (m :: * -> *) e.
(MonadValue v m, MonadReader e m, Has e Frames, MonadThrow m) =>
Text -> m v -> m v
mkThunk Text
n (FilePath -> (NValue t f f -> f (NValue t f f)) -> f (NValue t f f)
forall (m :: * -> *) (f :: * -> *) t.
(MonadThunk t m (NValue t f m), MonadDataContext f m) =>
FilePath -> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
builtin (Text -> FilePath
Text.unpack Text
n) NValue t f f -> f (NValue t f f)
v)
  add2 :: BuiltinType
-> Text
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add2 BuiltinType
t Text
n NValue t f f -> NValue t f f -> f (NValue t f f)
v = BuiltinType -> Text -> NValue t f f -> Builtin (NValue t f f)
forall v. BuiltinType -> Text -> v -> Builtin v
wrap BuiltinType
t Text
n (NValue t f f -> Builtin (NValue t f f))
-> f (NValue t f f) -> f (Builtin (NValue t f f))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f (NValue t f f) -> f (NValue t f f)
forall v (m :: * -> *) e.
(MonadValue v m, MonadReader e m, Has e Frames, MonadThrow m) =>
Text -> m v -> m v
mkThunk Text
n (FilePath
-> (NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (NValue t f f)
forall t (m :: * -> *) (f :: * -> *).
(MonadThunk t m (NValue t f m), MonadDataContext f m) =>
FilePath
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (NValue t f m)
builtin2 (Text -> FilePath
Text.unpack Text
n) NValue t f f -> NValue t f f -> f (NValue t f f)
v)
  add3 :: BuiltinType
-> Text
-> (NValue t f f
    -> NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (Builtin (NValue t f f))
add3 BuiltinType
t Text
n NValue t f f -> NValue t f f -> NValue t f f -> f (NValue t f f)
v = BuiltinType -> Text -> NValue t f f -> Builtin (NValue t f f)
forall v. BuiltinType -> Text -> v -> Builtin v
wrap BuiltinType
t Text
n (NValue t f f -> Builtin (NValue t f f))
-> f (NValue t f f) -> f (Builtin (NValue t f f))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f (NValue t f f) -> f (NValue t f f)
forall v (m :: * -> *) e.
(MonadValue v m, MonadReader e m, Has e Frames, MonadThrow m) =>
Text -> m v -> m v
mkThunk Text
n (FilePath
-> (NValue t f f
    -> NValue t f f -> NValue t f f -> f (NValue t f f))
-> f (NValue t f f)
forall t (m :: * -> *) (f :: * -> *).
(MonadThunk t m (NValue t f m), MonadDataContext f m) =>
FilePath
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (NValue t f m)
builtin3 (Text -> FilePath
Text.unpack Text
n) NValue t f f -> NValue t f f -> NValue t f f -> f (NValue t f f)
v)

  add' :: forall a. ToBuiltin t f m a
       => BuiltinType -> Text -> a -> m (Builtin (NValue t f m))
  add' :: BuiltinType -> Text -> a -> m (Builtin (NValue t f m))
add' BuiltinType
t Text
n a
v = BuiltinType -> Text -> NValue t f m -> Builtin (NValue t f m)
forall v. BuiltinType -> Text -> v -> Builtin v
wrap BuiltinType
t Text
n (NValue t f m -> Builtin (NValue t f m))
-> m (NValue t f m) -> m (Builtin (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> m (NValue t f m) -> m (NValue t f m)
forall v (m :: * -> *) e.
(MonadValue v m, MonadReader e m, Has e Frames, MonadThrow m) =>
Text -> m v -> m v
mkThunk Text
n (FilePath -> a -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *) a.
ToBuiltin t f m a =>
FilePath -> a -> m (NValue t f m)
toBuiltin (Text -> FilePath
Text.unpack Text
n) a
v)

-- Primops

foldNixPath
  :: forall e t f m r
   . MonadNix e t f m
  => (FilePath -> Maybe String -> NixPathEntryType -> r -> m r)
  -> r
  -> m r
foldNixPath :: (FilePath -> Maybe FilePath -> NixPathEntryType -> r -> m r)
-> r -> m r
foldNixPath FilePath -> Maybe FilePath -> NixPathEntryType -> r -> m r
f r
z = do
  Maybe (NValue t f m)
mres <- Text -> m (Maybe (NValue t f m))
forall a (m :: * -> *). Scoped a m => Text -> m (Maybe a)
lookupVar Text
"__includes"
  [NixString]
dirs <- case Maybe (NValue t f m)
mres of
    Maybe (NValue t f m)
Nothing -> [NixString] -> m [NixString]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
    Just NValue t f m
v  -> NValue t f m -> (NValue t f m -> m [NixString]) -> m [NixString]
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
v ((NValue t f m -> m [NixString]) -> m [NixString])
-> (NValue t f m -> m [NixString]) -> m [NixString]
forall a b. (a -> b) -> a -> b
$ Deeper (NValue t f m) -> m [NixString]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (Deeper (NValue t f m) -> m [NixString])
-> (NValue t f m -> Deeper (NValue t f m))
-> NValue t f m
-> m [NixString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper
  Maybe FilePath
mPath <- FilePath -> m (Maybe FilePath)
forall (m :: * -> *). MonadEnv m => FilePath -> m (Maybe FilePath)
getEnvVar FilePath
"NIX_PATH"
  Maybe FilePath
mDataDir <- FilePath -> m (Maybe FilePath)
forall (m :: * -> *). MonadEnv m => FilePath -> m (Maybe FilePath)
getEnvVar FilePath
"NIX_DATA_DIR"
  FilePath
dataDir <- m FilePath
-> (FilePath -> m FilePath) -> Maybe FilePath -> m FilePath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m FilePath
forall (m :: * -> *). MonadPaths m => m FilePath
getDataDir FilePath -> m FilePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe FilePath
mDataDir
  ((Text, NixPathEntryType) -> r -> m r)
-> r -> [(Text, NixPathEntryType)] -> m r
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
foldrM (Text, NixPathEntryType) -> r -> m r
go r
z
    ([(Text, NixPathEntryType)] -> m r)
-> [(Text, NixPathEntryType)] -> m r
forall a b. (a -> b) -> a -> b
$  (NixString -> (Text, NixPathEntryType))
-> [NixString] -> [(Text, NixPathEntryType)]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> (Text, NixPathEntryType)
fromInclude (Text -> (Text, NixPathEntryType))
-> (NixString -> Text) -> NixString -> (Text, NixPathEntryType)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
principledStringIgnoreContext) [NixString]
dirs
    [(Text, NixPathEntryType)]
-> [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall a. [a] -> [a] -> [a]
++ case Maybe FilePath
mPath of
         Maybe FilePath
Nothing  -> []
         Just FilePath
str -> Text -> [(Text, NixPathEntryType)]
uriAwareSplit (FilePath -> Text
Text.pack FilePath
str)
    [(Text, NixPathEntryType)]
-> [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall a. [a] -> [a] -> [a]
++ [ Text -> (Text, NixPathEntryType)
fromInclude (Text -> (Text, NixPathEntryType))
-> Text -> (Text, NixPathEntryType)
forall a b. (a -> b) -> a -> b
$ FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ FilePath
"nix=" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
dataDir FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"/nix/corepkgs" ]
 where
  fromInclude :: Text -> (Text, NixPathEntryType)
fromInclude Text
x | Text
"://" Text -> Text -> Bool
`Text.isInfixOf` Text
x = (Text
x, NixPathEntryType
PathEntryURI)
                | Bool
otherwise                = (Text
x, NixPathEntryType
PathEntryPath)
  go :: (Text, NixPathEntryType) -> r -> m r
go (Text
x, NixPathEntryType
ty) r
rest = case Text -> Text -> [Text]
Text.splitOn Text
"=" Text
x of
    [Text
p] -> FilePath -> Maybe FilePath -> NixPathEntryType -> r -> m r
f (Text -> FilePath
Text.unpack Text
p) Maybe FilePath
forall a. Maybe a
Nothing NixPathEntryType
ty r
rest
    [Text
n, Text
p] -> FilePath -> Maybe FilePath -> NixPathEntryType -> r -> m r
f (Text -> FilePath
Text.unpack Text
p) (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just (Text -> FilePath
Text.unpack Text
n)) NixPathEntryType
ty r
rest
    [Text]
_ -> ErrorCall -> m r
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m r) -> ErrorCall -> m r
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"Unexpected entry in NIX_PATH: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Text -> FilePath
forall a. Show a => a -> FilePath
show Text
x

nixPath :: MonadNix e t f m => m (NValue t f m)
nixPath :: m (NValue t f m)
nixPath = ([NValue t f m] -> NValue t f m)
-> m [NValue t f m] -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList (m [NValue t f m] -> m (NValue t f m))
-> m [NValue t f m] -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ ((FilePath
  -> Maybe FilePath
  -> NixPathEntryType
  -> [NValue t f m]
  -> m [NValue t f m])
 -> [NValue t f m] -> m [NValue t f m])
-> [NValue t f m]
-> (FilePath
    -> Maybe FilePath
    -> NixPathEntryType
    -> [NValue t f m]
    -> m [NValue t f m])
-> m [NValue t f m]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (FilePath
 -> Maybe FilePath
 -> NixPathEntryType
 -> [NValue t f m]
 -> m [NValue t f m])
-> [NValue t f m] -> m [NValue t f m]
forall e t (f :: * -> *) (m :: * -> *) r.
MonadNix e t f m =>
(FilePath -> Maybe FilePath -> NixPathEntryType -> r -> m r)
-> r -> m r
foldNixPath [] ((FilePath
  -> Maybe FilePath
  -> NixPathEntryType
  -> [NValue t f m]
  -> m [NValue t f m])
 -> m [NValue t f m])
-> (FilePath
    -> Maybe FilePath
    -> NixPathEntryType
    -> [NValue t f m]
    -> m [NValue t f m])
-> m [NValue t f m]
forall a b. (a -> b) -> a -> b
$ \FilePath
p Maybe FilePath
mn NixPathEntryType
ty [NValue t f m]
rest ->
  [NValue t f m] -> m [NValue t f m]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    ([NValue t f m] -> m [NValue t f m])
-> [NValue t f m] -> m [NValue t f m]
forall a b. (a -> b) -> a -> b
$ (HashMap Text (NValue t f m)
 -> HashMap Text SourcePos -> NValue t f m)
-> HashMap Text SourcePos
-> HashMap Text (NValue t f m)
-> NValue t f m
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text SourcePos
forall a. Monoid a => a
mempty ( [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
        [ case NixPathEntryType
ty of
          NixPathEntryType
PathEntryPath -> (Text
"path", FilePath -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
FilePath -> NValue t f m
nvPath FilePath
p)
          NixPathEntryType
PathEntryURI ->
            ( Text
"uri"
            , NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NixString
hackyMakeNixStringWithoutContext (Text -> NixString) -> Text -> NixString
forall a b. (a -> b) -> a -> b
$ FilePath -> Text
Text.pack FilePath
p
            )

        , ( Text
"prefix"
          , NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr
            (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NixString
hackyMakeNixStringWithoutContext (Text -> NixString) -> Text -> NixString
forall a b. (a -> b) -> a -> b
$ FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
"" Maybe FilePath
mn
          )
        ]
      )
    NValue t f m -> [NValue t f m] -> [NValue t f m]
forall a. a -> [a] -> [a]
: [NValue t f m]
rest

toString :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
toString :: NValue t f m -> m (NValue t f m)
toString = (NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadStore m, MonadThrow m,
 MonadDataErrorContext t f m, MonadValue (NValue t f m) m) =>
(NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
coerceToString NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
callFunc CopyToStoreMode
DontCopyToStore CoercionLevel
CoerceAny (NValue t f m -> m NixString)
-> (NixString -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue

hasAttr
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
hasAttr :: NValue t f m -> NValue t f m -> m (NValue t f m)
hasAttr NValue t f m
x NValue t f m
y = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
x m NixString -> (NixString -> m Text) -> m Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext m Text -> (Text -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
key ->
  NValue t f m -> m (AttrSet (NValue t f m), HashMap Text SourcePos)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) NValue t f m
y
    m (AttrSet (NValue t f m), HashMap Text SourcePos)
-> ((AttrSet (NValue t f m), HashMap Text SourcePos)
    -> m (NValue t f m))
-> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(AttrSet (NValue t f m)
aset, HashMap Text SourcePos
_) -> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m)) -> Bool -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> AttrSet (NValue t f m) -> Bool
forall k a. (Eq k, Hashable k) => k -> HashMap k a -> Bool
M.member Text
key AttrSet (NValue t f m)
aset

attrsetGet :: MonadNix e t f m => Text -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet :: Text -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet Text
k AttrSet (NValue t f m)
s = case Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
k AttrSet (NValue t f m)
s of
  Just NValue t f m
v -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
v
  Maybe (NValue t f m)
Nothing ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"Attribute '" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Text -> FilePath
Text.unpack Text
k FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"' required"

hasContext :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
hasContext :: NValue t f m -> m (NValue t f m)
hasContext = Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m))
-> (NixString -> Bool) -> NixString -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Bool
stringHasContext (NixString -> m (NValue t f m))
-> (NValue t f m -> m NixString)
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

getAttr
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
getAttr :: NValue t f m -> NValue t f m -> m (NValue t f m)
getAttr NValue t f m
x NValue t f m
y = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
x m NixString -> (NixString -> m Text) -> m Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext m Text -> (Text -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
key ->
  NValue t f m -> m (AttrSet (NValue t f m), HashMap Text SourcePos)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) NValue t f m
y
    m (AttrSet (NValue t f m), HashMap Text SourcePos)
-> ((AttrSet (NValue t f m), HashMap Text SourcePos)
    -> m (NValue t f m))
-> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(AttrSet (NValue t f m)
aset, HashMap Text SourcePos
_) -> Text -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Text -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet Text
key AttrSet (NValue t f m)
aset

unsafeGetAttrPos
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
unsafeGetAttrPos :: NValue t f m -> NValue t f m -> m (NValue t f m)
unsafeGetAttrPos NValue t f m
x NValue t f m
y = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
x' -> NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
y ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
y' -> case (NValue t f m
x', NValue t f m
y') of
  (NVStr NixString
ns, NVSet AttrSet (NValue t f m)
_ HashMap Text SourcePos
apos) ->
    case Text -> HashMap Text SourcePos -> Maybe SourcePos
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup (NixString -> Text
hackyStringIgnoreContext NixString
ns) HashMap Text SourcePos
apos of
      Maybe SourcePos
Nothing    -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant NAtom
NNull
      Just SourcePos
delta -> SourcePos -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue SourcePos
delta
  (NValue t f m
x, NValue t f m
y) ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
      (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
      (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"Invalid types for builtins.unsafeGetAttrPos: "
      FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ (NValue t f m, NValue t f m) -> FilePath
forall a. Show a => a -> FilePath
show (NValue t f m
x, NValue t f m
y)

-- This function is a bit special in that it doesn't care about the contents
-- of the list.
length_
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
length_ :: NValue t f m -> m (NValue t f m)
length_ = Int -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Int -> m (NValue t f m))
-> ([NValue t f m] -> Int) -> [NValue t f m] -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([NValue t f m] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length :: [NValue t f m] -> Int) ([NValue t f m] -> m (NValue t f m))
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

add_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
add_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
add_ NValue t f m
x NValue t f m
y = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
x' -> NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
y ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
y' -> case (NValue t f m
x', NValue t f m
y') of
  (NVConstant (NInt   Integer
x), NVConstant (NInt Integer
y)  ) -> Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
y :: Integer)
  (NVConstant (NFloat Float
x), NVConstant (NInt Integer
y)  ) -> Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
+ Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
y)
  (NVConstant (NInt   Integer
x), NVConstant (NFloat Float
y)) -> Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
x Float -> Float -> Float
forall a. Num a => a -> a -> a
+ Float
y)
  (NVConstant (NFloat Float
x), NVConstant (NFloat Float
y)) -> Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
+ Float
y)
  (NValue t f m
_                    , NValue t f m
_                    ) -> ValueFrame t f m -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (NValue t f m))
-> ValueFrame t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NValue t f m -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
NValue t f m -> NValue t f m -> ValueFrame t f m
Addition NValue t f m
x' NValue t f m
y'

mul_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
mul_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
mul_ NValue t f m
x NValue t f m
y = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
x' -> NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
y ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
y' -> case (NValue t f m
x', NValue t f m
y') of
  (NVConstant (NInt   Integer
x), NVConstant (NInt Integer
y)  ) -> Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
y :: Integer)
  (NVConstant (NFloat Float
x), NVConstant (NInt Integer
y)  ) -> Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
* Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
y)
  (NVConstant (NInt   Integer
x), NVConstant (NFloat Float
y)) -> Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
x Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
y)
  (NVConstant (NFloat Float
x), NVConstant (NFloat Float
y)) -> Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
y)
  (NValue t f m
_, NValue t f m
_) -> ValueFrame t f m -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (NValue t f m))
-> ValueFrame t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NValue t f m -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
NValue t f m -> NValue t f m -> ValueFrame t f m
Multiplication NValue t f m
x' NValue t f m
y'

div_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
div_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
div_ NValue t f m
x NValue t f m
y = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
x' -> NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
y ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
y' -> case (NValue t f m
x', NValue t f m
y') of
  (NVConstant (NInt Integer
x), NVConstant (NInt Integer
y)) | Integer
y Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0 ->
    Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Double -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
floor (Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
y :: Double) :: Integer)
  (NVConstant (NFloat Float
x), NVConstant (NInt Integer
y)) | Integer
y Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0 ->
    Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Float
x Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
y)
  (NVConstant (NInt Integer
x), NVConstant (NFloat Float
y)) | Float
y Float -> Float -> Bool
forall a. Eq a => a -> a -> Bool
/= Float
0 ->
    Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
x Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
y)
  (NVConstant (NFloat Float
x), NVConstant (NFloat Float
y)) | Float
y Float -> Float -> Bool
forall a. Eq a => a -> a -> Bool
/= Float
0 -> Float -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Float
x Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
y)
  (NValue t f m
_, NValue t f m
_) -> ValueFrame t f m -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (NValue t f m))
-> ValueFrame t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NValue t f m -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
NValue t f m -> NValue t f m -> ValueFrame t f m
Division NValue t f m
x' NValue t f m
y'

anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
anyM :: (a -> m Bool) -> [a] -> m Bool
anyM a -> m Bool
_ []       = Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
anyM a -> m Bool
p (a
x : [a]
xs) = do
  Bool
q <- a -> m Bool
p a
x
  if Bool
q then Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True else (a -> m Bool) -> [a] -> m Bool
forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
anyM a -> m Bool
p [a]
xs

any_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
any_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
any_ NValue t f m
f = Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m))
-> (NValue t f m -> m Bool) -> NValue t f m -> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (NValue t f m -> m Bool) -> [NValue t f m] -> m Bool
forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
anyM NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue ([NValue t f m] -> m Bool)
-> (NValue t f m -> m [NValue t f m]) -> NValue t f m -> m Bool
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (NValue t f m -> m (NValue t f m))
-> [NValue t f m] -> m [NValue t f m]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc`) ([NValue t f m] -> m [NValue t f m])
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
allM :: (a -> m Bool) -> [a] -> m Bool
allM a -> m Bool
_ []       = Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
allM a -> m Bool
p (a
x : [a]
xs) = do
  Bool
q <- a -> m Bool
p a
x
  if Bool
q then (a -> m Bool) -> [a] -> m Bool
forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
allM a -> m Bool
p [a]
xs else Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False

all_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
all_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
all_ NValue t f m
f = Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m))
-> (NValue t f m -> m Bool) -> NValue t f m -> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (NValue t f m -> m Bool) -> [NValue t f m] -> m Bool
forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
allM NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue ([NValue t f m] -> m Bool)
-> (NValue t f m -> m [NValue t f m]) -> NValue t f m -> m Bool
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (NValue t f m -> m (NValue t f m))
-> [NValue t f m] -> m [NValue t f m]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc`) ([NValue t f m] -> m [NValue t f m])
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

foldl'_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
foldl'_ :: NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
foldl'_ NValue t f m
f NValue t f m
z NValue t f m
xs = NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @[NValue t f m] NValue t f m
xs m [NValue t f m]
-> ([NValue t f m] -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (NValue t f m -> NValue t f m -> m (NValue t f m))
-> NValue t f m -> [NValue t f m] -> m (NValue t f m)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM NValue t f m -> NValue t f m -> m (NValue t f m)
go NValue t f m
z
  where go :: NValue t f m -> NValue t f m -> m (NValue t f m)
go NValue t f m
b NValue t f m
a = NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
b m (NValue t f m)
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
a)

head_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
head_ :: NValue t f m -> m (NValue t f m)
head_ = NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \case
  []    -> ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall FilePath
"builtins.head: empty list"
  NValue t f m
h : [NValue t f m]
_ -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
h

tail_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
tail_ :: NValue t f m -> m (NValue t f m)
tail_ = NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \case
  []    -> ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall FilePath
"builtins.tail: empty list"
  NValue t f m
_ : [NValue t f m]
t -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList [NValue t f m]
t

data VersionComponent
   = VersionComponent_Pre -- ^ The string "pre"
   | VersionComponent_String Text -- ^ A string other than "pre"
   | VersionComponent_Number Integer -- ^ A number
   deriving (Int -> VersionComponent -> FilePath -> FilePath
[VersionComponent] -> FilePath -> FilePath
VersionComponent -> FilePath
(Int -> VersionComponent -> FilePath -> FilePath)
-> (VersionComponent -> FilePath)
-> ([VersionComponent] -> FilePath -> FilePath)
-> Show VersionComponent
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
showList :: [VersionComponent] -> FilePath -> FilePath
$cshowList :: [VersionComponent] -> FilePath -> FilePath
show :: VersionComponent -> FilePath
$cshow :: VersionComponent -> FilePath
showsPrec :: Int -> VersionComponent -> FilePath -> FilePath
$cshowsPrec :: Int -> VersionComponent -> FilePath -> FilePath
Show, ReadPrec [VersionComponent]
ReadPrec VersionComponent
Int -> ReadS VersionComponent
ReadS [VersionComponent]
(Int -> ReadS VersionComponent)
-> ReadS [VersionComponent]
-> ReadPrec VersionComponent
-> ReadPrec [VersionComponent]
-> Read VersionComponent
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [VersionComponent]
$creadListPrec :: ReadPrec [VersionComponent]
readPrec :: ReadPrec VersionComponent
$creadPrec :: ReadPrec VersionComponent
readList :: ReadS [VersionComponent]
$creadList :: ReadS [VersionComponent]
readsPrec :: Int -> ReadS VersionComponent
$creadsPrec :: Int -> ReadS VersionComponent
Read, VersionComponent -> VersionComponent -> Bool
(VersionComponent -> VersionComponent -> Bool)
-> (VersionComponent -> VersionComponent -> Bool)
-> Eq VersionComponent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VersionComponent -> VersionComponent -> Bool
$c/= :: VersionComponent -> VersionComponent -> Bool
== :: VersionComponent -> VersionComponent -> Bool
$c== :: VersionComponent -> VersionComponent -> Bool
Eq, Eq VersionComponent
Eq VersionComponent
-> (VersionComponent -> VersionComponent -> Ordering)
-> (VersionComponent -> VersionComponent -> Bool)
-> (VersionComponent -> VersionComponent -> Bool)
-> (VersionComponent -> VersionComponent -> Bool)
-> (VersionComponent -> VersionComponent -> Bool)
-> (VersionComponent -> VersionComponent -> VersionComponent)
-> (VersionComponent -> VersionComponent -> VersionComponent)
-> Ord VersionComponent
VersionComponent -> VersionComponent -> Bool
VersionComponent -> VersionComponent -> Ordering
VersionComponent -> VersionComponent -> VersionComponent
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: VersionComponent -> VersionComponent -> VersionComponent
$cmin :: VersionComponent -> VersionComponent -> VersionComponent
max :: VersionComponent -> VersionComponent -> VersionComponent
$cmax :: VersionComponent -> VersionComponent -> VersionComponent
>= :: VersionComponent -> VersionComponent -> Bool
$c>= :: VersionComponent -> VersionComponent -> Bool
> :: VersionComponent -> VersionComponent -> Bool
$c> :: VersionComponent -> VersionComponent -> Bool
<= :: VersionComponent -> VersionComponent -> Bool
$c<= :: VersionComponent -> VersionComponent -> Bool
< :: VersionComponent -> VersionComponent -> Bool
$c< :: VersionComponent -> VersionComponent -> Bool
compare :: VersionComponent -> VersionComponent -> Ordering
$ccompare :: VersionComponent -> VersionComponent -> Ordering
$cp1Ord :: Eq VersionComponent
Ord)

versionComponentToString :: VersionComponent -> Text
versionComponentToString :: VersionComponent -> Text
versionComponentToString = \case
  VersionComponent
VersionComponent_Pre      -> Text
"pre"
  VersionComponent_String Text
s -> Text
s
  VersionComponent_Number Integer
n -> FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
n

-- | Based on https://github.com/NixOS/nix/blob/4ee4fda521137fed6af0446948b3877e0c5db803/src/libexpr/names.cc#L44
versionComponentSeparators :: String
versionComponentSeparators :: FilePath
versionComponentSeparators = FilePath
".-"

splitVersion :: Text -> [VersionComponent]
splitVersion :: Text -> [VersionComponent]
splitVersion Text
s = case Text -> Maybe (Char, Text)
Text.uncons Text
s of
  Maybe (Char, Text)
Nothing -> []
  Just (Char
h, Text
t)
    | Char
h Char -> FilePath -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FilePath
versionComponentSeparators
    -> Text -> [VersionComponent]
splitVersion Text
t
    | Char -> Bool
isDigit Char
h
    -> let (Text
digits, Text
rest) = (Char -> Bool) -> Text -> (Text, Text)
Text.span Char -> Bool
isDigit Text
s
       in
         Integer -> VersionComponent
VersionComponent_Number
             (Integer -> Maybe Integer -> Integer
forall a. a -> Maybe a -> a
fromMaybe (FilePath -> Integer
forall a. HasCallStack => FilePath -> a
error (FilePath -> Integer) -> FilePath -> Integer
forall a b. (a -> b) -> a -> b
$ FilePath
"splitVersion: couldn't parse " FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> Text -> FilePath
forall a. Show a => a -> FilePath
show Text
digits)
             (Maybe Integer -> Integer) -> Maybe Integer -> Integer
forall a b. (a -> b) -> a -> b
$ FilePath -> Maybe Integer
forall a. Read a => FilePath -> Maybe a
readMaybe
             (FilePath -> Maybe Integer) -> FilePath -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ Text -> FilePath
Text.unpack Text
digits
             )
           VersionComponent -> [VersionComponent] -> [VersionComponent]
forall a. a -> [a] -> [a]
: Text -> [VersionComponent]
splitVersion Text
rest
    | Bool
otherwise
    -> let (Text
chars, Text
rest) = (Char -> Bool) -> Text -> (Text, Text)
Text.span
             (\Char
c -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char
c Char -> FilePath -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FilePath
versionComponentSeparators)
             Text
s
           thisComponent :: VersionComponent
thisComponent = case Text
chars of
             Text
"pre" -> VersionComponent
VersionComponent_Pre
             Text
x     -> Text -> VersionComponent
VersionComponent_String Text
x
       in  VersionComponent
thisComponent VersionComponent -> [VersionComponent] -> [VersionComponent]
forall a. a -> [a] -> [a]
: Text -> [VersionComponent]
splitVersion Text
rest

splitVersion_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
splitVersion_ :: NValue t f m -> m (NValue t f m)
splitVersion_ = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m NixString)
-> (NixString -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext (NixString -> m Text)
-> (Text -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \Text
s ->
  NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList
    ([NValue t f m] -> NValue t f m) -> [NValue t f m] -> NValue t f m
forall a b. (a -> b) -> a -> b
$ ((VersionComponent -> NValue t f m)
 -> [VersionComponent] -> [NValue t f m])
-> [VersionComponent]
-> (VersionComponent -> NValue t f m)
-> [NValue t f m]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (VersionComponent -> NValue t f m)
-> [VersionComponent] -> [NValue t f m]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> [VersionComponent]
splitVersion Text
s)
    ((VersionComponent -> NValue t f m) -> [NValue t f m])
-> (VersionComponent -> NValue t f m) -> [NValue t f m]
forall a b. (a -> b) -> a -> b
$ NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr
    (NixString -> NValue t f m)
-> (VersionComponent -> NixString)
-> VersionComponent
-> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
principledMakeNixStringWithoutContext
    (Text -> NixString)
-> (VersionComponent -> Text) -> VersionComponent -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VersionComponent -> Text
versionComponentToString

compareVersions :: Text -> Text -> Ordering
compareVersions :: Text -> Text -> Ordering
compareVersions Text
s1 Text
s2 = [Ordering] -> Ordering
forall a. Monoid a => [a] -> a
mconcat
  ([Ordering] -> Ordering) -> [Ordering] -> Ordering
forall a b. (a -> b) -> a -> b
$ (These VersionComponent VersionComponent -> Ordering)
-> [VersionComponent] -> [VersionComponent] -> [Ordering]
forall (f :: * -> *) a b c.
Semialign f =>
(These a b -> c) -> f a -> f b -> f c
alignWith These VersionComponent VersionComponent -> Ordering
f (Text -> [VersionComponent]
splitVersion Text
s1) (Text -> [VersionComponent]
splitVersion Text
s2)
 where
  z :: VersionComponent
z = Text -> VersionComponent
VersionComponent_String Text
""
  f :: These VersionComponent VersionComponent -> Ordering
f = (VersionComponent -> VersionComponent -> Ordering)
-> (VersionComponent, VersionComponent) -> Ordering
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry VersionComponent -> VersionComponent -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ((VersionComponent, VersionComponent) -> Ordering)
-> (These VersionComponent VersionComponent
    -> (VersionComponent, VersionComponent))
-> These VersionComponent VersionComponent
-> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VersionComponent
-> VersionComponent
-> These VersionComponent VersionComponent
-> (VersionComponent, VersionComponent)
forall a b. a -> b -> These a b -> (a, b)
fromThese VersionComponent
z VersionComponent
z

compareVersions_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
compareVersions_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
compareVersions_ NValue t f m
t1 NValue t f m
t2 = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
t1 m NixString -> (NixString -> m Text) -> m Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext m Text -> (Text -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
s1 ->
  NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
t2 m NixString -> (NixString -> m Text) -> m Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext m Text -> (Text -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
s2 ->
    NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> NAtom -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Integer -> NAtom
NInt (Integer -> NAtom) -> Integer -> NAtom
forall a b. (a -> b) -> a -> b
$ case Text -> Text -> Ordering
compareVersions Text
s1 Text
s2 of
      Ordering
LT -> -Integer
1
      Ordering
EQ -> Integer
0
      Ordering
GT -> Integer
1

splitDrvName :: Text -> (Text, Text)
splitDrvName :: Text -> (Text, Text)
splitDrvName Text
s =
  let
    sep :: Text
sep    = Text
"-"
    pieces :: [Text]
pieces = Text -> Text -> [Text]
Text.splitOn Text
sep Text
s
    isFirstVersionPiece :: Text -> Bool
isFirstVersionPiece Text
p = case Text -> Maybe (Char, Text)
Text.uncons Text
p of
      Just (Char
h, Text
_) | Char -> Bool
isDigit Char
h -> Bool
True
      Maybe (Char, Text)
_                       -> Bool
False
    -- Like 'break', but always puts the first item into the first result
    -- list
    breakAfterFirstItem :: (a -> Bool) -> [a] -> ([a], [a])
    breakAfterFirstItem :: (a -> Bool) -> [a] -> ([a], [a])
breakAfterFirstItem a -> Bool
f = \case
      a
h : [a]
t -> let ([a]
a, [a]
b) = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
f [a]
t in (a
h a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
a, [a]
b)
      []    -> ([], [])
    ([Text]
namePieces, [Text]
versionPieces) =
      (Text -> Bool) -> [Text] -> ([Text], [Text])
forall a. (a -> Bool) -> [a] -> ([a], [a])
breakAfterFirstItem Text -> Bool
isFirstVersionPiece [Text]
pieces
  in
    (Text -> [Text] -> Text
Text.intercalate Text
sep [Text]
namePieces, Text -> [Text] -> Text
Text.intercalate Text
sep [Text]
versionPieces)

parseDrvName
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
parseDrvName :: NValue t f m -> m (NValue t f m)
parseDrvName = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m NixString)
-> (NixString -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext (NixString -> m Text)
-> (Text -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \Text
s -> do
  let (Text
name :: Text, Text
version :: Text) = Text -> (Text, Text)
splitDrvName Text
s
  forall a (m :: * -> *) v. ToValue a m v => a -> m v
forall (m :: * -> *) v.
ToValue (AttrSet (NValue t f m)) m v =>
AttrSet (NValue t f m) -> m v
toValue @(AttrSet (NValue t f m)) (AttrSet (NValue t f m) -> m (NValue t f m))
-> AttrSet (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [(Text, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
    [ ( Text
"name" :: Text
      , NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext Text
name
      )
    , ( Text
"version"
      , NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext Text
version
      )
    ]

match_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
match_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
match_ NValue t f m
pat NValue t f m
str = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
pat m NixString -> (NixString -> m Text) -> m Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext m Text -> (Text -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
p ->
  NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
str m NixString -> (NixString -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \NixString
ns -> do
        -- NOTE: Currently prim_match in nix/src/libexpr/primops.cc ignores the
        -- context of its second argument. This is probably a bug but we're
        -- going to preserve the behavior here until it is fixed upstream.
        -- Relevant issue: https://github.com/NixOS/nix/issues/2547
    let s :: Text
s  = NixString -> Text
principledStringIgnoreContext NixString
ns

    let re :: Regex
re = ByteString -> Regex
forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
source -> regex
makeRegex (Text -> ByteString
encodeUtf8 Text
p) :: Regex
    let mkMatch :: Text -> m v
mkMatch Text
t
          | Text -> Bool
Text.null Text
t = () -> m v
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue ()
          | -- Shorthand for Null
            Bool
otherwise   = NixString -> m v
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m v) -> NixString -> m v
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext Text
t
    case Regex
-> ByteString
-> Maybe (ByteString, MatchText ByteString, ByteString)
forall regex source.
RegexLike regex source =>
regex -> source -> Maybe (source, MatchText source, source)
matchOnceText Regex
re (Text -> ByteString
encodeUtf8 Text
s) of
      Just (ByteString
"", MatchText ByteString
sarr, ByteString
"") -> do
        let s :: [ByteString]
s = ((ByteString, (Int, Int)) -> ByteString)
-> [(ByteString, (Int, Int))] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (ByteString, (Int, Int)) -> ByteString
forall a b. (a, b) -> a
fst (MatchText ByteString -> [(ByteString, (Int, Int))]
forall i e. Array i e -> [e]
elems MatchText ByteString
sarr)
        [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList ([NValue t f m] -> NValue t f m)
-> m [NValue t f m] -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> m (NValue t f m))
-> [ByteString] -> m [NValue t f m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (Text -> m (NValue t f m)
forall (m :: * -> *) v.
(ToValue () m v, ToValue NixString m v) =>
Text -> m v
mkMatch (Text -> m (NValue t f m))
-> (ByteString -> Text) -> ByteString -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
decodeUtf8)
                            (if [ByteString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ByteString]
s Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then [ByteString] -> [ByteString]
forall a. [a] -> [a]
tail [ByteString]
s else [ByteString]
s)
      Maybe (ByteString, MatchText ByteString, ByteString)
_ -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant NAtom
NNull

split_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
split_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
split_ NValue t f m
pat NValue t f m
str = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
pat m NixString -> (NixString -> m Text) -> m Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext m Text -> (Text -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
p ->
  NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
str m NixString -> (NixString -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \NixString
ns -> do
        -- NOTE: Currently prim_split in nix/src/libexpr/primops.cc ignores the
        -- context of its second argument. This is probably a bug but we're
        -- going to preserve the behavior here until it is fixed upstream.
        -- Relevant issue: https://github.com/NixOS/nix/issues/2547
    let s :: Text
s = NixString -> Text
principledStringIgnoreContext NixString
ns
    let re :: Regex
re       = ByteString -> Regex
forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
source -> regex
makeRegex (Text -> ByteString
encodeUtf8 Text
p) :: Regex
        haystack :: ByteString
haystack = Text -> ByteString
encodeUtf8 Text
s
    NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList ([NValue t f m] -> NValue t f m) -> [NValue t f m] -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Int -> [[(ByteString, (Int, Int))]] -> ByteString -> [NValue t f m]
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Int -> [[(ByteString, (Int, Int))]] -> ByteString -> [NValue t f m]
splitMatches Int
0
                                   ((MatchText ByteString -> [(ByteString, (Int, Int))])
-> [MatchText ByteString] -> [[(ByteString, (Int, Int))]]
forall a b. (a -> b) -> [a] -> [b]
map MatchText ByteString -> [(ByteString, (Int, Int))]
forall i e. Array i e -> [e]
elems ([MatchText ByteString] -> [[(ByteString, (Int, Int))]])
-> [MatchText ByteString] -> [[(ByteString, (Int, Int))]]
forall a b. (a -> b) -> a -> b
$ Regex -> ByteString -> [MatchText ByteString]
forall regex source.
RegexLike regex source =>
regex -> source -> [MatchText source]
matchAllText Regex
re ByteString
haystack)
                                   ByteString
haystack

splitMatches
  :: forall e t f m
   . MonadNix e t f m
  => Int
  -> [[(ByteString, (Int, Int))]]
  -> ByteString
  -> [NValue t f m]
splitMatches :: Int -> [[(ByteString, (Int, Int))]] -> ByteString -> [NValue t f m]
splitMatches Int
_ [] ByteString
haystack = [ByteString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
ByteString -> NValue t f m
thunkStr ByteString
haystack]
splitMatches Int
_ ([] : [[(ByteString, (Int, Int))]]
_) ByteString
_ =
  FilePath -> [NValue t f m]
forall a. HasCallStack => FilePath -> a
error FilePath
"Error in splitMatches: this should never happen!"
splitMatches Int
numDropped (((ByteString
_, (Int
start, Int
len)) : [(ByteString, (Int, Int))]
captures) : [[(ByteString, (Int, Int))]]
mts) ByteString
haystack =
  ByteString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
ByteString -> NValue t f m
thunkStr ByteString
before NValue t f m -> [NValue t f m] -> [NValue t f m]
forall a. a -> [a] -> [a]
: NValue t f m
caps NValue t f m -> [NValue t f m] -> [NValue t f m]
forall a. a -> [a] -> [a]
: Int -> [[(ByteString, (Int, Int))]] -> ByteString -> [NValue t f m]
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Int -> [[(ByteString, (Int, Int))]] -> ByteString -> [NValue t f m]
splitMatches (Int
numDropped Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
relStart Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len)
                                        [[(ByteString, (Int, Int))]]
mts
                                        (Int -> ByteString -> ByteString
B.drop Int
len ByteString
rest)
 where
  relStart :: Int
relStart       = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
start Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
numDropped
  (ByteString
before, ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
relStart ByteString
haystack
  caps :: NValue t f m
caps           = [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList (((ByteString, (Int, Int)) -> NValue t f m)
-> [(ByteString, (Int, Int))] -> [NValue t f m]
forall a b. (a -> b) -> [a] -> [b]
map (ByteString, (Int, Int)) -> NValue t f m
forall a (f :: * -> *) b t (m :: * -> *).
(Ord a, Num a, Applicative f) =>
(ByteString, (a, b)) -> NValue t f m
f [(ByteString, (Int, Int))]
captures)
  f :: (ByteString, (a, b)) -> NValue t f m
f (ByteString
a, (a
s, b
_)) = if a
s a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant NAtom
NNull else ByteString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
ByteString -> NValue t f m
thunkStr ByteString
a

thunkStr :: ByteString -> NValue t f m
thunkStr ByteString
s = NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (Text -> NixString
hackyMakeNixStringWithoutContext (ByteString -> Text
decodeUtf8 ByteString
s))

substring :: forall e t f m. MonadNix e t f m => Int -> Int -> NixString -> Prim m NixString
substring :: Int -> Int -> NixString -> Prim m NixString
substring Int
start Int
len NixString
str = m NixString -> Prim m NixString
forall (m :: * -> *) a. m a -> Prim m a
Prim (m NixString -> Prim m NixString)
-> m NixString -> Prim m NixString
forall a b. (a -> b) -> a -> b
$ if Int
start Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 --NOTE: negative values of 'len' are OK
  then
    ErrorCall -> m NixString
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
    (ErrorCall -> m NixString) -> ErrorCall -> m NixString
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
    (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.substring: negative start position: "
    FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
start
  else NixString -> m NixString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NixString -> m NixString) -> NixString -> m NixString
forall a b. (a -> b) -> a -> b
$ (Text -> Text) -> NixString -> NixString
principledModifyNixContents (Int -> Text -> Text
Text.take Int
len (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
Text.drop Int
start) NixString
str

attrNames
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
attrNames :: NValue t f m -> m (NValue t f m)
attrNames =
  forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue (AttrSet (NValue t f m)) m v =>
v -> m (AttrSet (NValue t f m))
fromValue @(AttrSet (NValue t f m))
    (NValue t f m -> m (AttrSet (NValue t f m)))
-> (AttrSet (NValue t f m) -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (Deeper (NValue t f m) -> NValue t f m)
-> m (Deeper (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Deeper (NValue t f m) -> NValue t f m
forall a. Deeper a -> a
getDeeper
    (m (Deeper (NValue t f m)) -> m (NValue t f m))
-> (AttrSet (NValue t f m) -> m (Deeper (NValue t f m)))
-> AttrSet (NValue t f m)
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   [NixString] -> m (Deeper (NValue t f m))
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
    ([NixString] -> m (Deeper (NValue t f m)))
-> (AttrSet (NValue t f m) -> [NixString])
-> AttrSet (NValue t f m)
-> m (Deeper (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   (Text -> NixString) -> [Text] -> [NixString]
forall a b. (a -> b) -> [a] -> [b]
map Text -> NixString
principledMakeNixStringWithoutContext
    ([Text] -> [NixString])
-> (AttrSet (NValue t f m) -> [Text])
-> AttrSet (NValue t f m)
-> [NixString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   [Text] -> [Text]
forall a. Ord a => [a] -> [a]
sort
    ([Text] -> [Text])
-> (AttrSet (NValue t f m) -> [Text])
-> AttrSet (NValue t f m)
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   AttrSet (NValue t f m) -> [Text]
forall k v. HashMap k v -> [k]
M.keys

attrValues
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
attrValues :: NValue t f m -> m (NValue t f m)
attrValues =
  forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue (AttrSet (NValue t f m)) m v =>
v -> m (AttrSet (NValue t f m))
fromValue @(AttrSet (NValue t f m))
    (NValue t f m -> m (AttrSet (NValue t f m)))
-> (AttrSet (NValue t f m) -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
    ([NValue t f m] -> m (NValue t f m))
-> (AttrSet (NValue t f m) -> [NValue t f m])
-> AttrSet (NValue t f m)
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   ((Text, NValue t f m) -> NValue t f m)
-> [(Text, NValue t f m)] -> [NValue t f m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text, NValue t f m) -> NValue t f m
forall a b. (a, b) -> b
snd
    ([(Text, NValue t f m)] -> [NValue t f m])
-> (AttrSet (NValue t f m) -> [(Text, NValue t f m)])
-> AttrSet (NValue t f m)
-> [NValue t f m]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   ((Text, NValue t f m) -> Text)
-> [(Text, NValue t f m)] -> [(Text, NValue t f m)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn ((Text, NValue t f m) -> Text
forall a b. (a, b) -> a
fst @Text @(NValue t f m))
    ([(Text, NValue t f m)] -> [(Text, NValue t f m)])
-> (AttrSet (NValue t f m) -> [(Text, NValue t f m)])
-> AttrSet (NValue t f m)
-> [(Text, NValue t f m)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   AttrSet (NValue t f m) -> [(Text, NValue t f m)]
forall k v. HashMap k v -> [(k, v)]
M.toList

map_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
map_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
map_ NValue t f m
f =
  [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
    ([NValue t f m] -> m (NValue t f m))
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (NValue t f m -> m (NValue t f m))
-> [NValue t f m] -> m [NValue t f m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
          ( forall v (m :: * -> *). MonadValue v m => m v -> m v
forall (m :: * -> *).
MonadValue (NValue t f m) m =>
m (NValue t f m) -> m (NValue t f m)
defer @(NValue t f m)
          (m (NValue t f m) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixLevel -> ErrorCall -> m (NValue t f m) -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s) =>
NixLevel -> s -> m a -> m a
withFrame NixLevel
Debug (FilePath -> ErrorCall
ErrorCall FilePath
"While applying f in map:\n")
          (m (NValue t f m) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc`)
          )
    ([NValue t f m] -> m [NValue t f m])
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m]

mapAttrs_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
mapAttrs_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
mapAttrs_ NValue t f m
f NValue t f m
xs = NValue t f m -> m (AttrSet (NValue t f m))
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m)) NValue t f m
xs m (AttrSet (NValue t f m))
-> (AttrSet (NValue t f m) -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \AttrSet (NValue t f m)
aset -> do
  let pairs :: [(Text, NValue t f m)]
pairs = AttrSet (NValue t f m) -> [(Text, NValue t f m)]
forall k v. HashMap k v -> [(k, v)]
M.toList AttrSet (NValue t f m)
aset
  [NValue t f m]
values <- [(Text, NValue t f m)]
-> ((Text, NValue t f m) -> m (NValue t f m)) -> m [NValue t f m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [(Text, NValue t f m)]
pairs (((Text, NValue t f m) -> m (NValue t f m)) -> m [NValue t f m])
-> ((Text, NValue t f m) -> m (NValue t f m)) -> m [NValue t f m]
forall a b. (a -> b) -> a -> b
$ \(Text
key, NValue t f m
value) ->
    forall v (m :: * -> *). MonadValue v m => m v -> m v
forall (m :: * -> *).
MonadValue (NValue t f m) m =>
m (NValue t f m) -> m (NValue t f m)
defer @(NValue t f m)
      (m (NValue t f m) -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$   NixLevel -> ErrorCall -> m (NValue t f m) -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s) =>
NixLevel -> s -> m a -> m a
withFrame NixLevel
Debug (FilePath -> ErrorCall
ErrorCall FilePath
"While applying f in mapAttrs:\n")
      (m (NValue t f m) -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$   NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
callFunc (NValue t f m -> NValue t f m -> m (NValue t f m))
-> NValue t f m -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => f (a -> b) -> a -> f b
?? NValue t f m
value
      (NValue t f m -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
callFunc NValue t f m
f (NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (Text -> NixString
principledMakeNixStringWithoutContext Text
key))
  AttrSet (NValue t f m) -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (AttrSet (NValue t f m) -> m (NValue t f m))
-> ([NValue t f m] -> AttrSet (NValue t f m))
-> [NValue t f m]
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(Text, NValue t f m)] -> AttrSet (NValue t f m))
-> ([NValue t f m] -> [(Text, NValue t f m)])
-> [NValue t f m]
-> AttrSet (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [NValue t f m] -> [(Text, NValue t f m)]
forall a b. [a] -> [b] -> [(a, b)]
zip (((Text, NValue t f m) -> Text) -> [(Text, NValue t f m)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, NValue t f m) -> Text
forall a b. (a, b) -> a
fst [(Text, NValue t f m)]
pairs) ([NValue t f m] -> m (NValue t f m))
-> [NValue t f m] -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [NValue t f m]
values

filter_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
filter_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
filter_ NValue t f m
f =
  [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
    ([NValue t f m] -> m (NValue t f m))
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (NValue t f m -> m Bool) -> [NValue t f m] -> m [NValue t f m]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM (NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m Bool)
-> (NValue t f m -> m (NValue t f m)) -> NValue t f m -> m Bool
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
callFunc NValue t f m
f)
    ([NValue t f m] -> m [NValue t f m])
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

catAttrs
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
catAttrs :: NValue t f m -> NValue t f m -> m (NValue t f m)
catAttrs NValue t f m
attrName NValue t f m
xs = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
attrName m NixString -> (NixString -> m Text) -> m Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext m Text -> (Text -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
n ->
  NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @[NValue t f m] NValue t f m
xs m [NValue t f m]
-> ([NValue t f m] -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[NValue t f m]
l ->
    ([Maybe (NValue t f m)] -> NValue t f m)
-> m [Maybe (NValue t f m)] -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList ([NValue t f m] -> NValue t f m)
-> ([Maybe (NValue t f m)] -> [NValue t f m])
-> [Maybe (NValue t f m)]
-> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (NValue t f m)] -> [NValue t f m]
forall a. [Maybe a] -> [a]
catMaybes)
      (m [Maybe (NValue t f m)] -> m (NValue t f m))
-> m [Maybe (NValue t f m)] -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [NValue t f m]
-> (NValue t f m -> m (Maybe (NValue t f m)))
-> m [Maybe (NValue t f m)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [NValue t f m]
l
      ((NValue t f m -> m (Maybe (NValue t f m)))
 -> m [Maybe (NValue t f m)])
-> (NValue t f m -> m (Maybe (NValue t f m)))
-> m [Maybe (NValue t f m)]
forall a b. (a -> b) -> a -> b
$ (HashMap Text (NValue t f m) -> Maybe (NValue t f m))
-> m (HashMap Text (NValue t f m)) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> HashMap Text (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
n)
      (m (HashMap Text (NValue t f m)) -> m (Maybe (NValue t f m)))
-> (NValue t f m -> m (HashMap Text (NValue t f m)))
-> NValue t f m
-> m (Maybe (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NValue t f m
 -> (NValue t f m -> m (HashMap Text (NValue t f m)))
 -> m (HashMap Text (NValue t f m)))
-> (NValue t f m -> m (HashMap Text (NValue t f m)))
-> NValue t f m
-> m (HashMap Text (NValue t f m))
forall a b c. (a -> b -> c) -> b -> a -> c
flip NValue t f m
-> (NValue t f m -> m (HashMap Text (NValue t f m)))
-> m (HashMap Text (NValue t f m))
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m -> m (HashMap Text (NValue t f m))
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

baseNameOf :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
baseNameOf :: NValue t f m -> m (NValue t f m)
baseNameOf NValue t f m
x = do
  NixString
ns <- (NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadStore m, MonadThrow m,
 MonadDataErrorContext t f m, MonadValue (NValue t f m) m) =>
(NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
coerceToString NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
callFunc CopyToStoreMode
DontCopyToStore CoercionLevel
CoerceStringy NValue t f m
x
  NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr
    ((Text -> Text) -> NixString -> NixString
principledModifyNixContents (FilePath -> Text
Text.pack (FilePath -> Text) -> (Text -> FilePath) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
takeFileName (FilePath -> FilePath) -> (Text -> FilePath) -> Text -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> FilePath
Text.unpack) NixString
ns)

bitAnd
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
bitAnd :: NValue t f m -> NValue t f m -> m (NValue t f m)
bitAnd NValue t f m
x NValue t f m
y =
  NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
x m Integer -> (Integer -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Integer
a -> NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
y m Integer -> (Integer -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Integer
b -> Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer
a Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
b)

bitOr
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
bitOr :: NValue t f m -> NValue t f m -> m (NValue t f m)
bitOr NValue t f m
x NValue t f m
y =
  NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
x m Integer -> (Integer -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Integer
a -> NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
y m Integer -> (Integer -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Integer
b -> Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer
a Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
b)

bitXor
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
bitXor :: NValue t f m -> NValue t f m -> m (NValue t f m)
bitXor NValue t f m
x NValue t f m
y = NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
x
  m Integer -> (Integer -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Integer
a -> NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
y m Integer -> (Integer -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Integer
b -> Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer
a Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
`xor` Integer
b)

dirOf :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
dirOf :: NValue t f m -> m (NValue t f m)
dirOf NValue t f m
x = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \case
  NVStr NixString
ns -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr
    ((Text -> Text) -> NixString -> NixString
principledModifyNixContents (FilePath -> Text
Text.pack (FilePath -> Text) -> (Text -> FilePath) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
takeDirectory (FilePath -> FilePath) -> (Text -> FilePath) -> Text -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> FilePath
Text.unpack) NixString
ns)
  NVPath FilePath
path -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
FilePath -> NValue t f m
nvPath (FilePath -> NValue t f m) -> FilePath -> NValue t f m
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
takeDirectory FilePath
path
  NValue t f m
v ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"dirOf: expected string or path, got " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
v

-- jww (2018-04-28): This should only be a string argument, and not coerced?
unsafeDiscardStringContext
  :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
unsafeDiscardStringContext :: NValue t f m -> m (NValue t f m)
unsafeDiscardStringContext NValue t f m
mnv = do
  NixString
ns <- NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
mnv
  NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext (Text -> NixString) -> Text -> NixString
forall a b. (a -> b) -> a -> b
$ NixString -> Text
principledStringIgnoreContext
    NixString
ns

seq_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
seq_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
seq_ NValue t f m
a NValue t f m
b = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
a ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
_ -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
b

-- | We evaluate 'a' only for its effects, so data cycles are ignored.
deepSeq
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
deepSeq :: NValue t f m -> NValue t f m -> m (NValue t f m)
deepSeq NValue t f m
a NValue t f m
b = NValue t f m
b NValue t f m -> m () -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ NValue t f m -> m ()
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadThunk t m (NValue t f m),
 MonadDataErrorContext t f m, Ord (ThunkId m)) =>
NValue t f m -> m ()
normalForm_ NValue t f m
a

elem_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
elem_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
elem_ NValue t f m
x = Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m))
-> (NValue t f m -> m Bool) -> NValue t f m -> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (NValue t f m -> m Bool) -> [NValue t f m] -> m Bool
forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
anyM (NValue t f m -> NValue t f m -> m Bool
forall t (f :: * -> *) (m :: * -> *).
(MonadThunk t m (NValue t f m), Comonad f) =>
NValue t f m -> NValue t f m -> m Bool
valueEqM NValue t f m
x) ([NValue t f m] -> m Bool)
-> (NValue t f m -> m [NValue t f m]) -> NValue t f m -> m Bool
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

elemAt :: [a] -> Int -> Maybe a
elemAt :: [a] -> Int -> Maybe a
elemAt [a]
ls Int
i = case Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
i [a]
ls of
  []    -> Maybe a
forall a. Maybe a
Nothing
  a
a : [a]
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
a

elemAt_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
elemAt_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
elemAt_ NValue t f m
xs NValue t f m
n = NValue t f m -> m Int
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
n m Int -> (Int -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
n' -> NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
xs m [NValue t f m]
-> ([NValue t f m] -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[NValue t f m]
xs' ->
  case [NValue t f m] -> Int -> Maybe (NValue t f m)
forall a. [a] -> Int -> Maybe a
elemAt [NValue t f m]
xs' Int
n' of
    Just NValue t f m
a -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
a
    Maybe (NValue t f m)
Nothing ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
        (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
        (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.elem: Index "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
n'
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" too large for list of length "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show ([NValue t f m] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NValue t f m]
xs')

genList
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
genList :: NValue t f m -> NValue t f m -> m (NValue t f m)
genList NValue t f m
f = forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v. FromValue Integer m v => v -> m Integer
fromValue @Integer (NValue t f m -> m Integer)
-> (Integer -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \Integer
n -> if Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0
  then [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue ([NValue t f m] -> m (NValue t f m))
-> m [NValue t f m] -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Integer] -> (Integer -> m (NValue t f m)) -> m [NValue t f m]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Integer
0 .. Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1] (\Integer
i -> m (NValue t f m) -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => m v -> m v
defer (m (NValue t f m) -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ (NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc`) (NValue t f m -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue Integer
i)
  else
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
    (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
    (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.genList: Expected a non-negative number, got "
    FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
n

-- We wrap values solely to provide an Ord instance for genericClosure
newtype WValue t f m = WValue (NValue t f m)

instance Comonad f => Eq (WValue t f m) where
  WValue (NVConstant (NFloat Float
x)) == :: WValue t f m -> WValue t f m -> Bool
== WValue (NVConstant (NInt Integer
y)) =
    Float
x Float -> Float -> Bool
forall a. Eq a => a -> a -> Bool
== Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
y
  WValue (NVConstant (NInt   Integer
x)) == WValue (NVConstant (NFloat Float
y)) =
    Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
x Float -> Float -> Bool
forall a. Eq a => a -> a -> Bool
== Float
y
  WValue (NVConstant (NInt   Integer
x)) == WValue (NVConstant (NInt   Integer
y)) = Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
y
  WValue (NVConstant (NFloat Float
x)) == WValue (NVConstant (NFloat Float
y)) = Float
x Float -> Float -> Bool
forall a. Eq a => a -> a -> Bool
== Float
y
  WValue (NVPath     FilePath
x         ) == WValue (NVPath     FilePath
y         ) = FilePath
x FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
y
  WValue (NVStr NixString
x) == WValue (NVStr NixString
y) =
    NixString -> Text
hackyStringIgnoreContext NixString
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== NixString -> Text
hackyStringIgnoreContext NixString
y
  WValue t f m
_ == WValue t f m
_ = Bool
False

instance Comonad f => Ord (WValue t f m) where
  WValue (NVConstant (NFloat Float
x)) <= :: WValue t f m -> WValue t f m -> Bool
<= WValue (NVConstant (NInt Integer
y)) =
    Float
x Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
y
  WValue (NVConstant (NInt   Integer
x)) <= WValue (NVConstant (NFloat Float
y)) =
    Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
x Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
<= Float
y
  WValue (NVConstant (NInt   Integer
x)) <= WValue (NVConstant (NInt   Integer
y)) = Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
y
  WValue (NVConstant (NFloat Float
x)) <= WValue (NVConstant (NFloat Float
y)) = Float
x Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
<= Float
y
  WValue (NVPath     FilePath
x         ) <= WValue (NVPath     FilePath
y         ) = FilePath
x FilePath -> FilePath -> Bool
forall a. Ord a => a -> a -> Bool
<= FilePath
y
  WValue (NVStr NixString
x) <= WValue (NVStr NixString
y) =
    NixString -> Text
hackyStringIgnoreContext NixString
x Text -> Text -> Bool
forall a. Ord a => a -> a -> Bool
<= NixString -> Text
hackyStringIgnoreContext NixString
y
  WValue t f m
_ <= WValue t f m
_ = Bool
False

genericClosure
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
genericClosure :: NValue t f m -> m (NValue t f m)
genericClosure = forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue (AttrSet (NValue t f m)) m v =>
v -> m (AttrSet (NValue t f m))
fromValue @(AttrSet (NValue t f m)) (NValue t f m -> m (AttrSet (NValue t f m)))
-> (AttrSet (NValue t f m) -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \AttrSet (NValue t f m)
s ->
  case (Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
"startSet" AttrSet (NValue t f m)
s, Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
"operator" AttrSet (NValue t f m)
s) of
    (Maybe (NValue t f m)
Nothing, Maybe (NValue t f m)
Nothing) ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
        (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
        (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.genericClosure: "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"Attributes 'startSet' and 'operator' required"
    (Maybe (NValue t f m)
Nothing, Just NValue t f m
_) ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
        (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall
        (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"builtins.genericClosure: Attribute 'startSet' required"
    (Just NValue t f m
_, Maybe (NValue t f m)
Nothing) ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
        (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall
        (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"builtins.genericClosure: Attribute 'operator' required"
    (Just NValue t f m
startSet, Just NValue t f m
operator) ->
      NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
startSet ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m] (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \[NValue t f m]
ss ->
        NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
operator ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
op -> forall a (m :: * -> *) v. ToValue a m v => a -> m v
forall (m :: * -> *) v.
ToValue [NValue t f m] m v =>
[NValue t f m] -> m v
toValue @[NValue t f m] ([NValue t f m] -> m (NValue t f m))
-> m [NValue t f m] -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (Set (WValue t f m), [NValue t f m]) -> [NValue t f m]
forall a b. (a, b) -> b
snd ((Set (WValue t f m), [NValue t f m]) -> [NValue t f m])
-> m (Set (WValue t f m), [NValue t f m]) -> m [NValue t f m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NValue t f m
-> [NValue t f m]
-> Set (WValue t f m)
-> m (Set (WValue t f m), [NValue t f m])
go NValue t f m
op [NValue t f m]
ss Set (WValue t f m)
forall a. Set a
S.empty
 where
  go
    :: NValue t f m
    -> [NValue t f m]
    -> Set (WValue t f m)
    -> m (Set (WValue t f m), [NValue t f m])
  go :: NValue t f m
-> [NValue t f m]
-> Set (WValue t f m)
-> m (Set (WValue t f m), [NValue t f m])
go NValue t f m
_  []       Set (WValue t f m)
ks = (Set (WValue t f m), [NValue t f m])
-> m (Set (WValue t f m), [NValue t f m])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set (WValue t f m)
ks, [])
  go NValue t f m
op (NValue t f m
t : [NValue t f m]
ts) Set (WValue t f m)
ks = NValue t f m
-> (NValue t f m -> m (Set (WValue t f m), [NValue t f m]))
-> m (Set (WValue t f m), [NValue t f m])
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
t ((NValue t f m -> m (Set (WValue t f m), [NValue t f m]))
 -> m (Set (WValue t f m), [NValue t f m]))
-> (NValue t f m -> m (Set (WValue t f m), [NValue t f m]))
-> m (Set (WValue t f m), [NValue t f m])
forall a b. (a -> b) -> a -> b
$ \NValue t f m
v -> NValue t f m -> m (AttrSet (NValue t f m))
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m)) NValue t f m
v m (AttrSet (NValue t f m))
-> (AttrSet (NValue t f m)
    -> m (Set (WValue t f m), [NValue t f m]))
-> m (Set (WValue t f m), [NValue t f m])
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \AttrSet (NValue t f m)
s -> do
    NValue t f m
k <- Text -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Text -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet Text
"key" AttrSet (NValue t f m)
s
    NValue t f m
-> (NValue t f m -> m (Set (WValue t f m), [NValue t f m]))
-> m (Set (WValue t f m), [NValue t f m])
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
k ((NValue t f m -> m (Set (WValue t f m), [NValue t f m]))
 -> m (Set (WValue t f m), [NValue t f m]))
-> (NValue t f m -> m (Set (WValue t f m), [NValue t f m]))
-> m (Set (WValue t f m), [NValue t f m])
forall a b. (a -> b) -> a -> b
$ \NValue t f m
k' -> do
      if WValue t f m -> Set (WValue t f m) -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member (NValue t f m -> WValue t f m
forall t (f :: * -> *) (m :: * -> *). NValue t f m -> WValue t f m
WValue NValue t f m
k') Set (WValue t f m)
ks
        then NValue t f m
-> [NValue t f m]
-> Set (WValue t f m)
-> m (Set (WValue t f m), [NValue t f m])
go NValue t f m
op [NValue t f m]
ts Set (WValue t f m)
ks
        else do
          [NValue t f m]
ys <- forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m] (NValue t f m -> m [NValue t f m])
-> m (NValue t f m) -> m [NValue t f m]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (NValue t f m
op NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
v)
          case Set (WValue t f m) -> [WValue t f m]
forall a. Set a -> [a]
S.toList Set (WValue t f m)
ks of
            []           -> NValue t f m -> NValue t f m -> m ()
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadDataErrorContext t f m) =>
NValue t f m -> NValue t f m -> m ()
checkComparable NValue t f m
k' NValue t f m
k'
            WValue NValue t f m
j : [WValue t f m]
_ -> NValue t f m -> NValue t f m -> m ()
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadDataErrorContext t f m) =>
NValue t f m -> NValue t f m -> m ()
checkComparable NValue t f m
k' NValue t f m
j
          ([NValue t f m] -> [NValue t f m])
-> (Set (WValue t f m), [NValue t f m])
-> (Set (WValue t f m), [NValue t f m])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NValue t f m
t NValue t f m -> [NValue t f m] -> [NValue t f m]
forall a. a -> [a] -> [a]
:) ((Set (WValue t f m), [NValue t f m])
 -> (Set (WValue t f m), [NValue t f m]))
-> m (Set (WValue t f m), [NValue t f m])
-> m (Set (WValue t f m), [NValue t f m])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NValue t f m
-> [NValue t f m]
-> Set (WValue t f m)
-> m (Set (WValue t f m), [NValue t f m])
go NValue t f m
op ([NValue t f m]
ts [NValue t f m] -> [NValue t f m] -> [NValue t f m]
forall a. [a] -> [a] -> [a]
++ [NValue t f m]
ys) (WValue t f m -> Set (WValue t f m) -> Set (WValue t f m)
forall a. Ord a => a -> Set a -> Set a
S.insert (NValue t f m -> WValue t f m
forall t (f :: * -> *) (m :: * -> *). NValue t f m -> WValue t f m
WValue NValue t f m
k') Set (WValue t f m)
ks)

replaceStrings
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
replaceStrings :: NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
replaceStrings NValue t f m
tfrom NValue t f m
tto NValue t f m
ts = Deeper (NValue t f m) -> m [NixString]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper NValue t f m
tfrom) m [NixString]
-> ([NixString] -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \([NixString]
nsFrom :: [NixString]) ->
  Deeper (NValue t f m) -> m [NixString]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper NValue t f m
tto) m [NixString]
-> ([NixString] -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \([NixString]
nsTo :: [NixString]) ->
    NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
ts m NixString -> (NixString -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(NixString
ns :: NixString) -> do
      let from :: [Text]
from = (NixString -> Text) -> [NixString] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map NixString -> Text
principledStringIgnoreContext [NixString]
nsFrom
      Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([NixString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NixString]
nsFrom Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [NixString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NixString]
nsTo)
        (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$  ErrorCall -> m ()
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
        (ErrorCall -> m ()) -> ErrorCall -> m ()
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
        (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"'from' and 'to' arguments to 'replaceStrings'"
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" have different lengths"
      let
        lookupPrefix :: Text -> Maybe (Text, NixString, Text)
lookupPrefix Text
s = do
          (Text
prefix, NixString
replacement) <- ((Text, NixString) -> Bool)
-> [(Text, NixString)] -> Maybe (Text, NixString)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Text -> Text -> Bool
`Text.isPrefixOf` Text
s) (Text -> Bool)
-> ((Text, NixString) -> Text) -> (Text, NixString) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, NixString) -> Text
forall a b. (a, b) -> a
fst)
            ([(Text, NixString)] -> Maybe (Text, NixString))
-> [(Text, NixString)] -> Maybe (Text, NixString)
forall a b. (a -> b) -> a -> b
$ [Text] -> [NixString] -> [(Text, NixString)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Text]
from [NixString]
nsTo
          let rest :: Text
rest = Int -> Text -> Text
Text.drop (Text -> Int
Text.length Text
prefix) Text
s
          (Text, NixString, Text) -> Maybe (Text, NixString, Text)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
prefix, NixString
replacement, Text
rest)
        finish :: Builder -> HashSet StringContext -> NixString
finish Builder
b =
          Text -> HashSet StringContext -> NixString
principledMakeNixString (Text -> Text
LazyText.toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Builder -> Text
Builder.toLazyText Builder
b)
        go :: Text -> Builder -> HashSet StringContext -> NixString
go Text
orig Builder
result HashSet StringContext
ctx = case Text -> Maybe (Text, NixString, Text)
lookupPrefix Text
orig of
          Maybe (Text, NixString, Text)
Nothing -> case Text -> Maybe (Char, Text)
Text.uncons Text
orig of
            Maybe (Char, Text)
Nothing     -> Builder -> HashSet StringContext -> NixString
finish Builder
result HashSet StringContext
ctx
            Just (Char
h, Text
t) -> Text -> Builder -> HashSet StringContext -> NixString
go Text
t (Builder
result Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Char -> Builder
Builder.singleton Char
h) HashSet StringContext
ctx
          Just (Text
prefix, NixString
replacementNS, Text
rest) ->
            let replacement :: Text
replacement = NixString -> Text
principledStringIgnoreContext NixString
replacementNS
                newCtx :: HashSet StringContext
newCtx      = NixString -> HashSet StringContext
principledGetContext NixString
replacementNS
            in  case Text
prefix of
                  Text
"" -> case Text -> Maybe (Char, Text)
Text.uncons Text
rest of
                    Maybe (Char, Text)
Nothing -> Builder -> HashSet StringContext -> NixString
finish
                      (Builder
result Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Builder.fromText Text
replacement)
                      (HashSet StringContext
ctx HashSet StringContext
-> HashSet StringContext -> HashSet StringContext
forall a. Semigroup a => a -> a -> a
<> HashSet StringContext
newCtx)
                    Just (Char
h, Text
t) -> Text -> Builder -> HashSet StringContext -> NixString
go
                      Text
t
                      ([Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
                        [ Builder
result
                        , Text -> Builder
Builder.fromText Text
replacement
                        , Char -> Builder
Builder.singleton Char
h
                        ]
                      )
                      (HashSet StringContext
ctx HashSet StringContext
-> HashSet StringContext -> HashSet StringContext
forall a. Semigroup a => a -> a -> a
<> HashSet StringContext
newCtx)
                  Text
_ -> Text -> Builder -> HashSet StringContext -> NixString
go Text
rest
                          (Builder
result Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
Builder.fromText Text
replacement)
                          (HashSet StringContext
ctx HashSet StringContext
-> HashSet StringContext -> HashSet StringContext
forall a. Semigroup a => a -> a -> a
<> HashSet StringContext
newCtx)
      NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
        (NixString -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> Builder -> HashSet StringContext -> NixString
go (NixString -> Text
principledStringIgnoreContext NixString
ns) Builder
forall a. Monoid a => a
mempty
        (HashSet StringContext -> NixString)
-> HashSet StringContext -> NixString
forall a b. (a -> b) -> a -> b
$ NixString -> HashSet StringContext
principledGetContext NixString
ns

removeAttrs
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
removeAttrs :: NValue t f m -> NValue t f m -> m (NValue t f m)
removeAttrs NValue t f m
set = Deeper (NValue t f m) -> m [NixString]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (Deeper (NValue t f m) -> m [NixString])
-> (NValue t f m -> Deeper (NValue t f m))
-> NValue t f m
-> m [NixString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper (NValue t f m -> m [NixString])
-> ([NixString] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \([NixString]
nsToRemove :: [NixString]) ->
  NValue t f m -> m (AttrSet (NValue t f m), HashMap Text SourcePos)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) NValue t f m
set m (AttrSet (NValue t f m), HashMap Text SourcePos)
-> ((AttrSet (NValue t f m), HashMap Text SourcePos)
    -> m (NValue t f m))
-> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(AttrSet (NValue t f m)
m, HashMap Text SourcePos
p) -> do
    [Text]
toRemove <- (NixString -> m Text) -> [NixString] -> m [Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext [NixString]
nsToRemove
    (AttrSet (NValue t f m), HashMap Text SourcePos)
-> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (AttrSet (NValue t f m) -> [Text] -> AttrSet (NValue t f m)
forall v. HashMap Text v -> [Text] -> HashMap Text v
go AttrSet (NValue t f m)
m [Text]
toRemove, HashMap Text SourcePos -> [Text] -> HashMap Text SourcePos
forall v. HashMap Text v -> [Text] -> HashMap Text v
go HashMap Text SourcePos
p [Text]
toRemove)
  where go :: HashMap Text v -> [Text] -> HashMap Text v
go = (HashMap Text v -> Text -> HashMap Text v)
-> HashMap Text v -> [Text] -> HashMap Text v
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((Text -> HashMap Text v -> HashMap Text v)
-> HashMap Text v -> Text -> HashMap Text v
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> HashMap Text v -> HashMap Text v
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
M.delete)

intersectAttrs
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
intersectAttrs :: NValue t f m -> NValue t f m -> m (NValue t f m)
intersectAttrs NValue t f m
set1 NValue t f m
set2 =
  NValue t f m -> m (AttrSet (NValue t f m), HashMap Text SourcePos)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) NValue t f m
set1 m (AttrSet (NValue t f m), HashMap Text SourcePos)
-> ((AttrSet (NValue t f m), HashMap Text SourcePos)
    -> m (NValue t f m))
-> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(AttrSet (NValue t f m)
s1, HashMap Text SourcePos
p1) ->
    NValue t f m -> m (AttrSet (NValue t f m), HashMap Text SourcePos)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) NValue t f m
set2 m (AttrSet (NValue t f m), HashMap Text SourcePos)
-> ((AttrSet (NValue t f m), HashMap Text SourcePos)
    -> m (NValue t f m))
-> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(AttrSet (NValue t f m)
s2, HashMap Text SourcePos
p2) ->
      NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ AttrSet (NValue t f m) -> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet (AttrSet (NValue t f m)
s2 AttrSet (NValue t f m)
-> AttrSet (NValue t f m) -> AttrSet (NValue t f m)
forall k v w.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k w -> HashMap k v
`M.intersection` AttrSet (NValue t f m)
s1) (HashMap Text SourcePos
p2 HashMap Text SourcePos
-> HashMap Text SourcePos -> HashMap Text SourcePos
forall k v w.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k w -> HashMap k v
`M.intersection` HashMap Text SourcePos
p1)

functionArgs
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
functionArgs :: NValue t f m -> m (NValue t f m)
functionArgs NValue t f m
fun = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
fun ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \case
  NVClosure Params ()
p NValue t f m -> m (NValue t f m)
_ ->
    forall a (m :: * -> *) v. ToValue a m v => a -> m v
forall (m :: * -> *) v.
ToValue (AttrSet (NValue t f m)) m v =>
AttrSet (NValue t f m) -> m v
toValue @(AttrSet (NValue t f m)) (AttrSet (NValue t f m) -> m (NValue t f m))
-> AttrSet (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> (Bool -> NAtom) -> Bool -> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> NAtom
NBool (Bool -> NValue t f m)
-> HashMap Text Bool -> AttrSet (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Params ()
p of
      Param Text
name     -> Text -> Bool -> HashMap Text Bool
forall k v. Hashable k => k -> v -> HashMap k v
M.singleton Text
name Bool
False
      ParamSet ParamSet ()
s Bool
_ Maybe Text
_ -> Maybe () -> Bool
forall a. Maybe a -> Bool
isJust (Maybe () -> Bool) -> HashMap Text (Maybe ()) -> HashMap Text Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParamSet () -> HashMap Text (Maybe ())
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ParamSet ()
s
  NValue t f m
v ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
      (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
      (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.functionArgs: expected function, got "
      FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
v

toFile
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
toFile :: NValue t f m -> NValue t f m -> m (NValue t f m)
toFile NValue t f m
name NValue t f m
s = do
  Text
name' <- NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext (NixString -> m Text) -> m NixString -> m Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
name
  NixString
s'    <- NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
s
  -- TODO Using hacky here because we still need to turn the context into
  -- runtime references of the resulting file.
  -- See prim_toFile in nix/src/libexpr/primops.cc
  StorePath
mres  <- FilePath -> FilePath -> m StorePath
forall e (m :: * -> *).
(Framed e m, MonadStore m) =>
FilePath -> FilePath -> m StorePath
toFile_ (Text -> FilePath
Text.unpack Text
name')
                   (Text -> FilePath
Text.unpack (Text -> FilePath) -> Text -> FilePath
forall a b. (a -> b) -> a -> b
$ NixString -> Text
hackyStringIgnoreContext NixString
s')
  let t :: Text
t  = FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ StorePath -> FilePath
unStorePath StorePath
mres
      sc :: StringContext
sc = Text -> ContextFlavor -> StringContext
StringContext Text
t ContextFlavor
DirectPath
  NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> StringContext -> NixString
principledMakeNixStringWithSingletonContext Text
t StringContext
sc

toPath :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
toPath :: NValue t f m -> m (NValue t f m)
toPath = forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v. FromValue Path m v => v -> m Path
fromValue @Path (NValue t f m -> m Path)
-> (Path -> m (NValue t f m)) -> NValue t f m -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a (m :: * -> *) v. ToValue a m v => a -> m v
forall (m :: * -> *) v. ToValue Path m v => Path -> m v
toValue @Path

pathExists_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
pathExists_ :: NValue t f m -> m (NValue t f m)
pathExists_ NValue t f m
path = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
path ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \case
  NVPath FilePath
p  -> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m)) -> m Bool -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> m Bool
forall (m :: * -> *). MonadFile m => FilePath -> m Bool
pathExists FilePath
p
  NVStr  NixString
ns -> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m)) -> m Bool -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> m Bool
forall (m :: * -> *). MonadFile m => FilePath -> m Bool
pathExists (Text -> FilePath
Text.unpack (NixString -> Text
hackyStringIgnoreContext NixString
ns))
  NValue t f m
v ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
      (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
      (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.pathExists: expected path, got "
      FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
v

hasKind
  :: forall a e t f m
   . (MonadNix e t f m, FromValue a m (NValue t f m))
  => NValue t f m
  -> m (NValue t f m)
hasKind :: NValue t f m -> m (NValue t f m)
hasKind = NValue t f m -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (NValue t f m -> m (Maybe a))
-> (Maybe a -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Bool -> m (NValue t f m))
-> (Maybe a -> Bool) -> Maybe a -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
  Just (a
_ :: a) -> Bool
True
  Maybe a
_             -> Bool
False

isAttrs
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isAttrs :: NValue t f m -> m (NValue t f m)
isAttrs = forall a e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue a m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m,
 FromValue (AttrSet (NValue t f m)) m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @(AttrSet (NValue t f m))

isList
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isList :: NValue t f m -> m (NValue t f m)
isList = forall a e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue a m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue [NValue t f m] m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @[NValue t f m]

isString
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isString :: NValue t f m -> m (NValue t f m)
isString = forall a e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue a m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue NixString m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @NixString

isInt
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isInt :: NValue t f m -> m (NValue t f m)
isInt = forall a e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue a m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue Int m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @Int

isFloat
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isFloat :: NValue t f m -> m (NValue t f m)
isFloat = forall a e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue a m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue Float m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @Float

isBool
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isBool :: NValue t f m -> m (NValue t f m)
isBool = forall a e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue a m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue Bool m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @Bool

isNull
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isNull :: NValue t f m -> m (NValue t f m)
isNull = forall a e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue a m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, FromValue () m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @()

isFunction :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
isFunction :: NValue t f m -> m (NValue t f m)
isFunction NValue t f m
func = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
func ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \case
  NVClosure{} -> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue Bool
True
  NValue t f m
_           -> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue Bool
False

throw_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
throw_ :: NValue t f m -> m (NValue t f m)
throw_ NValue t f m
mnv = do
  NixString
ns <- (NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadStore m, MonadThrow m,
 MonadDataErrorContext t f m, MonadValue (NValue t f m) m) =>
(NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
coerceToString NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
callFunc CopyToStoreMode
CopyToStore CoercionLevel
CoerceStringy NValue t f m
mnv
  ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m))
-> (Text -> ErrorCall) -> Text -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> (Text -> FilePath) -> Text -> ErrorCall
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> FilePath
Text.unpack (Text -> m (NValue t f m)) -> Text -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> Text
principledStringIgnoreContext NixString
ns

import_
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
import_ :: NValue t f m -> m (NValue t f m)
import_ = NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
scopedImport (HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text (NValue t f m)
forall k v. HashMap k v
M.empty HashMap Text SourcePos
forall k v. HashMap k v
M.empty)

scopedImport
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
scopedImport :: NValue t f m -> NValue t f m -> m (NValue t f m)
scopedImport NValue t f m
asetArg NValue t f m
pathArg = NValue t f m -> m (AttrSet (NValue t f m))
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m)) NValue t f m
asetArg m (AttrSet (NValue t f m))
-> (AttrSet (NValue t f m) -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \AttrSet (NValue t f m)
s ->
  NValue t f m -> m Path
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
pathArg m Path -> (Path -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(Path FilePath
p) -> do
    FilePath
path  <- FilePath -> m FilePath
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
FilePath -> m FilePath
pathToDefaultNix @t @f @m FilePath
p
    Maybe (NValue t f m)
mres  <- Text -> m (Maybe (NValue t f m))
forall a (m :: * -> *). Scoped a m => Text -> m (Maybe a)
lookupVar Text
"__cur_file"
    FilePath
path' <- case Maybe (NValue t f m)
mres of
      Maybe (NValue t f m)
Nothing -> do
        FilePath -> m ()
forall (m :: * -> *). Monad m => FilePath -> m ()
traceM FilePath
"No known current directory"
        FilePath -> m FilePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure FilePath
path
      Just NValue t f m
p -> NValue t f m -> (NValue t f m -> m FilePath) -> m FilePath
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
p ((NValue t f m -> m FilePath) -> m FilePath)
-> (NValue t f m -> m FilePath) -> m FilePath
forall a b. (a -> b) -> a -> b
$ NValue t f m -> m Path
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m Path)
-> (Path -> m FilePath) -> NValue t f m -> m FilePath
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \(Path FilePath
p') -> do
        FilePath -> m ()
forall (m :: * -> *). Monad m => FilePath -> m ()
traceM (FilePath -> m ()) -> FilePath -> m ()
forall a b. (a -> b) -> a -> b
$ FilePath
"Current file being evaluated is: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
forall a. Show a => a -> FilePath
show FilePath
p'
        FilePath -> m FilePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FilePath -> m FilePath) -> FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
takeDirectory FilePath
p' FilePath -> FilePath -> FilePath
</> FilePath
path
    forall a (m :: * -> *) r. Scoped a m => m r -> m r
forall (m :: * -> *) r. Scoped (NValue t f m) m => m r -> m r
clearScopes @(NValue t f m)
      (m (NValue t f m) -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Maybe FilePath -> m (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *) r.
(MonadNix e t f m, Has e Options) =>
Maybe FilePath -> m r -> m r
withNixContext (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
path')
      (m (NValue t f m) -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ AttrSet (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
forall a (m :: * -> *) r. Scoped a m => AttrSet a -> m r -> m r
pushScope AttrSet (NValue t f m)
s
      (m (NValue t f m) -> m (NValue t f m))
-> m (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
FilePath -> m (NValue t f m)
importPath @t @f @m FilePath
path'

getEnv_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
getEnv_ :: NValue t f m -> m (NValue t f m)
getEnv_ = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m NixString)
-> (NixString -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext (NixString -> m Text)
-> (Text -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \Text
s -> do
  Maybe FilePath
mres <- FilePath -> m (Maybe FilePath)
forall (m :: * -> *). MonadEnv m => FilePath -> m (Maybe FilePath)
getEnvVar (Text -> FilePath
Text.unpack Text
s)
  NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext (Text -> NixString) -> Text -> NixString
forall a b. (a -> b) -> a -> b
$ Text -> (FilePath -> Text) -> Maybe FilePath -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" FilePath -> Text
Text.pack Maybe FilePath
mres

sort_
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
sort_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
sort_ NValue t f m
comp = NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (NValue t f m -> NValue t f m -> m Ordering)
-> [NValue t f m] -> m [NValue t f m]
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m Ordering) -> [a] -> m [a]
sortByM (NValue t f m -> NValue t f m -> NValue t f m -> m Ordering
forall e (m :: * -> *) t (f :: * -> *).
(MonadReader e m, MonadValue (NValue t f m) m,
 HasCitations1 m (NValue t f m) f, HasCitations m (NValue t f m) t,
 Traversable f, Comonad f, MonadThunk t m (NValue t f m),
 MonadEffects t f m, Alternative m, MonadCatch m, MonadFix m,
 Scoped (NValue t f m) m, Applicative f, Has e Frames,
 Has e Options, Has e SrcSpan, Show t, Typeable f, Typeable m,
 Typeable t) =>
NValue t f m -> NValue t f m -> NValue t f m -> m Ordering
cmp NValue t f m
comp) ([NValue t f m] -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> [NValue t f m]
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
 where
  cmp :: NValue t f m -> NValue t f m -> NValue t f m -> m Ordering
cmp NValue t f m
f NValue t f m
a NValue t f m
b = do
    NValue t f m
isLessThan <- NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
a m (NValue t f m)
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
b)
    NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
isLessThan m Bool -> (Bool -> m Ordering) -> m Ordering
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Bool
True  -> Ordering -> m Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure Ordering
LT
      Bool
False -> do
        NValue t f m
isGreaterThan <- NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
b m (NValue t f m)
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
a)
        NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
isGreaterThan m Bool -> (Bool -> Ordering) -> m Ordering
forall (f :: * -> *) a c. Functor f => f a -> (a -> c) -> f c
<&> \case
          Bool
True  -> Ordering
GT
          Bool
False -> Ordering
EQ

lessThan
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
lessThan :: NValue t f m -> NValue t f m -> m (NValue t f m)
lessThan NValue t f m
ta NValue t f m
tb = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
ta ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
va -> NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
tb ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
vb -> do
  let badType :: m Bool
badType =
        ErrorCall -> m Bool
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
          (ErrorCall -> m Bool) -> ErrorCall -> m Bool
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
          (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.lessThan: expected two numbers or two strings, "
          FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"got "
          FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
va
          FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" and "
          FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
vb
  NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> (Bool -> NAtom) -> Bool -> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> NAtom
NBool (Bool -> NValue t f m) -> m Bool -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case (NValue t f m
va, NValue t f m
vb) of
    (NVConstant NAtom
ca, NVConstant NAtom
cb) -> case (NAtom
ca, NAtom
cb) of
      (NInt   Integer
a, NInt Integer
b  ) -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
b
      (NFloat Float
a, NInt Integer
b  ) -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Float
a Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
b
      (NInt   Integer
a, NFloat Float
b) -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
a Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
< Float
b
      (NFloat Float
a, NFloat Float
b) -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Float
a Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
< Float
b
      (NAtom, NAtom)
_                    -> m Bool
badType
    (NVStr NixString
a, NVStr NixString
b) ->
      Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ NixString -> Text
principledStringIgnoreContext NixString
a Text -> Text -> Bool
forall a. Ord a => a -> a -> Bool
< NixString -> Text
principledStringIgnoreContext NixString
b
    (NValue t f m, NValue t f m)
_ -> m Bool
badType

concatLists
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
concatLists :: NValue t f m -> m (NValue t f m)
concatLists =
  forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m]
    (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (NValue t f m -> m [NValue t f m])
-> [NValue t f m] -> m [[NValue t f m]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((NValue t f m
 -> (NValue t f m -> m [NValue t f m]) -> m [NValue t f m])
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall a b c. (a -> b -> c) -> b -> a -> c
flip NValue t f m
-> (NValue t f m -> m [NValue t f m]) -> m [NValue t f m]
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand ((NValue t f m -> m [NValue t f m])
 -> NValue t f m -> m [NValue t f m])
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m] (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [NValue t f m] -> m [NValue t f m]
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
    ([NValue t f m] -> m [[NValue t f m]])
-> ([[NValue t f m]] -> m (NValue t f m))
-> [NValue t f m]
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
    ([NValue t f m] -> m (NValue t f m))
-> ([[NValue t f m]] -> [NValue t f m])
-> [[NValue t f m]]
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   [[NValue t f m]] -> [NValue t f m]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat

concatMap_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
concatMap_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
concatMap_ NValue t f m
f =
  forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m]
    (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (NValue t f m -> m [NValue t f m])
-> [NValue t f m] -> m [[NValue t f m]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NValue t f m -> m [NValue t f m]
applyFunc
    ([NValue t f m] -> m [[NValue t f m]])
-> ([[NValue t f m]] -> m (NValue t f m))
-> [NValue t f m]
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue ([NValue t f m] -> m (NValue t f m))
-> ([[NValue t f m]] -> [NValue t f m])
-> [[NValue t f m]]
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[NValue t f m]] -> [NValue t f m]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
  where
    applyFunc :: NValue t f m  -> m [NValue t f m]
    applyFunc :: NValue t f m -> m [NValue t f m]
applyFunc =  (NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc`) (NValue t f m -> m (NValue t f m))
-> (NValue t f m -> m [NValue t f m])
-> NValue t f m
-> m [NValue t f m]
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

listToAttrs
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
listToAttrs :: NValue t f m -> m (NValue t f m)
listToAttrs = forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m] (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \[NValue t f m]
l ->
  ([(Text, NValue t f m)] -> NValue t f m)
-> m [(Text, NValue t f m)] -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((HashMap Text (NValue t f m)
 -> HashMap Text SourcePos -> NValue t f m)
-> HashMap Text SourcePos
-> HashMap Text (NValue t f m)
-> NValue t f m
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text SourcePos
forall k v. HashMap k v
M.empty (HashMap Text (NValue t f m) -> NValue t f m)
-> ([(Text, NValue t f m)] -> HashMap Text (NValue t f m))
-> [(Text, NValue t f m)]
-> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(Text, NValue t f m)] -> HashMap Text (NValue t f m))
-> ([(Text, NValue t f m)] -> [(Text, NValue t f m)])
-> [(Text, NValue t f m)]
-> HashMap Text (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, NValue t f m)] -> [(Text, NValue t f m)]
forall a. [a] -> [a]
reverse)
    (m [(Text, NValue t f m)] -> m (NValue t f m))
-> m [(Text, NValue t f m)] -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$   [NValue t f m]
-> (NValue t f m -> m (Text, NValue t f m))
-> m [(Text, NValue t f m)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [NValue t f m]
l
    ((NValue t f m -> m (Text, NValue t f m))
 -> m [(Text, NValue t f m)])
-> (NValue t f m -> m (Text, NValue t f m))
-> m [(Text, NValue t f m)]
forall a b. (a -> b) -> a -> b
$   (NValue t f m
 -> (NValue t f m -> m (Text, NValue t f m))
 -> m (Text, NValue t f m))
-> (NValue t f m -> m (Text, NValue t f m))
-> NValue t f m
-> m (Text, NValue t f m)
forall a b c. (a -> b -> c) -> b -> a -> c
flip NValue t f m
-> (NValue t f m -> m (Text, NValue t f m))
-> m (Text, NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand
    ((NValue t f m -> m (Text, NValue t f m))
 -> NValue t f m -> m (Text, NValue t f m))
-> (NValue t f m -> m (Text, NValue t f m))
-> NValue t f m
-> m (Text, NValue t f m)
forall a b. (a -> b) -> a -> b
$   forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue (HashMap Text (NValue t f m)) m v =>
v -> m (HashMap Text (NValue t f m))
fromValue @(AttrSet (NValue t f m))
    (NValue t f m -> m (HashMap Text (NValue t f m)))
-> (HashMap Text (NValue t f m) -> m (Text, NValue t f m))
-> NValue t f m
-> m (Text, NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \HashMap Text (NValue t f m)
s -> do
          NValue t f m
t <- Text -> HashMap Text (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Text -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet Text
"name" HashMap Text (NValue t f m)
s
          NValue t f m
-> (NValue t f m -> m (Text, NValue t f m))
-> m (Text, NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
t ((NValue t f m -> m (Text, NValue t f m))
 -> m (Text, NValue t f m))
-> (NValue t f m -> m (Text, NValue t f m))
-> m (Text, NValue t f m)
forall a b. (a -> b) -> a -> b
$ NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m NixString)
-> (NixString -> m (Text, NValue t f m))
-> NValue t f m
-> m (Text, NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \NixString
n -> do
            Text
name <- NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext NixString
n
            NValue t f m
val  <- Text -> HashMap Text (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Text -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet Text
"value" HashMap Text (NValue t f m)
s
            (Text, NValue t f m) -> m (Text, NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
name, NValue t f m
val)

-- prim_hashString from nix/src/libexpr/primops.cc
-- fail if context in the algo arg
-- propagate context from the s arg
hashString
  :: forall e t f m. MonadNix e t f m => NixString -> NixString -> Prim m NixString
hashString :: NixString -> NixString -> Prim m NixString
hashString NixString
nsAlgo NixString
ns = m NixString -> Prim m NixString
forall (m :: * -> *) a. m a -> Prim m a
Prim (m NixString -> Prim m NixString)
-> m NixString -> Prim m NixString
forall a b. (a -> b) -> a -> b
$ do
  Text
algo <- NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext NixString
nsAlgo
  let f :: (Text -> Text) -> m NixString
f Text -> Text
g = NixString -> m NixString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NixString -> m NixString) -> NixString -> m NixString
forall a b. (a -> b) -> a -> b
$ (Text -> Text) -> NixString -> NixString
principledModifyNixContents Text -> Text
g NixString
ns
  case Text
algo of
    Text
"md5" ->
      (Text -> Text) -> m NixString
f ((Text -> Text) -> m NixString) -> (Text -> Text) -> m NixString
forall a b. (a -> b) -> a -> b
$ \Text
s ->
                FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ MD5 -> FilePath
forall a. Show a => a -> FilePath
show (ByteString -> MD5
forall a. HashAlgorithm a => ByteString -> a
hash (Text -> ByteString
encodeUtf8 Text
s) :: MD5.MD5)
    Text
"sha1" ->
      (Text -> Text) -> m NixString
f ((Text -> Text) -> m NixString) -> (Text -> Text) -> m NixString
forall a b. (a -> b) -> a -> b
$ \Text
s ->
                FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ SHA1 -> FilePath
forall a. Show a => a -> FilePath
show (ByteString -> SHA1
forall a. HashAlgorithm a => ByteString -> a
hash (Text -> ByteString
encodeUtf8 Text
s) :: SHA1.SHA1)
    Text
"sha256" ->
      (Text -> Text) -> m NixString
f ((Text -> Text) -> m NixString) -> (Text -> Text) -> m NixString
forall a b. (a -> b) -> a -> b
$ \Text
s ->
                FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ SHA256 -> FilePath
forall a. Show a => a -> FilePath
show (ByteString -> SHA256
forall a. HashAlgorithm a => ByteString -> a
hash (Text -> ByteString
encodeUtf8 Text
s) :: SHA256.SHA256)
    Text
"sha512" ->
      (Text -> Text) -> m NixString
f ((Text -> Text) -> m NixString) -> (Text -> Text) -> m NixString
forall a b. (a -> b) -> a -> b
$ \Text
s ->
                FilePath -> Text
Text.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ SHA512 -> FilePath
forall a. Show a => a -> FilePath
show (ByteString -> SHA512
forall a. HashAlgorithm a => ByteString -> a
hash (Text -> ByteString
encodeUtf8 Text
s) :: SHA512.SHA512)
    Text
_ ->
      ErrorCall -> m NixString
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
        (ErrorCall -> m NixString) -> ErrorCall -> m NixString
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
        (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.hashString: "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"expected \"md5\", \"sha1\", \"sha256\", or \"sha512\", got "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Text -> FilePath
forall a. Show a => a -> FilePath
show Text
algo

placeHolder :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
placeHolder :: NValue t f m -> m (NValue t f m)
placeHolder = NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m NixString)
-> (NixString -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext (NixString -> m Text)
-> (Text -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \Text
t -> do
  NixString
h <- Prim m NixString -> m NixString
forall (m :: * -> *) a. Prim m a -> m a
runPrim
    (NixString -> NixString -> Prim m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NixString -> NixString -> Prim m NixString
hashString (Text -> NixString
principledMakeNixStringWithoutContext Text
"sha256")
                (Text -> NixString
principledMakeNixStringWithoutContext (Text
"nix-output:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t))
    )
  NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
    (NixString -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext
    (Text -> NixString) -> Text -> NixString
forall a b. (a -> b) -> a -> b
$ Char -> Text -> Text
Text.cons Char
'/'
    (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ ByteString -> Text
Base32.encode
    (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ (ByteString, ByteString) -> ByteString
forall a b. (a, b) -> a
fst             -- The result coming out of hashString is base16 encoded
    ((ByteString, ByteString) -> ByteString)
-> (ByteString, ByteString) -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> (ByteString, ByteString)
Base16.decode
    (ByteString -> (ByteString, ByteString))
-> ByteString -> (ByteString, ByteString)
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
encodeUtf8
    (Text -> ByteString) -> Text -> ByteString
forall a b. (a -> b) -> a -> b
$ NixString -> Text
principledStringIgnoreContext NixString
h

absolutePathFromValue :: MonadNix e t f m => NValue t f m -> m FilePath
absolutePathFromValue :: NValue t f m -> m FilePath
absolutePathFromValue = \case
  NVStr NixString
ns -> do
    let path :: FilePath
path = Text -> FilePath
Text.unpack (Text -> FilePath) -> Text -> FilePath
forall a b. (a -> b) -> a -> b
$ NixString -> Text
hackyStringIgnoreContext NixString
ns
    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (FilePath -> Bool
isAbsolute FilePath
path)
      (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$  ErrorCall -> m ()
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
      (ErrorCall -> m ()) -> ErrorCall -> m ()
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
      (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"string "
      FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
forall a. Show a => a -> FilePath
show FilePath
path
      FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" doesn't represent an absolute path"
    FilePath -> m FilePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure FilePath
path
  NVPath FilePath
path -> FilePath -> m FilePath
forall (f :: * -> *) a. Applicative f => a -> f a
pure FilePath
path
  NValue t f m
v           -> ErrorCall -> m FilePath
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m FilePath) -> ErrorCall -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"expected a path, got " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
v

readFile_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
readFile_ :: NValue t f m -> m (NValue t f m)
readFile_ NValue t f m
path = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
path ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$
  NValue t f m -> m FilePath
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m FilePath
absolutePathFromValue (NValue t f m -> m FilePath)
-> (FilePath -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> FilePath -> m ByteString
forall (m :: * -> *). MonadFile m => FilePath -> m ByteString
Nix.Render.readFile (FilePath -> m ByteString)
-> (ByteString -> m (NValue t f m)) -> FilePath -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ByteString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue

findFile_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
findFile_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
findFile_ NValue t f m
aset NValue t f m
filePath = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
aset ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
aset' -> NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
filePath ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
filePath' ->
  case (NValue t f m
aset', NValue t f m
filePath') of
    (NVList [NValue t f m]
x, NVStr NixString
ns) -> do
      FilePath
mres <- [NValue t f m] -> FilePath -> m FilePath
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
[NValue t f m] -> FilePath -> m FilePath
findPath @t @f @m [NValue t f m]
x (Text -> FilePath
Text.unpack (NixString -> Text
hackyStringIgnoreContext NixString
ns))
      NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
FilePath -> NValue t f m
nvPath FilePath
mres
    (NVList [NValue t f m]
_, NValue t f m
y) ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"expected a string, got " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
y
    (NValue t f m
x, NVStr NixString
_) -> ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"expected a list, got " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
x
    (NValue t f m
x, NValue t f m
y) ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"Invalid types for builtins.findFile: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ (NValue t f m, NValue t f m) -> FilePath
forall a. Show a => a -> FilePath
show
        (NValue t f m
x, NValue t f m
y)

data FileType
   = FileTypeRegular
   | FileTypeDirectory
   | FileTypeSymlink
   | FileTypeUnknown
   deriving (Int -> FileType -> FilePath -> FilePath
[FileType] -> FilePath -> FilePath
FileType -> FilePath
(Int -> FileType -> FilePath -> FilePath)
-> (FileType -> FilePath)
-> ([FileType] -> FilePath -> FilePath)
-> Show FileType
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
showList :: [FileType] -> FilePath -> FilePath
$cshowList :: [FileType] -> FilePath -> FilePath
show :: FileType -> FilePath
$cshow :: FileType -> FilePath
showsPrec :: Int -> FileType -> FilePath -> FilePath
$cshowsPrec :: Int -> FileType -> FilePath -> FilePath
Show, ReadPrec [FileType]
ReadPrec FileType
Int -> ReadS FileType
ReadS [FileType]
(Int -> ReadS FileType)
-> ReadS [FileType]
-> ReadPrec FileType
-> ReadPrec [FileType]
-> Read FileType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [FileType]
$creadListPrec :: ReadPrec [FileType]
readPrec :: ReadPrec FileType
$creadPrec :: ReadPrec FileType
readList :: ReadS [FileType]
$creadList :: ReadS [FileType]
readsPrec :: Int -> ReadS FileType
$creadsPrec :: Int -> ReadS FileType
Read, FileType -> FileType -> Bool
(FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool) -> Eq FileType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileType -> FileType -> Bool
$c/= :: FileType -> FileType -> Bool
== :: FileType -> FileType -> Bool
$c== :: FileType -> FileType -> Bool
Eq, Eq FileType
Eq FileType
-> (FileType -> FileType -> Ordering)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> FileType)
-> (FileType -> FileType -> FileType)
-> Ord FileType
FileType -> FileType -> Bool
FileType -> FileType -> Ordering
FileType -> FileType -> FileType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FileType -> FileType -> FileType
$cmin :: FileType -> FileType -> FileType
max :: FileType -> FileType -> FileType
$cmax :: FileType -> FileType -> FileType
>= :: FileType -> FileType -> Bool
$c>= :: FileType -> FileType -> Bool
> :: FileType -> FileType -> Bool
$c> :: FileType -> FileType -> Bool
<= :: FileType -> FileType -> Bool
$c<= :: FileType -> FileType -> Bool
< :: FileType -> FileType -> Bool
$c< :: FileType -> FileType -> Bool
compare :: FileType -> FileType -> Ordering
$ccompare :: FileType -> FileType -> Ordering
$cp1Ord :: Eq FileType
Ord)

instance Convertible e t f m => ToValue FileType m (NValue t f m) where
  toValue :: FileType -> m (NValue t f m)
toValue = NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m))
-> (FileType -> NixString) -> FileType -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
principledMakeNixStringWithoutContext (Text -> NixString) -> (FileType -> Text) -> FileType -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
    FileType
FileTypeRegular   -> Text
"regular" :: Text
    FileType
FileTypeDirectory -> Text
"directory"
    FileType
FileTypeSymlink   -> Text
"symlink"
    FileType
FileTypeUnknown   -> Text
"unknown"

readDir_
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
readDir_ :: NValue t f m -> m (NValue t f m)
readDir_ NValue t f m
p = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
p ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
path' -> do
  FilePath
path           <- NValue t f m -> m FilePath
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m FilePath
absolutePathFromValue NValue t f m
path'
  [FilePath]
items          <- FilePath -> m [FilePath]
forall (m :: * -> *). MonadFile m => FilePath -> m [FilePath]
listDirectory FilePath
path
  [(Text, FileType)]
itemsWithTypes <- [FilePath]
-> (FilePath -> m (Text, FileType)) -> m [(Text, FileType)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [FilePath]
items ((FilePath -> m (Text, FileType)) -> m [(Text, FileType)])
-> (FilePath -> m (Text, FileType)) -> m [(Text, FileType)]
forall a b. (a -> b) -> a -> b
$ \FilePath
item -> do
    FileStatus
s <- FilePath -> m FileStatus
forall (m :: * -> *). MonadFile m => FilePath -> m FileStatus
getSymbolicLinkStatus (FilePath -> m FileStatus) -> FilePath -> m FileStatus
forall a b. (a -> b) -> a -> b
$ FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
item
    let t :: FileType
t = if
          | FileStatus -> Bool
isRegularFile FileStatus
s  -> FileType
FileTypeRegular
          | FileStatus -> Bool
isDirectory FileStatus
s    -> FileType
FileTypeDirectory
          | FileStatus -> Bool
isSymbolicLink FileStatus
s -> FileType
FileTypeSymlink
          | Bool
otherwise        -> FileType
FileTypeUnknown
    (Text, FileType) -> m (Text, FileType)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FilePath -> Text
Text.pack FilePath
item, FileType
t)
  Deeper (NValue t f m) -> NValue t f m
forall a. Deeper a -> a
getDeeper (Deeper (NValue t f m) -> NValue t f m)
-> m (Deeper (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Text FileType -> m (Deeper (NValue t f m))
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue ([(Text, FileType)] -> HashMap Text FileType
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [(Text, FileType)]
itemsWithTypes)

fromJSON
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
fromJSON :: NValue t f m -> m (NValue t f m)
fromJSON NValue t f m
arg = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
arg ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m NixString)
-> (NixString -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext (NixString -> m Text)
-> (Text -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \Text
encoded ->
  case FromJSON Value => ByteString -> Either FilePath Value
forall a. FromJSON a => ByteString -> Either FilePath a
A.eitherDecodeStrict' @A.Value (ByteString -> Either FilePath Value)
-> ByteString -> Either FilePath Value
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
encodeUtf8 Text
encoded of
    Left FilePath
jsonError ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"builtins.fromJSON: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
jsonError
    Right Value
v -> Value -> m (NValue t f m)
forall t (m :: * -> *). Value -> m (NValue t f m)
jsonToNValue Value
v
 where
  jsonToNValue :: Value -> m (NValue t f m)
jsonToNValue = \case
    A.Object Object
m -> (HashMap Text (NValue t f m)
 -> HashMap Text SourcePos -> NValue t f m)
-> HashMap Text SourcePos
-> HashMap Text (NValue t f m)
-> NValue t f m
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text SourcePos
forall k v. HashMap k v
M.empty (HashMap Text (NValue t f m) -> NValue t f m)
-> m (HashMap Text (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value -> m (NValue t f m))
-> Object -> m (HashMap Text (NValue t f m))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value -> m (NValue t f m)
jsonToNValue Object
m
    A.Array  Array
l -> [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList ([NValue t f m] -> NValue t f m)
-> m [NValue t f m] -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value -> m (NValue t f m)) -> [Value] -> m [NValue t f m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value -> m (NValue t f m)
jsonToNValue (Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
l)
    A.String Text
s -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NixString
hackyMakeNixStringWithoutContext Text
s
    A.Number Scientific
n -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> NAtom -> NValue t f m
forall a b. (a -> b) -> a -> b
$ case Scientific -> Either Float Integer
forall r i. (RealFloat r, Integral i) => Scientific -> Either r i
floatingOrInteger Scientific
n of
      Left  Float
r -> Float -> NAtom
NFloat Float
r
      Right Integer
i -> Integer -> NAtom
NInt Integer
i
    A.Bool   Bool
b -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> NAtom -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Bool -> NAtom
NBool Bool
b
    Value
A.Null     -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant NAtom
NNull

prim_toJSON :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
prim_toJSON :: NValue t f m -> m (NValue t f m)
prim_toJSON NValue t f m
x = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ (NixString -> NValue t f m) -> m NixString -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (m NixString -> m (NValue t f m))
-> (NValue t f m -> m NixString)
-> NValue t f m
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m NixString
nvalueToJSONNixString

toXML_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
toXML_ :: NValue t f m -> m (NValue t f m)
toXML_ NValue t f m
v = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
v ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ (NValue t f m -> NValue t f m)
-> m (NValue t f m) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (NixString -> NValue t f m)
-> (NValue t f m -> NixString) -> NValue t f m -> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> NixString
forall t (f :: * -> *) (m :: * -> *).
MonadDataContext f m =>
NValue t f m -> NixString
toXML) (m (NValue t f m) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> m (NValue t f m)
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadThunk t m (NValue t f m),
 MonadDataErrorContext t f m, HasCitations m (NValue t f m) t,
 HasCitations1 m (NValue t f m) f, Ord (ThunkId m)) =>
NValue t f m -> m (NValue t f m)
normalForm

typeOf :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
typeOf :: NValue t f m -> m (NValue t f m)
typeOf NValue t f m
v = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
v ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m))
-> (NValue t f m -> NixString) -> NValue t f m -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
principledMakeNixStringWithoutContext (Text -> NixString)
-> (NValue t f m -> Text) -> NValue t f m -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
  NVConstant NAtom
a -> case NAtom
a of
    NURI   Text
_ -> Text
"string"
    NInt   Integer
_ -> Text
"int"
    NFloat Float
_ -> Text
"float"
    NBool  Bool
_ -> Text
"bool"
    NAtom
NNull    -> Text
"null"
  NVStr  NixString
_      -> Text
"string"
  NVList [NValue t f m]
_      -> Text
"list"
  NVSet AttrSet (NValue t f m)
_ HashMap Text SourcePos
_     -> Text
"set"
  NVClosure{}   -> Text
"lambda"
  NVPath FilePath
_      -> Text
"path"
  NVBuiltin FilePath
_ NValue t f m -> m (NValue t f m)
_ -> Text
"lambda"
  NValue t f m
_             -> FilePath -> Text
forall a. HasCallStack => FilePath -> a
error FilePath
"Pattern synonyms obscure complete patterns"

tryEval
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
tryEval :: NValue t f m -> m (NValue t f m)
tryEval NValue t f m
e = m (NValue t f m)
-> (SomeException -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
catch (NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
e (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> (NValue t f m -> NValue t f m)
-> NValue t f m
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NValue t f m -> NValue t f m
onSuccess)) (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> (SomeException -> NValue t f m)
-> SomeException
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SomeException -> NValue t f m
onError)
 where
  onSuccess :: NValue t f m -> NValue t f m
onSuccess NValue t f m
v = (HashMap Text (NValue t f m)
 -> HashMap Text SourcePos -> NValue t f m)
-> HashMap Text SourcePos
-> HashMap Text (NValue t f m)
-> NValue t f m
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text SourcePos
forall k v. HashMap k v
M.empty (HashMap Text (NValue t f m) -> NValue t f m)
-> HashMap Text (NValue t f m) -> NValue t f m
forall a b. (a -> b) -> a -> b
$ [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
    [(Text
"success", NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (Bool -> NAtom
NBool Bool
True)), (Text
"value", NValue t f m
v)]

  onError :: SomeException -> NValue t f m
  onError :: SomeException -> NValue t f m
onError SomeException
_ = (HashMap Text (NValue t f m)
 -> HashMap Text SourcePos -> NValue t f m)
-> HashMap Text SourcePos
-> HashMap Text (NValue t f m)
-> NValue t f m
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text SourcePos
forall k v. HashMap k v
M.empty (HashMap Text (NValue t f m) -> NValue t f m)
-> HashMap Text (NValue t f m) -> NValue t f m
forall a b. (a -> b) -> a -> b
$ [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
    [ (Text
"success", NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (Bool -> NAtom
NBool Bool
False))
    , (Text
"value"  , NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (Bool -> NAtom
NBool Bool
False))
    ]

trace_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
trace_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
trace_ NValue t f m
msg NValue t f m
action = do
  MonadEffects t f m => FilePath -> m ()
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
FilePath -> m ()
traceEffect @t @f @m
    (FilePath -> m ()) -> (NixString -> FilePath) -> NixString -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   Text -> FilePath
Text.unpack
    (Text -> FilePath) -> (NixString -> Text) -> NixString -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   NixString -> Text
principledStringIgnoreContext
    (NixString -> m ()) -> m NixString -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
msg
  NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
action

-- TODO: remember error context
addErrorContext
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
addErrorContext :: NValue t f m -> NValue t f m -> m (NValue t f m)
addErrorContext NValue t f m
_ NValue t f m
action = NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
action

exec_
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
exec_ :: NValue t f m -> m (NValue t f m)
exec_ NValue t f m
xs = do
  [NValue t f m]
ls <- NValue t f m -> m [NValue t f m]
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @[NValue t f m] NValue t f m
xs
  [NixString]
xs <- (NValue t f m -> m NixString) -> [NValue t f m] -> m [NixString]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
forall e (m :: * -> *) t (f :: * -> *).
(Framed e m, MonadStore m, MonadThrow m,
 MonadDataErrorContext t f m, MonadValue (NValue t f m) m) =>
(NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
coerceToString NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
callFunc CopyToStoreMode
DontCopyToStore CoercionLevel
CoerceStringy) [NValue t f m]
ls
  -- TODO Still need to do something with the context here
  -- See prim_exec in nix/src/libexpr/primops.cc
  -- Requires the implementation of EvalState::realiseContext
  [FilePath] -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, MonadInstantiate m) =>
[FilePath] -> m (NValue t f m)
exec ((NixString -> FilePath) -> [NixString] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> FilePath
Text.unpack (Text -> FilePath) -> (NixString -> Text) -> NixString -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
hackyStringIgnoreContext) [NixString]
xs)

fetchurl
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
fetchurl :: NValue t f m -> m (NValue t f m)
fetchurl NValue t f m
v = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
v ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \case
  NVSet AttrSet (NValue t f m)
s HashMap Text SourcePos
_ -> Text -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Text -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet Text
"url" AttrSet (NValue t f m)
s m (NValue t f m)
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand (NValue t f m
 -> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => f (a -> b) -> a -> f b
?? Maybe (NValue t f m) -> NValue t f m -> m (NValue t f m)
go (Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
"sha256" AttrSet (NValue t f m)
s)
  v :: NValue t f m
v@NVStr{} -> Maybe (NValue t f m) -> NValue t f m -> m (NValue t f m)
go Maybe (NValue t f m)
forall a. Maybe a
Nothing NValue t f m
v
  NValue t f m
v ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
      (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
      (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.fetchurl: Expected URI or set, got "
      FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
v
 where
  go :: Maybe (NValue t f m) -> NValue t f m -> m (NValue t f m)
  go :: Maybe (NValue t f m) -> NValue t f m -> m (NValue t f m)
go Maybe (NValue t f m)
_msha = \case
    NVStr NixString
ns -> NixString -> m Text
forall e (m :: * -> *).
(MonadReader e m, Has e Frames, MonadThrow m) =>
NixString -> m Text
noContextAttrs NixString
ns m Text
-> (Text -> m (Either ErrorCall StorePath))
-> m (Either ErrorCall StorePath)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> m (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadHttp m =>
Text -> m (Either ErrorCall StorePath)
getURL m (Either ErrorCall StorePath)
-> (Either ErrorCall StorePath -> m (NValue t f m))
-> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case -- msha
      Left  ErrorCall
e -> ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError ErrorCall
e
      Right StorePath
p -> StorePath -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue StorePath
p
    NValue t f m
v ->
      ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
        (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
        (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"builtins.fetchurl: Expected URI or string, got "
        FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
v

  noContextAttrs :: NixString -> m Text
noContextAttrs NixString
ns = case NixString -> Maybe Text
principledGetStringNoContext NixString
ns of
    Maybe Text
Nothing ->
      ErrorCall -> m Text
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m Text) -> ErrorCall -> m Text
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"builtins.fetchurl: unsupported arguments to url"
    Just Text
t -> Text -> m Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
t

partition_
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
partition_ :: NValue t f m -> NValue t f m -> m (NValue t f m)
partition_ NValue t f m
f = forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v.
FromValue [NValue t f m] m v =>
v -> m [NValue t f m]
fromValue @[NValue t f m] (NValue t f m -> m [NValue t f m])
-> ([NValue t f m] -> m (NValue t f m))
-> NValue t f m
-> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \[NValue t f m]
l -> do
  let match :: NValue t f m -> m (Bool, NValue t f m)
match NValue t f m
t = NValue t f m
f NValue t f m -> NValue t f m -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> NValue t f m -> m (NValue t f m)
`callFunc` NValue t f m
t m (NValue t f m)
-> (NValue t f m -> m (Bool, NValue t f m))
-> m (Bool, NValue t f m)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> (Bool, NValue t f m)) -> m Bool -> m (Bool, NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (, NValue t f m
t) (m Bool -> m (Bool, NValue t f m))
-> (NValue t f m -> m Bool)
-> NValue t f m
-> m (Bool, NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue
  [(Bool, NValue t f m)]
selection <- (NValue t f m -> m (Bool, NValue t f m))
-> [NValue t f m] -> m [(Bool, NValue t f m)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NValue t f m -> m (Bool, NValue t f m)
match [NValue t f m]
l
  let ([(Bool, NValue t f m)]
right, [(Bool, NValue t f m)]
wrong) = ((Bool, NValue t f m) -> Bool)
-> [(Bool, NValue t f m)]
-> ([(Bool, NValue t f m)], [(Bool, NValue t f m)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (Bool, NValue t f m) -> Bool
forall a b. (a, b) -> a
fst [(Bool, NValue t f m)]
selection
  let makeSide :: [(a, NValue t f m)] -> NValue t f m
makeSide       = [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
nvList ([NValue t f m] -> NValue t f m)
-> ([(a, NValue t f m)] -> [NValue t f m])
-> [(a, NValue t f m)]
-> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, NValue t f m) -> NValue t f m)
-> [(a, NValue t f m)] -> [NValue t f m]
forall a b. (a -> b) -> [a] -> [b]
map (a, NValue t f m) -> NValue t f m
forall a b. (a, b) -> b
snd
  forall a (m :: * -> *) v. ToValue a m v => a -> m v
forall (m :: * -> *) v.
ToValue (AttrSet (NValue t f m)) m v =>
AttrSet (NValue t f m) -> m v
toValue @(AttrSet (NValue t f m))
    (AttrSet (NValue t f m) -> m (NValue t f m))
-> AttrSet (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [(Text, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [(Text
"right", [(Bool, NValue t f m)] -> NValue t f m
forall a t (m :: * -> *). [(a, NValue t f m)] -> NValue t f m
makeSide [(Bool, NValue t f m)]
right), (Text
"wrong", [(Bool, NValue t f m)] -> NValue t f m
forall a t (m :: * -> *). [(a, NValue t f m)] -> NValue t f m
makeSide [(Bool, NValue t f m)]
wrong)]

currentSystem :: MonadNix e t f m => m (NValue t f m)
currentSystem :: m (NValue t f m)
currentSystem = do
  Text
os   <- m Text
forall (m :: * -> *). MonadEnv m => m Text
getCurrentSystemOS
  Text
arch <- m Text
forall (m :: * -> *). MonadEnv m => m Text
getCurrentSystemArch
  NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
nvStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NixString
principledMakeNixStringWithoutContext (Text
arch Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
os)

currentTime_ :: MonadNix e t f m => m (NValue t f m)
currentTime_ :: m (NValue t f m)
currentTime_ = do
  Options
opts :: Options <- (e -> Options) -> m Options
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (FoldLike Options e e Options Options -> e -> Options
forall a s t b. FoldLike a s t a b -> s -> a
view FoldLike Options e e Options Options
forall a b. Has a b => Lens' a b
hasLens)
  forall a (m :: * -> *) v. ToValue a m v => a -> m v
forall (m :: * -> *) v. ToValue Integer m v => Integer -> m v
toValue @Integer (Integer -> m (NValue t f m)) -> Integer -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ POSIXTime -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
round (POSIXTime -> Integer) -> POSIXTime -> Integer
forall a b. (a -> b) -> a -> b
$ UTCTime -> POSIXTime
Time.utcTimeToPOSIXSeconds (Options -> UTCTime
currentTime Options
opts)

derivationStrict_ :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
derivationStrict_ :: NValue t f m -> m (NValue t f m)
derivationStrict_ = NValue t f m -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
NValue t f m -> m (NValue t f m)
derivationStrict

getRecursiveSize :: (MonadIntrospect m, Applicative f) => a -> m (NValue t f m)
getRecursiveSize :: a -> m (NValue t f m)
getRecursiveSize = (Word -> NValue t f m) -> m Word -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
nvConstant (NAtom -> NValue t f m) -> (Word -> NAtom) -> Word -> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> NAtom
NInt (Integer -> NAtom) -> (Word -> Integer) -> Word -> NAtom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (m Word -> m (NValue t f m))
-> (a -> m Word) -> a -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m Word
forall (m :: * -> *) a. MonadIntrospect m => a -> m Word
recursiveSize

getContext
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
getContext :: NValue t f m -> m (NValue t f m)
getContext NValue t f m
x = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \case
  (NVStr NixString
ns) -> do
    let context :: HashMap Text NixLikeContextValue
context =
          NixLikeContext -> HashMap Text NixLikeContextValue
getNixLikeContext (NixLikeContext -> HashMap Text NixLikeContextValue)
-> NixLikeContext -> HashMap Text NixLikeContextValue
forall a b. (a -> b) -> a -> b
$ HashSet StringContext -> NixLikeContext
toNixLikeContext (HashSet StringContext -> NixLikeContext)
-> HashSet StringContext -> NixLikeContext
forall a b. (a -> b) -> a -> b
$ NixString -> HashSet StringContext
principledGetContext NixString
ns
    HashMap Text (NValue t f m)
valued :: M.HashMap Text (NValue t f m) <- HashMap Text (m (NValue t f m)) -> m (HashMap Text (NValue t f m))
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA (HashMap Text (m (NValue t f m))
 -> m (HashMap Text (NValue t f m)))
-> HashMap Text (m (NValue t f m))
-> m (HashMap Text (NValue t f m))
forall a b. (a -> b) -> a -> b
$ (NixLikeContextValue -> m (NValue t f m))
-> HashMap Text NixLikeContextValue
-> HashMap Text (m (NValue t f m))
forall v1 v2 k. (v1 -> v2) -> HashMap k v1 -> HashMap k v2
M.map NixLikeContextValue -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue HashMap Text NixLikeContextValue
context
    NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
HashMap Text (NValue t f m)
-> HashMap Text SourcePos -> NValue t f m
nvSet HashMap Text (NValue t f m)
valued HashMap Text SourcePos
forall k v. HashMap k v
M.empty
  NValue t f m
x ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"Invalid type for builtins.getContext: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
x

appendContext
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
appendContext :: NValue t f m -> NValue t f m -> m (NValue t f m)
appendContext NValue t f m
x NValue t f m
y = NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
x ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
x' -> NValue t f m
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
y ((NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
y' -> case (NValue t f m
x', NValue t f m
y') of
  (NVStr NixString
ns, NVSet AttrSet (NValue t f m)
attrs HashMap Text SourcePos
_) -> do
    HashMap Text NixLikeContextValue
newContextValues <- AttrSet (NValue t f m)
-> (NValue t f m -> m NixLikeContextValue)
-> m (HashMap Text NixLikeContextValue)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM AttrSet (NValue t f m)
attrs ((NValue t f m -> m NixLikeContextValue)
 -> m (HashMap Text NixLikeContextValue))
-> (NValue t f m -> m NixLikeContextValue)
-> m (HashMap Text NixLikeContextValue)
forall a b. (a -> b) -> a -> b
$ \NValue t f m
attr -> NValue t f m
-> (NValue t f m -> m NixLikeContextValue) -> m NixLikeContextValue
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
attr ((NValue t f m -> m NixLikeContextValue) -> m NixLikeContextValue)
-> (NValue t f m -> m NixLikeContextValue) -> m NixLikeContextValue
forall a b. (a -> b) -> a -> b
$ \case
      NVSet AttrSet (NValue t f m)
attrs HashMap Text SourcePos
_ -> do
        -- TODO: Fail for unexpected keys.
        Bool
path <- m Bool
-> (NValue t f m -> m Bool) -> Maybe (NValue t f m) -> m Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False) (NValue t f m -> (NValue t f m -> m Bool) -> m Bool
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand (NValue t f m -> (NValue t f m -> m Bool) -> m Bool)
-> (NValue t f m -> m Bool) -> NValue t f m -> m Bool
forall (f :: * -> *) a b. Functor f => f (a -> b) -> a -> f b
?? NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue)
          (Maybe (NValue t f m) -> m Bool) -> Maybe (NValue t f m) -> m Bool
forall a b. (a -> b) -> a -> b
$ Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
"path" AttrSet (NValue t f m)
attrs
        Bool
allOutputs <- m Bool
-> (NValue t f m -> m Bool) -> Maybe (NValue t f m) -> m Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False) (NValue t f m -> (NValue t f m -> m Bool) -> m Bool
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand (NValue t f m -> (NValue t f m -> m Bool) -> m Bool)
-> (NValue t f m -> m Bool) -> NValue t f m -> m Bool
forall (f :: * -> *) a b. Functor f => f (a -> b) -> a -> f b
?? NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue)
          (Maybe (NValue t f m) -> m Bool) -> Maybe (NValue t f m) -> m Bool
forall a b. (a -> b) -> a -> b
$ Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
"allOutputs" AttrSet (NValue t f m)
attrs
        [Text]
outputs <- case Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
"outputs" AttrSet (NValue t f m)
attrs of
          Maybe (NValue t f m)
Nothing -> [Text] -> m [Text]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
          Just NValue t f m
os -> NValue t f m -> (NValue t f m -> m [Text]) -> m [Text]
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
os ((NValue t f m -> m [Text]) -> m [Text])
-> (NValue t f m -> m [Text]) -> m [Text]
forall a b. (a -> b) -> a -> b
$ \case
            NVList [NValue t f m]
vs ->
              [NValue t f m] -> (NValue t f m -> m Text) -> m [Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [NValue t f m]
vs ((NValue t f m -> m Text) -> m [Text])
-> (NValue t f m -> m Text) -> m [Text]
forall a b. (a -> b) -> a -> b
$ (NixString -> Text) -> m NixString -> m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NixString -> Text
principledStringIgnoreContext (m NixString -> m Text)
-> (NValue t f m -> m NixString) -> NValue t f m -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue
            NValue t f m
x ->
              ErrorCall -> m [Text]
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
                (ErrorCall -> m [Text]) -> ErrorCall -> m [Text]
forall a b. (a -> b) -> a -> b
$ FilePath -> ErrorCall
ErrorCall
                (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$ FilePath
"Invalid types for context value outputs in builtins.appendContext: "
                FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
x
        NixLikeContextValue -> m NixLikeContextValue
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NixLikeContextValue -> m NixLikeContextValue)
-> NixLikeContextValue -> m NixLikeContextValue
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> [Text] -> NixLikeContextValue
NixLikeContextValue Bool
path Bool
allOutputs [Text]
outputs
      NValue t f m
x ->
        ErrorCall -> m NixLikeContextValue
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
          (ErrorCall -> m NixLikeContextValue)
-> ErrorCall -> m NixLikeContextValue
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
          (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"Invalid types for context value in builtins.appendContext: "
          FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ NValue t f m -> FilePath
forall a. Show a => a -> FilePath
show NValue t f m
x
    NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
      (NixString -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> HashSet StringContext -> NixString
principledMakeNixString (NixString -> Text
principledStringIgnoreContext NixString
ns)
      (HashSet StringContext -> NixString)
-> HashSet StringContext -> NixString
forall a b. (a -> b) -> a -> b
$ NixLikeContext -> HashSet StringContext
fromNixLikeContext
      (NixLikeContext -> HashSet StringContext)
-> NixLikeContext -> HashSet StringContext
forall a b. (a -> b) -> a -> b
$ HashMap Text NixLikeContextValue -> NixLikeContext
NixLikeContext
      (HashMap Text NixLikeContextValue -> NixLikeContext)
-> HashMap Text NixLikeContextValue -> NixLikeContext
forall a b. (a -> b) -> a -> b
$ (NixLikeContextValue -> NixLikeContextValue -> NixLikeContextValue)
-> HashMap Text NixLikeContextValue
-> HashMap Text NixLikeContextValue
-> HashMap Text NixLikeContextValue
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v
M.unionWith NixLikeContextValue -> NixLikeContextValue -> NixLikeContextValue
forall a. Semigroup a => a -> a -> a
(<>) HashMap Text NixLikeContextValue
newContextValues
      (HashMap Text NixLikeContextValue
 -> HashMap Text NixLikeContextValue)
-> HashMap Text NixLikeContextValue
-> HashMap Text NixLikeContextValue
forall a b. (a -> b) -> a -> b
$ NixLikeContext -> HashMap Text NixLikeContextValue
getNixLikeContext
      (NixLikeContext -> HashMap Text NixLikeContextValue)
-> NixLikeContext -> HashMap Text NixLikeContextValue
forall a b. (a -> b) -> a -> b
$ HashSet StringContext -> NixLikeContext
toNixLikeContext
      (HashSet StringContext -> NixLikeContext)
-> HashSet StringContext -> NixLikeContext
forall a b. (a -> b) -> a -> b
$ NixString -> HashSet StringContext
principledGetContext NixString
ns
  (NValue t f m
x, NValue t f m
y) ->
    ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
      (ErrorCall -> m (NValue t f m)) -> ErrorCall -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$  FilePath -> ErrorCall
ErrorCall
      (FilePath -> ErrorCall) -> FilePath -> ErrorCall
forall a b. (a -> b) -> a -> b
$  FilePath
"Invalid types for builtins.appendContext: "
      FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ (NValue t f m, NValue t f m) -> FilePath
forall a. Show a => a -> FilePath
show (NValue t f m
x, NValue t f m
y)

newtype Prim m a = Prim { Prim m a -> m a
runPrim :: m a }

-- | Types that support conversion to nix in a particular monad
class ToBuiltin t f m a | a -> m where
    toBuiltin :: String -> a -> m (NValue t f m)

instance (MonadNix e t f m, ToValue a m (NValue t f m))
      => ToBuiltin t f m (Prim m a) where
  toBuiltin :: FilePath -> Prim m a -> m (NValue t f m)
toBuiltin FilePath
_ Prim m a
p = a -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (a -> m (NValue t f m)) -> m a -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Prim m a -> m a
forall (m :: * -> *) a. Prim m a -> m a
runPrim Prim m a
p

instance ( MonadNix e t f m
         , FromValue a m (Deeper (NValue t f m))
         , ToBuiltin t f m b
         )
      => ToBuiltin t f m (a -> b) where
  toBuiltin :: FilePath -> (a -> b) -> m (NValue t f m)
toBuiltin FilePath
name a -> b
f =
    NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ FilePath -> (NValue t f m -> m (NValue t f m)) -> NValue t f m
forall (f :: * -> *) (m :: * -> *) t.
(Applicative f, Functor m) =>
FilePath -> (NValue t f m -> m (NValue t f m)) -> NValue t f m
nvBuiltin FilePath
name (Deeper (NValue t f m) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (Deeper (NValue t f m) -> m a)
-> (NValue t f m -> Deeper (NValue t f m)) -> NValue t f m -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper (NValue t f m -> m a)
-> (a -> m (NValue t f m)) -> NValue t f m -> m (NValue t f m)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> FilePath -> b -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *) a.
ToBuiltin t f m a =>
FilePath -> a -> m (NValue t f m)
toBuiltin FilePath
name (b -> m (NValue t f m)) -> (a -> b) -> a -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)