{-# language CPP #-}
{-# language AllowAmbiguousTypes #-}
{-# language ConstraintKinds #-}
{-# language FunctionalDependencies #-}
{-# language KindSignatures #-}
{-# language MonoLocalBinds #-}
{-# language MultiWayIf #-}
{-# language PartialTypeSignatures #-}
{-# language QuasiQuotes #-}
{-# language TemplateHaskell #-}
{-# language UndecidableInstances #-}
{-# language PackageImports #-} -- 2021-07-05: Due to hashing Haskell IT system situation, in HNix we currently ended-up with 2 hash package dependencies @{hashing, cryptonite}@

{-# options_ghc -fno-warn-name-shadowing #-}


-- | Code that implements Nix builtins. Lists the functions that are built into the Nix expression evaluator. Some built-ins (aka `derivation`), are always in the scope, so they can be accessed by the name. To keap the namespace clean, most built-ins are inside the `builtins` scope - a set that contains all what is a built-in.
module Nix.Builtins
  ( withNixContext
  , builtins
  )
where

import           Nix.Prelude
import           GHC.Exception                  ( ErrorCall(ErrorCall) )
import           Control.Comonad                ( Comonad )
import           Control.Monad                  ( foldM )
import           Control.Monad.Catch            ( MonadCatch(catch) )
import           Control.Monad.ListM            ( sortByM )
import           "hashing" Crypto.Hash
import qualified "hashing" Crypto.Hash.MD5     as MD5
import qualified "hashing" Crypto.Hash.SHA1    as SHA1
import qualified "hashing" Crypto.Hash.SHA256  as SHA256
import qualified "hashing" Crypto.Hash.SHA512  as SHA512
import qualified Data.Aeson                    as A
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key                as AKM
import qualified Data.Aeson.KeyMap             as AKM
#endif
import           Data.Align                     ( alignWith )
import           Data.Array
import           Data.Bits
import qualified Data.ByteString               as B
import           Data.ByteString.Base16        as Base16
import           Data.Char                      ( isDigit )
import           Data.Foldable                  ( foldrM )
import           Data.Fix                       ( foldFix )
import           Data.List                      ( partition )
import qualified Data.HashSet                  as HS
import qualified Data.HashMap.Lazy             as M
import           Data.Scientific
import qualified Data.Set                      as S
import qualified Data.Text                     as Text
import           Data.Text.Read                 ( decimal )
import qualified Data.Text.Lazy.Builder        as Builder
import           Data.These                     ( fromThese, These )
import qualified Data.Time.Clock.POSIX         as Time
import qualified Data.Vector                   as V
import           NeatInterpolation              ( text )
import           Nix.Atoms
import           Nix.Convert
import           Nix.Effects
import           Nix.Effects.Basic              ( fetchTarball )
import           Nix.Exec
import           Nix.Expr.Types
import qualified Nix.Eval                      as Eval
import           Nix.Frames
import           Nix.Json
import           Nix.Normal
import           Nix.Options
import           Nix.Parser
import           Nix.Render
import           Nix.Scope
import           Nix.String
import           Nix.String.Coerce
import           Nix.Value
import           Nix.Value.Equal
import           Nix.Value.Monad
import           Nix.XML
import           System.Nix.Base32             as Base32
import           System.PosixCompat.Files       ( isRegularFile
                                                , isDirectory
                                                , isSymbolicLink
                                                )
import qualified Text.Show
import           Text.Regex.TDFA                ( Regex
                                                , makeRegex
                                                , matchOnceText
                                                , matchAllText
                                                )

-- This is a big module. There is recursive reuse:
-- @builtins -> builtinsList -> scopedImport -> withNixContext -> builtins@,
-- since @builtins@ is self-recursive: aka we ship @builtins.builtins.builtins...@.

-- * Internal

-- ** Nix Builtins Haskell type level

newtype Prim m a = Prim (m a)

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

-- *** @class ToBuiltin@ and its instances

-- | Types that support conversion to nix in a particular monad
class ToBuiltin t f m a | a -> m where
  toBuiltin :: Text -> 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 :: Text -> Prim m a -> m (NValue t f m)
toBuiltin Text
_ Prim m a
p = forall v. ToValue a m v => a -> m v
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue @a @m (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
coerce 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 :: Text -> (a -> b) -> m (NValue t f m)
toBuiltin Text
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
$ VarName -> (NValue t f m -> m (NValue t f m)) -> NValue t f m
forall (f :: * -> *) (m :: * -> *) t.
(Applicative f, Functor m) =>
VarName -> (NValue t f m -> m (NValue t f m)) -> NValue t f m
mkNVBuiltin (Text -> VarName
coerce Text
name) ((NValue t f m -> m (NValue t f m)) -> NValue t f m)
-> (NValue t f m -> m (NValue t f m)) -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> b -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *) a.
ToBuiltin t f m a =>
Text -> a -> m (NValue t f m)
toBuiltin Text
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 (a -> m (NValue t f m))
-> (NValue t f m -> m a) -> 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
<=< 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

-- *** @WValue@ closure wrapper to have @Ord@

-- 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     Path
x         ) == WValue (NVPath     Path
y         ) = Path
x Path -> Path -> Bool
forall a. Eq a => a -> a -> Bool
== Path
y
  WValue (NVStr NixString
x) == WValue (NVStr NixString
y) =
    NixString -> Text
ignoreContext NixString
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== NixString -> Text
ignoreContext 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     Path
x         ) <= WValue (NVPath     Path
y         ) = Path
x Path -> Path -> Bool
forall a. Ord a => a -> a -> Bool
<= Path
y
  WValue (NVStr NixString
x) <= WValue (NVStr NixString
y) =
    NixString -> Text
ignoreContext NixString
x Text -> Text -> Bool
forall a. Ord a => a -> a -> Bool
<= NixString -> Text
ignoreContext NixString
y
  WValue t f m
_ <= WValue t f m
_ = Bool
False

-- ** Helpers

mkNVBool
  :: MonadNix e t f m
  => Bool
  -> NValue t f m
mkNVBool :: Bool -> NValue t f m
mkNVBool = NAtom -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NAtom -> NValue t f m
mkNVConstant (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

data NixPathEntryType
  = PathEntryPath
  | PathEntryURI
 deriving (Int -> NixPathEntryType -> ShowS
[NixPathEntryType] -> ShowS
NixPathEntryType -> String
(Int -> NixPathEntryType -> ShowS)
-> (NixPathEntryType -> String)
-> ([NixPathEntryType] -> ShowS)
-> Show NixPathEntryType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NixPathEntryType] -> ShowS
$cshowList :: [NixPathEntryType] -> ShowS
show :: NixPathEntryType -> String
$cshow :: NixPathEntryType -> String
showsPrec :: Int -> NixPathEntryType -> ShowS
$cshowsPrec :: Int -> NixPathEntryType -> ShowS
Show, NixPathEntryType -> NixPathEntryType -> Bool
(NixPathEntryType -> NixPathEntryType -> Bool)
-> (NixPathEntryType -> NixPathEntryType -> Bool)
-> Eq NixPathEntryType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NixPathEntryType -> NixPathEntryType -> Bool
$c/= :: NixPathEntryType -> NixPathEntryType -> Bool
== :: NixPathEntryType -> NixPathEntryType -> Bool
$c== :: NixPathEntryType -> NixPathEntryType -> Bool
Eq)

-- | @NIX_PATH@ is colon-separated, but can also contain URLs, which have a colon
-- (i.e. @https://...@)
uriAwareSplit :: Text -> [(Text, NixPathEntryType)]
uriAwareSplit :: Text -> [(Text, NixPathEntryType)]
uriAwareSplit Text
txt =
  case (Char -> Bool) -> Text -> (Text, Text)
Text.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':') Text
txt of
    (Text
e1, Text
e2)
      | Text -> Bool
Text.null Text
e2                              -> OneItem [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall x. One x => OneItem x -> x
one (Text
e1, NixPathEntryType
PathEntryPath)
      | Text
"://" Text -> Text -> Bool
`Text.isPrefixOf` Text
e2      ->
        let ((Text
suffix, NixPathEntryType
_) : [(Text, NixPathEntryType)]
path) = Text -> [(Text, NixPathEntryType)]
uriAwareSplit (Int -> Text -> Text
Text.drop Int
3 Text
e2) in
        (Text
e1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"://" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
suffix, NixPathEntryType
PathEntryURI) (Text, NixPathEntryType)
-> [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall a. a -> [a] -> [a]
: [(Text, NixPathEntryType)]
path
      | Bool
otherwise                                 -> (Text
e1, NixPathEntryType
PathEntryPath) (Text, NixPathEntryType)
-> [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall a. a -> [a] -> [a]
: Text -> [(Text, NixPathEntryType)]
uriAwareSplit (Int -> Text -> Text
Text.drop Int
1 Text
e2)

foldNixPath
  :: forall e t f m r
   . MonadNix e t f m
  => r
  -> (Path -> Maybe Text -> NixPathEntryType -> r -> m r)
  -> m r
foldNixPath :: r -> (Path -> Maybe Text -> NixPathEntryType -> r -> m r) -> m r
foldNixPath r
z Path -> Maybe Text -> NixPathEntryType -> r -> m r
f =
  do
    Maybe (NValue t f m)
mres <- VarName -> m (Maybe (NValue t f m))
forall a (m :: * -> *). Scoped a m => VarName -> m (Maybe a)
lookupVar VarName
"__includes"
    [NixString]
dirs <-
      m [NixString]
-> (NValue t f m -> m [NixString])
-> Maybe (NValue t f m)
-> m [NixString]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        m [NixString]
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
        ((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])
-> (NValue t f m -> m (NValue t f m))
-> NValue t f m
-> m [NixString]
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 v (m :: * -> *). MonadValue v m => v -> m v
demand)
        Maybe (NValue t f m)
mres
    Maybe Text
mPath    <- Text -> m (Maybe Text)
forall (m :: * -> *). MonadEnv m => Text -> m (Maybe Text)
getEnvVar Text
"NIX_PATH"
    Maybe Text
mDataDir <- Text -> m (Maybe Text)
forall (m :: * -> *). MonadEnv m => Text -> m (Maybe Text)
getEnvVar Text
"NIX_DATA_DIR"
    Path
dataDir  <-
      m Path -> (Text -> m Path) -> Maybe Text -> m Path
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        m Path
forall (m :: * -> *). MonadPaths m => m Path
getDataDir
        (Path -> m Path
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Path -> m Path) -> (Text -> Path) -> Text -> m Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Path
coerce (String -> Path) -> (Text -> String) -> Text -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString)
        Maybe Text
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
fun
      r
z
      ([(Text, NixPathEntryType)] -> m r)
-> [(Text, NixPathEntryType)] -> m r
forall a b. (a -> b) -> a -> b
$ (Text -> (Text, NixPathEntryType)
fromInclude (Text -> (Text, NixPathEntryType))
-> (NixString -> Text) -> NixString -> (Text, NixPathEntryType)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
ignoreContext (NixString -> (Text, NixPathEntryType))
-> [NixString] -> [(Text, NixPathEntryType)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [NixString]
dirs)
        [(Text, NixPathEntryType)]
-> [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall a. Semigroup a => a -> a -> a
<> Text -> [(Text, NixPathEntryType)]
uriAwareSplit (Text -> [(Text, NixPathEntryType)])
-> Maybe Text -> [(Text, NixPathEntryType)]
forall b a. Monoid b => (a -> b) -> Maybe a -> b
`whenJust` Maybe Text
mPath
        [(Text, NixPathEntryType)]
-> [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall a. Semigroup a => a -> a -> a
<> OneItem [(Text, NixPathEntryType)] -> [(Text, NixPathEntryType)]
forall x. One x => OneItem x -> x
one (Text -> (Text, NixPathEntryType)
fromInclude (Text -> (Text, NixPathEntryType))
-> Text -> (Text, NixPathEntryType)
forall a b. (a -> b) -> a -> b
$ Text
"nix=" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. IsString a => String -> a
fromString (Path -> String
coerce Path
dataDir) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/nix/corepkgs")
 where

  fromInclude :: Text -> (Text, NixPathEntryType)
  fromInclude :: Text -> (Text, NixPathEntryType)
fromInclude Text
x =
    (Text
x, ) (NixPathEntryType -> (Text, NixPathEntryType))
-> NixPathEntryType -> (Text, NixPathEntryType)
forall a b. (a -> b) -> a -> b
$
      NixPathEntryType -> NixPathEntryType -> Bool -> NixPathEntryType
forall a. a -> a -> Bool -> a
bool
        NixPathEntryType
PathEntryPath
        NixPathEntryType
PathEntryURI
        (Text
"://" Text -> Text -> Bool
`Text.isInfixOf` Text
x)

  fun :: (Text, NixPathEntryType) -> r -> m r
  fun :: (Text, NixPathEntryType) -> r -> m r
fun (Text
x, NixPathEntryType
ty) r
rest =
    case Text -> Text -> [Text]
Text.splitOn Text
"=" Text
x of
      [Text
p] -> Path -> Maybe Text -> NixPathEntryType -> r -> m r
f (String -> Path
coerce (String -> Path) -> String -> Path
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. ToString a => a -> String
toString Text
p) Maybe Text
forall a. Monoid a => a
mempty NixPathEntryType
ty r
rest
      [Text
n, Text
p] -> Path -> Maybe Text -> NixPathEntryType -> r -> m r
f (String -> Path
coerce (String -> Path) -> String -> Path
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. ToString a => a -> String
toString Text
p) (Text -> Maybe Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Unexpected entry in NIX_PATH: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall b a. (Show a, IsString b) => a -> b
show Text
x

attrsetGet :: MonadNix e t f m => VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet :: VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet VarName
k AttrSet (NValue t f m)
s =
  m (NValue t f m)
-> (NValue t f m -> m (NValue t f m))
-> Maybe (NValue t f m)
-> m (NValue t f m)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ ToString Text => Text -> String
forall a. ToString a => a -> String
toString @Text (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text
"Attribute '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> VarName -> Text
coerce VarName
k Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"' required")
    NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (VarName -> 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 VarName
k AttrSet (NValue t f m)
s)

data VersionComponent
  = VersionComponentPre -- ^ The string "pre"
  | VersionComponentString !Text -- ^ A string other than "pre"
  | VersionComponentNumber !Integer -- ^ A number
  deriving (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)

instance Show VersionComponent where
  show :: VersionComponent -> String
show =
    \case
      VersionComponent
VersionComponentPre      -> String
"pre"
      VersionComponentString Text
s -> Text -> String
forall b a. (Show a, IsString b) => a -> b
show Text
s
      VersionComponentNumber Integer
n -> Integer -> String
forall b a. (Show a, IsString b) => a -> b
show Integer
n

splitVersion :: Text -> [VersionComponent]
splitVersion :: Text -> [VersionComponent]
splitVersion Text
s =
  (\ (Char
x, Text
xs) -> if
    | Either String (Integer, Text) -> Bool
forall a b. Either a b -> Bool
isRight Either String (Integer, Text)
eDigitsPart ->
        (String -> [VersionComponent])
-> ((Integer, Text) -> [VersionComponent])
-> Either String (Integer, Text)
-> [VersionComponent]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
          (\ String
e -> Text -> [VersionComponent]
forall a t. (HasCallStack, IsText t) => t -> a
error (Text -> [VersionComponent]) -> Text -> [VersionComponent]
forall a b. (a -> b) -> a -> b
$ Text
"splitVersion: did hit impossible: '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. IsString a => String -> a
fromString String
e Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"' while parsing '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
s Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'.")
          (\ (Integer, Text)
res ->
            OneItem [VersionComponent] -> [VersionComponent]
forall x. One x => OneItem x -> x
one (Integer -> VersionComponent
VersionComponentNumber (Integer -> VersionComponent) -> Integer -> VersionComponent
forall a b. (a -> b) -> a -> b
$ (Integer, Text) -> Integer
forall a b. (a, b) -> a
fst (Integer, Text)
res)
            [VersionComponent] -> [VersionComponent] -> [VersionComponent]
forall a. Semigroup a => a -> a -> a
<> Text -> [VersionComponent]
splitVersion ((Integer, Text) -> Text
forall a b. (a, b) -> b
snd (Integer, Text)
res)
          )
          Either String (Integer, Text)
eDigitsPart

    | Char
x Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` String
separators -> Text -> [VersionComponent]
splitVersion Text
xs

    | Bool
otherwise -> OneItem [VersionComponent] -> [VersionComponent]
forall x. One x => OneItem x -> x
one OneItem [VersionComponent]
VersionComponent
charsPart [VersionComponent] -> [VersionComponent] -> [VersionComponent]
forall a. Semigroup a => a -> a -> a
<> Text -> [VersionComponent]
splitVersion Text
rest2
  ) ((Char, Text) -> [VersionComponent])
-> Maybe (Char, Text) -> [VersionComponent]
forall b a. Monoid b => (a -> b) -> Maybe a -> b
`whenJust` Text -> Maybe (Char, Text)
Text.uncons Text
s
 where
  -- | Based on https://github.com/NixOS/nix/blob/4ee4fda521137fed6af0446948b3877e0c5db803/src/libexpr/names.cc#L44
  separators :: String
  separators :: String
separators = String
".-"

  eDigitsPart :: Either String (Integer, Text)
  eDigitsPart :: Either String (Integer, Text)
eDigitsPart = Integral Integer => Reader Integer
forall a. Integral a => Reader a
decimal @Integer Reader Integer -> Reader Integer
forall a b. (a -> b) -> a -> b
$ Text
s

  (Text
charsSpan, Text
rest2) =
    (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 -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` String
separators)
      Text
s

  charsPart :: VersionComponent
  charsPart :: VersionComponent
charsPart =
    case Text
charsSpan of
      Text
"pre" -> VersionComponent
VersionComponentPre
      Text
xs'   -> Text -> VersionComponent
VersionComponentString Text
xs'


compareVersions :: Text -> Text -> Ordering
compareVersions :: Text -> Text -> Ordering
compareVersions Text
s1 Text
s2 =
  [Ordering] -> Ordering
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold ([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
cmp ([VersionComponent] -> [VersionComponent] -> [Ordering])
-> (Text -> [VersionComponent]) -> Text -> Text -> [Ordering]
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Text -> [VersionComponent]
splitVersion) Text
s1 Text
s2
 where
  cmp :: These VersionComponent VersionComponent -> Ordering
  cmp :: These VersionComponent VersionComponent -> Ordering
cmp = (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))
-> VersionComponent
-> These VersionComponent VersionComponent
-> (VersionComponent, VersionComponent)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join VersionComponent
-> VersionComponent
-> These VersionComponent VersionComponent
-> (VersionComponent, VersionComponent)
forall a b. a -> b -> These a b -> (a, b)
fromThese (Text -> VersionComponent
VersionComponentString Text
forall a. Monoid a => a
mempty)

splitDrvName :: Text -> (Text, Text)
splitDrvName :: Text -> (Text, Text)
splitDrvName Text
s =
  ([Text] -> Text) -> ([Text], [Text]) -> (Text, Text)
forall a b. (a -> b) -> (a, a) -> (b, b)
both (Text -> [Text] -> Text
Text.intercalate Text
sep) ([Text]
namePieces, [Text]
versionPieces)
 where
  sep :: Text
sep    = Text
"-"
  pieces :: [Text]
  pieces :: [Text]
pieces = Text -> Text -> [Text]
Text.splitOn Text
sep Text
s
  isFirstVersionPiece :: Text -> Bool
  isFirstVersionPiece :: Text -> Bool
isFirstVersionPiece Text
p =
    Bool -> ((Char, Text) -> Bool) -> Maybe (Char, Text) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
      Bool
False
      (Char -> Bool
isDigit (Char -> Bool) -> ((Char, Text) -> Char) -> (Char, Text) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Text) -> Char
forall a b. (a, b) -> a
fst)
      (Text -> Maybe (Char, Text)
Text.uncons Text
p)
  -- 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 =
    ([a], [a]) -> ([a] -> ([a], [a])) -> [a] -> ([a], [a])
forall (t :: * -> *) b a. Foldable t => b -> (t a -> b) -> t a -> b
list
      ([a], [a])
forall a. Monoid a => a
mempty
      (\ (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

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 = OneItem [NValue t f m] -> [NValue t f m]
forall x. One x => OneItem x -> x
one (OneItem [NValue t f m] -> [NValue t f m])
-> OneItem [NValue t f m] -> [NValue t f m]
forall a b. (a -> b) -> a -> b
$ 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
_ =
  String -> [NValue t f m]
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Fail 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
caps           = [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
mkNVList ((ByteString, (Int, Int)) -> NValue t f m
forall b. (ByteString, (Int, b)) -> NValue t f m
f ((ByteString, (Int, Int)) -> NValue t f m)
-> [(ByteString, (Int, Int))] -> [NValue t f m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(ByteString, (Int, Int))]
captures)
  f :: (ByteString, (Int, b)) -> NValue t f m
  f :: (ByteString, (Int, b)) -> NValue t f m
f (ByteString
a, (Int
s, b
_))  =
    NValue t f m -> NValue t f m -> Bool -> NValue t f m
forall a. a -> a -> Bool -> a
bool
      NValue t f m
forall (f :: * -> *) t (m :: * -> *). Applicative f => NValue t f m
nvNull
      (ByteString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
ByteString -> NValue t f m
thunkStr ByteString
a)
      (Int
s Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0)

thunkStr :: Applicative f => ByteString -> NValue t f m
thunkStr :: ByteString -> NValue t f m
thunkStr ByteString
s = Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext (Text -> NValue t f m) -> Text -> NValue t f m
forall a b. (a -> b) -> a -> b
$ ByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 ByteString
s

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 =
  (Maybe a -> Bool) -> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(Maybe a1 -> a2) -> v -> m b
inHaskMay
    (Maybe a -> Bool
forall a. Maybe a -> Bool
isJust @a)


absolutePathFromValue :: MonadNix e t f m => NValue t f m -> m Path
absolutePathFromValue :: NValue t f m -> m Path
absolutePathFromValue =
  \case
    NVStr NixString
ns ->
      do
        let
          path :: Path
path = String -> Path
coerce (String -> Path) -> (Text -> String) -> Text -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString (Text -> Path) -> Text -> Path
forall a b. (a -> b) -> a -> b
$ NixString -> Text
ignoreContext NixString
ns

        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Path -> Bool
isAbsolute Path
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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"string " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Path -> String
forall b a. (Show a, IsString b) => a -> b
show Path
path String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" doesn't represent an absolute path"
        pure Path
path

    NVPath Path
path -> Path -> m Path
forall (f :: * -> *) a. Applicative f => a -> f a
pure Path
path
    NValue t f m
v           -> ErrorCall -> m Path
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ErrorCall -> m Path) -> ErrorCall -> m Path
forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"expected a path, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
v


data FileType
  = FileTypeRegular
  | FileTypeDirectory
  | FileTypeSymlink
  | FileTypeUnknown
  deriving (Int -> FileType -> ShowS
[FileType] -> ShowS
FileType -> String
(Int -> FileType -> ShowS)
-> (FileType -> String) -> ([FileType] -> ShowS) -> Show FileType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FileType] -> ShowS
$cshowList :: [FileType] -> ShowS
show :: FileType -> String
$cshow :: FileType -> String
showsPrec :: Int -> FileType -> ShowS
$cshowsPrec :: Int -> FileType -> ShowS
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
mkNixStringWithoutContext (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"

-- ** Builtin functions

derivationNix
  :: forall e t f m. (MonadNix e t f m, Scoped (NValue t f m) m)
  => m (NValue t f m)
derivationNix :: m (NValue t f m)
derivationNix = (NExprF (m (NValue t f m)) -> m (NValue t f m))
-> Fix NExprF -> m (NValue t f m)
forall (f :: * -> *) a. Functor f => (f a -> a) -> Fix f -> a
foldFix NExprF (m (NValue t f m)) -> m (NValue t f m)
forall v (m :: * -> *). MonadNixEval v m => NExprF (m v) -> m v
Eval.eval $$(do
    -- This is compiled in so that we only parse it once at compile-time.
    let Right 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|]
    [|| expr ||]
  )

nixPathNix :: forall e t f m . MonadNix e t f m => m (NValue t f m)
nixPathNix :: m (NValue t f m)
nixPathNix =
  ([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
mkNVList
    (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]
-> (Path
    -> Maybe Text
    -> NixPathEntryType
    -> [NValue t f m]
    -> m [NValue t f m])
-> m [NValue t f m]
forall e t (f :: * -> *) (m :: * -> *) r.
MonadNix e t f m =>
r -> (Path -> Maybe Text -> NixPathEntryType -> r -> m r) -> m r
foldNixPath [NValue t f m]
forall a. Monoid a => a
mempty ((Path
  -> Maybe Text
  -> NixPathEntryType
  -> [NValue t f m]
  -> m [NValue t f m])
 -> m [NValue t f m])
-> (Path
    -> Maybe Text
    -> NixPathEntryType
    -> [NValue t f m]
    -> m [NValue t f m])
-> m [NValue t f m]
forall a b. (a -> b) -> a -> b
$
        \Path
p Maybe Text
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
$
            NValue t f m -> [NValue t f m]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
              (PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet
                PositionSet
forall a. Monoid a => a
mempty
                ([(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
                  [case NixPathEntryType
ty of
                    NixPathEntryType
PathEntryPath -> (VarName
"path", Path -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Path -> NValue t f m
mkNVPath  Path
p)
                    NixPathEntryType
PathEntryURI  -> ( VarName
"uri", Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext (Text -> NValue t f m) -> Text -> NValue t f m
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a. IsString a => String -> a
fromString (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Path -> String
coerce Path
p)

                  , ( VarName
"prefix", Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext (Text -> NValue t f m) -> Text -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text
forall m. Monoid m => Maybe m -> m
maybeToMonoid Maybe Text
mn)
                  ]
                )
              )
            [NValue t f m] -> [NValue t f m] -> [NValue t f m]
forall a. Semigroup a => a -> a -> a
<> [NValue t f m]
rest

toStringNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
toStringNix :: NValue t f m -> m (NValue t f m)
toStringNix = 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 -> 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 -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> NValue t f m -> m NixString
forall e t (f :: * -> *) (m :: * -> *).
(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 -> NValue t f m -> m NixString
coerceAnyToNixString 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

hasAttrNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
hasAttrNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
hasAttrNix NValue t f m
x NValue t f m
y =
  do
    (Text -> VarName
coerce -> VarName
key) <- 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
x
    (AttrSet (NValue t f m)
aset, PositionSet
_) <- NValue t f m -> m (AttrSet (NValue t f m), PositionSet)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), PositionSet) NValue t f m
y

    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
$ VarName -> AttrSet (NValue t f m) -> Bool
forall k a. (Eq k, Hashable k) => k -> HashMap k a -> Bool
M.member VarName
key AttrSet (NValue t f m)
aset

hasContextNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
hasContextNix :: NValue t f m -> m (NValue t f m)
hasContextNix = (NixString -> Bool) -> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> a2) -> v -> m b
inHask NixString -> Bool
hasContext

getAttrNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
getAttrNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
getAttrNix NValue t f m
x NValue t f m
y =
  do
    (Text -> VarName
coerce -> VarName
key) <- 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
x
    (AttrSet (NValue t f m)
aset, PositionSet
_) <- NValue t f m -> m (AttrSet (NValue t f m), PositionSet)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), PositionSet) NValue t f m
y

    VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet VarName
key AttrSet (NValue t f m)
aset

unsafeDiscardOutputDependencyNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> m (NValue t f m)
unsafeDiscardOutputDependencyNix :: NValue t f m -> m (NValue t f m)
unsafeDiscardOutputDependencyNix NValue t f m
nv =
  do
    (HashSet StringContext
nc, Text
ns) <- (NixString -> HashSet StringContext
getStringContext (NixString -> HashSet StringContext)
-> (NixString -> Text)
-> NixString
-> (HashSet StringContext, Text)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& NixString -> Text
ignoreContext) (NixString -> (HashSet StringContext, Text))
-> m NixString -> m (HashSet StringContext, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
nv
    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
$ HashSet StringContext -> Text -> NixString
mkNixString ((StringContext -> StringContext)
-> HashSet StringContext -> HashSet StringContext
forall b a.
(Hashable b, Eq b) =>
(a -> b) -> HashSet a -> HashSet b
HS.map StringContext -> StringContext
discard HashSet StringContext
nc) Text
ns
 where
  discard :: StringContext -> StringContext
  discard :: StringContext -> StringContext
discard (StringContext ContextFlavor
AllOutputs VarName
a) = ContextFlavor -> VarName -> StringContext
StringContext ContextFlavor
DirectPath VarName
a
  discard StringContext
x                            = StringContext
x

unsafeGetAttrPosNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
unsafeGetAttrPosNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
unsafeGetAttrPosNix NValue t f m
nvX NValue t f m
nvY =
  do
    NValue t f m
x <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvX
    NValue t f m
y <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvY

    case (NValue t f m
x, NValue t f m
y) of
      (NVStr NixString
ns, NVSet PositionSet
apos AttrSet (NValue t f m)
_) ->
        m (NValue t f m)
-> (SourcePos -> m (NValue t f m))
-> Maybe SourcePos
-> m (NValue t f m)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
          (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
forall (f :: * -> *) t (m :: * -> *). Applicative f => NValue t f m
nvNull)
          SourcePos -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
          (VarName -> PositionSet -> Maybe SourcePos
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup @VarName (Text -> VarName
coerce (Text -> VarName) -> Text -> VarName
forall a b. (a -> b) -> a -> b
$ NixString -> Text
ignoreContext NixString
ns) PositionSet
apos)
      (NValue t f m, NValue t f m)
_xy -> 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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Invalid types for builtins.unsafeGetAttrPosNix: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (NValue t f m, NValue t f m) -> String
forall b a. (Show a, IsString b) => a -> b
show (NValue t f m, NValue t f m)
_xy

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

addNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
addNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
addNix NValue t f m
nvX NValue t f m
nvY =
  do
    NValue t f m
x' <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvX
    NValue t f m
y' <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvY

    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 -> m (NValue t f m)) -> Float -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$             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 (Float -> m (NValue t f m)) -> Float -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ 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 -> m (NValue t f m)) -> Float -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$             Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
+ Float
y
      (NValue t f m
_x                   , NValue t f m
_y                   ) -> 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

mulNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
mulNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
mulNix NValue t f m
nvX NValue t f m
nvY =
  do
    NValue t f m
x' <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvX
    NValue t f m
y' <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvY

    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
_x                   , NValue t f m
_y                   ) -> 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

divNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
divNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
divNix NValue t f m
nvX NValue t f m
nvY =
  do
    NValue t f m
x' <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvX
    NValue t f m
y' <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvY
    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 -> m (NValue t f m)) -> Float -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$                     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 (Float -> m (NValue t f m)) -> Float -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$         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 -> m (NValue t f m)) -> Float -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$                     Float
x Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
y
      (NValue t f m
_x                   , NValue t f m
_y                   )         -> 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

anyNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
anyNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
anyNix 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
anyMNix 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 :: * -> *) (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)
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
 where
  anyMNix :: Monad m => (a -> m Bool) -> [a] -> m Bool
  anyMNix :: (a -> m Bool) -> [a] -> m Bool
anyMNix a -> m Bool
_ []       = Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
  anyMNix a -> m Bool
p (a
x : [a]
xs) =
    m Bool -> m Bool -> Bool -> m Bool
forall a. a -> a -> Bool -> a
bool
      ((a -> m Bool) -> [a] -> m Bool
forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
anyMNix a -> m Bool
p [a]
xs)
      (Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True)
      (Bool -> m Bool) -> m Bool -> m Bool
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> m Bool
p a
x

allNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
allNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
allNix 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
allMNix 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 :: * -> *) (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)
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
 where
  allMNix :: Monad m => (a -> m Bool) -> [a] -> m Bool
  allMNix :: (a -> m Bool) -> [a] -> m Bool
allMNix a -> m Bool
_ []       = Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
  allMNix a -> m Bool
p (a
x : [a]
xs) =
    m Bool -> m Bool -> Bool -> m Bool
forall a. a -> a -> Bool -> a
bool
      (Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False)
      ((a -> m Bool) -> [a] -> m Bool
forall (m :: * -> *) a. Monad m => (a -> m Bool) -> [a] -> m Bool
allMNix a -> m Bool
p [a]
xs)
      (Bool -> m Bool) -> m Bool -> m Bool
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> m Bool
p a
x

foldl'Nix
  :: 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'Nix :: NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
foldl'Nix NValue t f m
f NValue t f m
z NValue t f m
xs =  (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 ([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 -> 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
 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 -> 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 (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 NValue t f m
b

headNix :: forall e t f m. MonadNix e t f m => NValue t f m -> m (NValue t f m)
headNix :: NValue t f m -> m (NValue t f m)
headNix =
  m (NValue t f m)
-> (NValue t f m -> m (NValue t f m))
-> Maybe (NValue t f m)
-> m (NValue t f m)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (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
$ String -> ErrorCall
ErrorCall String
"builtins.head: empty list")
    NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  (Maybe (NValue t f m) -> m (NValue t f m))
-> ([NValue t f m] -> Maybe (NValue t f m))
-> [NValue t f m]
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NonEmpty (NValue t f m) -> NValue t f m)
-> [NValue t f m] -> Maybe (NValue t f m)
forall a b. (NonEmpty a -> b) -> [a] -> Maybe b
viaNonEmpty NonEmpty (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. IsNonEmpty f a a "head" => f a -> a
head ([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]

tailNix :: forall e t f m. MonadNix e t f m => NValue t f m -> m (NValue t f m)
tailNix :: NValue t f m -> m (NValue t f m)
tailNix =
  m (NValue t f m)
-> ([NValue t f m] -> m (NValue t f m))
-> Maybe [NValue t f m]
-> m (NValue t f m)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (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
$ String -> ErrorCall
ErrorCall String
"builtins.tail: empty list")
    (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
mkNVList)
  (Maybe [NValue t f m] -> m (NValue t f m))
-> ([NValue t f m] -> Maybe [NValue t f m])
-> [NValue t f m]
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NonEmpty (NValue t f m) -> [NValue t f m])
-> [NValue t f m] -> Maybe [NValue t f m]
forall a b. (NonEmpty a -> b) -> [a] -> Maybe b
viaNonEmpty NonEmpty (NValue t f m) -> [NValue t f m]
forall (f :: * -> *) a. IsNonEmpty f a [a] "tail" => f a -> [a]
tail ([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]

splitVersionNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
splitVersionNix :: NValue t f m -> m (NValue t f m)
splitVersionNix NValue t f m
v =
  do
    Text
version <- 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
v
    pure $
      [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
mkNVList ([NValue t f m] -> NValue t f m) -> [NValue t f m] -> NValue t f m
forall a b. (a -> b) -> a -> b
$
        Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext (Text -> NValue t f m)
-> (VersionComponent -> Text) -> VersionComponent -> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VersionComponent -> Text
forall b a. (Show a, IsString b) => a -> b
show (VersionComponent -> NValue t f m)
-> [VersionComponent] -> [NValue t f m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
          Text -> [VersionComponent]
splitVersion Text
version

compareVersionsNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
compareVersionsNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
compareVersionsNix NValue t f m
t1 NValue t f m
t2 =
  do
    Text
s1 <- NValue t f m -> m Text
mkText NValue t f m
t1
    Text
s2 <- NValue t f m -> m Text
mkText NValue t f m
t2

    let
      cmpVers :: Integer
cmpVers =
        case Text -> Text -> Ordering
compareVersions Text
s1 Text
s2 of
          Ordering
LT -> -Integer
1
          Ordering
EQ -> Integer
0
          Ordering
GT -> Integer
1

    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
mkNVConstant (NAtom -> NValue t f m) -> NAtom -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Integer -> NAtom
NInt Integer
cmpVers

 where
  mkText :: NValue t f m -> m Text
mkText = NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext (NixString -> m Text)
-> (NValue t f m -> m NixString) -> NValue t f m -> m Text
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

parseDrvNameNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
parseDrvNameNix :: NValue t f m -> m (NValue t f m)
parseDrvNameNix NValue t f m
drvname =
  do
    Text
s <- 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
drvname

    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
$
      [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
        [ ( VarName
"name" :: VarName
          , Text -> NValue t f m
forall t (m :: * -> *). Text -> NValue t f m
mkNVStr Text
name
          )
        , ( VarName
"version"
          , Text -> NValue t f m
forall t (m :: * -> *). Text -> NValue t f m
mkNVStr Text
version
          )
        ]

 where
  mkNVStr :: Text -> NValue t f m
mkNVStr = Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext

matchNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
matchNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
matchNix NValue t f m
pat NValue t f m
str =
  do
    Text
p <- 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
pat
    NixString
ns <- NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
str

    -- NOTE: 2018-11-19: 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
ignoreContext NixString
ns
      re :: Regex
re = Text -> Regex
forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
source -> regex
makeRegex Text
p :: Regex
      mkMatch :: Text -> f (NValue t f m)
mkMatch Text
t =
        f (NValue t f m) -> f (NValue t f m) -> Bool -> f (NValue t f m)
forall a. a -> a -> Bool -> a
bool
          (NValue t f m -> f (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
forall (f :: * -> *) t (m :: * -> *). Applicative f => NValue t f m
nvNull)
          (NixString -> f (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> f (NValue t f m)) -> NixString -> f (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> NixString
mkNixStringWithoutContext Text
t)
          (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
Text.null Text
t)

    case Regex -> Text -> Maybe (Text, MatchText Text, Text)
forall regex source.
RegexLike regex source =>
regex -> source -> Maybe (source, MatchText source, source)
matchOnceText Regex
re Text
s of
      Just (Text
"", MatchText Text
sarr, Text
"") ->
        do
          let submatches :: [Text]
submatches = (Text, (Int, Int)) -> Text
forall a b. (a, b) -> a
fst ((Text, (Int, Int)) -> Text) -> [(Text, (Int, Int))] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MatchText Text -> [(Text, (Int, Int))]
forall i e. Array i e -> [e]
elems MatchText Text
sarr
          [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
mkNVList ([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
<$>
            (Text -> m (NValue t f m)) -> [Text] -> 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 (f :: * -> *) (f :: * -> *) t (m :: * -> *).
(Applicative f, Applicative f,
 ToValue NixString f (NValue t f m)) =>
Text -> f (NValue t f m)
mkMatch
              (case [Text]
submatches of
                 [] -> [Text]
forall a. Monoid a => a
mempty
                 [Text
a] -> OneItem [Text] -> [Text]
forall x. One x => OneItem x -> x
one Text
OneItem [Text]
a
                 Text
_:[Text]
xs -> [Text]
xs -- return only the matched groups, drop the full string
              )
      Maybe (Text, MatchText Text, Text)
_ -> NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
forall (f :: * -> *) t (m :: * -> *). Applicative f => NValue t f m
nvNull

splitNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
splitNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
splitNix NValue t f m
pat NValue t f m
str =
  do
    Text
p <- 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
pat
    NixString
ns <- NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
str
        -- 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
ignoreContext NixString
ns
      regex :: Regex
regex       = Text -> Regex
forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
source -> regex
makeRegex Text
p :: Regex
      haystack :: ByteString
haystack = Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
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
mkNVList ([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 (Array Int (ByteString, (Int, Int)) -> [(ByteString, (Int, Int))]
forall i e. Array i e -> [e]
elems (Array Int (ByteString, (Int, Int)) -> [(ByteString, (Int, Int))])
-> [Array Int (ByteString, (Int, Int))]
-> [[(ByteString, (Int, Int))]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Regex -> ByteString -> [Array Int (ByteString, (Int, Int))]
forall regex source.
RegexLike regex source =>
regex -> source -> [MatchText source]
matchAllText Regex
regex ByteString
haystack) ByteString
haystack

substringNix :: forall e t f m. MonadNix e t f m => Int -> Int -> NixString -> Prim m NixString
substringNix :: Int -> Int -> NixString -> Prim m NixString
substringNix 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
$
    m NixString -> m NixString -> Bool -> m NixString
forall a. a -> a -> Bool -> a
bool
      (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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.substring: negative start position: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall b a. (Show a, IsString b) => a -> b
show Int
start)
      (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
modifyNixContents (Text -> Text
take (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)
      (Int
start Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0)
 where
  take :: Text -> Text
take =
    (Text -> Text) -> (Text -> Text) -> Bool -> Text -> Text
forall a. a -> a -> Bool -> a
bool
      Text -> Text
forall a. a -> a
id  --NOTE: negative values of 'len' are OK, and mean "take everything"
      (Int -> Text -> Text
Text.take Int
len)
      (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0)

attrNamesNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
attrNamesNix :: NValue t f m -> m (NValue t f m)
attrNamesNix =
    m (Deeper (NValue t f m)) -> m (NValue t f m)
coersion (m (Deeper (NValue t f m)) -> m (NValue t f m))
-> (NValue t f m -> m (Deeper (NValue t f m)))
-> NValue t f m
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AttrSet (NValue t f m) -> [NixString])
-> NValue t f m -> m (Deeper (NValue t f m))
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> a2) -> v -> m b
inHask @(AttrSet (NValue t f m))
      ((VarName -> NixString) -> [VarName] -> [NixString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> NixString
mkNixStringWithoutContext (Text -> NixString) -> (VarName -> Text) -> VarName -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarName -> Text
coerce) ([VarName] -> [NixString])
-> (AttrSet (NValue t f m) -> [VarName])
-> AttrSet (NValue t f m)
-> [NixString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [VarName] -> [VarName]
forall a. Ord a => [a] -> [a]
sort ([VarName] -> [VarName])
-> (AttrSet (NValue t f m) -> [VarName])
-> AttrSet (NValue t f m)
-> [VarName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AttrSet (NValue t f m) -> [VarName]
forall k v. HashMap k v -> [k]
M.keys)
 where
  coersion :: m (Deeper (NValue t f m)) -> m (NValue t f m)
coersion = (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
coerce :: CoerceDeeperToNValue t f m)

attrValuesNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
attrValuesNix :: NValue t f m -> m (NValue t f m)
attrValuesNix NValue t f m
nvattrs =
  do
    AttrSet (NValue t f m)
attrs <- 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
nvattrs
    [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)
forall a b. (a -> b) -> a -> b
$
      (VarName, NValue t f m) -> NValue t f m
forall a b. (a, b) -> b
snd ((VarName, NValue t f m) -> NValue t f m)
-> [(VarName, NValue t f m)] -> [NValue t f m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        ((VarName, NValue t f m) -> VarName)
-> [(VarName, NValue t f m)] -> [(VarName, NValue t f m)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn
          ((VarName, NValue t f m) -> VarName
forall a b. (a, b) -> a
fst @VarName @(NValue t f m))
          (AttrSet (NValue t f m) -> [(VarName, NValue t f m)]
forall k v. HashMap k v -> [(k, v)]
M.toList AttrSet (NValue t f m)
attrs)

mapNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
mapNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
mapNix NValue t f m
f =
  ([NValue t f m] -> m [NValue t f m])
-> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> m a2) -> v -> m b
inHaskM @[NValue t f m]
    ((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
      (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))
-> (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 (String -> ErrorCall
ErrorCall String
"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 -> 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
      )
    )

mapAttrsNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
mapAttrsNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
mapAttrsNix NValue t f m
f NValue t f m
xs =
  do
    AttrSet (NValue t f m)
nixAttrset <- 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
    let
      keyVals :: [(VarName, NValue t f m)]
keyVals = AttrSet (NValue t f m) -> [(VarName, NValue t f m)]
forall k v. HashMap k v -> [(k, v)]
M.toList AttrSet (NValue t f m)
nixAttrset
      keys :: [VarName]
keys = (VarName, NValue t f m) -> VarName
forall a b. (a, b) -> a
fst ((VarName, NValue t f m) -> VarName)
-> [(VarName, NValue t f m)] -> [VarName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(VarName, NValue t f m)]
keyVals

      applyFunToKeyVal :: (VarName, NValue t f m) -> m (NValue t f m)
applyFunToKeyVal (VarName
key, NValue t f m
val) =
        do
          NValue t f m
runFunForKey <- 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)
forall a b. (a -> b) -> a -> b
$ Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext (VarName -> Text
coerce VarName
key)
          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
runFunForKey NValue t f m
val

    [NValue t f m]
newVals <-
      ((VarName, NValue t f m) -> m (NValue t f m))
-> [(VarName, 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))
-> ((VarName, NValue t f m) -> m (NValue t f m))
-> (VarName, 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 (String -> ErrorCall
ErrorCall String
"While applying f in mapAttrs:\n") (m (NValue t f m) -> m (NValue t f m))
-> ((VarName, NValue t f m) -> m (NValue t f m))
-> (VarName, NValue t f m)
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VarName, NValue t f m) -> m (NValue t f m)
applyFunToKeyVal)
        [(VarName, NValue t f m)]
keyVals

    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))
-> AttrSet (NValue t f m) -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [VarName] -> [NValue t f m] -> [(VarName, NValue t f m)]
forall a b. [a] -> [b] -> [(a, b)]
zip [VarName]
keys [NValue t f m]
newVals

filterNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
filterNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
filterNix NValue t f m
f =
  ([NValue t f m] -> m [NValue t f m])
-> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> m a2) -> v -> m b
inHaskM
    ((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
fh)
 where
  fh :: NValue t f m -> m Bool
  fh :: NValue t f m -> m Bool
fh = 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

catAttrsNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
catAttrsNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
catAttrsNix NValue t f m
attrName NValue t f m
xs =
  do
    Text
n <- 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
attrName
    [NValue t f m]
l <- 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

    [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
mkNVList ([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 ([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
<$>
      (NValue t f m -> m (Maybe (NValue t f m)))
-> [NValue t f m] -> m [Maybe (NValue t f m)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
        ((HashMap VarName (NValue t f m) -> Maybe (NValue t f m))
-> m (HashMap VarName (NValue t f m)) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall v.
(Eq VarName, Hashable VarName) =>
VarName -> HashMap VarName v -> Maybe v
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup @VarName (VarName -> HashMap VarName (NValue t f m) -> Maybe (NValue t f m))
-> VarName
-> HashMap VarName (NValue t f m)
-> Maybe (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> VarName
coerce Text
n) (m (HashMap VarName (NValue t f m)) -> m (Maybe (NValue t f m)))
-> (NValue t f m -> m (HashMap VarName (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 -> m (HashMap VarName (NValue t f m))
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m (Maybe (NValue t f m)))
-> (NValue t f m -> m (NValue t f m))
-> NValue t f m
-> m (Maybe (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 v (m :: * -> *). MonadValue v m => v -> m v
demand)
        [NValue t f m]
l

baseNameOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
baseNameOfNix :: NValue t f m -> m (NValue t f m)
baseNameOfNix NValue t f m
x =
  do
    NixString
ns <- CopyToStoreMode -> NValue t f m -> m NixString
forall e t (f :: * -> *) (m :: * -> *).
(Framed e m, MonadStore m, MonadThrow m,
 MonadDataErrorContext t f m, MonadValue (NValue t f m) m) =>
CopyToStoreMode -> NValue t f m -> m NixString
coerceStringlikeToNixString CopyToStoreMode
DontCopyToStore NValue t f m
x
    pure $
      NixString -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
NixString -> NValue t f m
mkNVStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$
        (Text -> Text) -> NixString -> NixString
modifyNixContents
          (String -> Text
forall a. IsString a => String -> a
fromString (String -> Text) -> (Text -> String) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Path -> Path) -> ShowS
coerce Path -> Path
takeFileName ShowS -> (Text -> String) -> Text -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString)
          NixString
ns

bitAndNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
bitAndNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
bitAndNix NValue t f m
x NValue t f m
y =
  do
    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
x
    Integer
b <- NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
y

    Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer -> m (NValue t f m)) -> Integer -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Integer
a Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
b

bitOrNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
bitOrNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
bitOrNix NValue t f m
x NValue t f m
y =
  do
    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
x
    Integer
b <- NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
y

    Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer -> m (NValue t f m)) -> Integer -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Integer
a Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
b

bitXorNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
bitXorNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
bitXorNix NValue t f m
x NValue t f m
y =
  do
    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
x
    Integer
b <- NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
y

    Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Integer -> m (NValue t f m)) -> Integer -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Integer
a Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
`xor` Integer
b

builtinsBuiltinNix
  :: forall e t f m
   . MonadNix e t f m
  => m (NValue t f m)
builtinsBuiltinNix :: m (NValue t f m)
builtinsBuiltinNix = 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
$ String -> ErrorCall
ErrorCall String
"HNix does not provide builtins.builtins at the moment. Using builtins directly should be preferred"

-- a safer version of `attrsetGet`
attrGetOr
  :: forall e t f m v a
   . (MonadNix e t f m, FromValue v m (NValue t f m))
  => a
  -> (v -> m a)
  -> VarName
  -> AttrSet (NValue t f m)
  -> m a
attrGetOr :: a -> (v -> m a) -> VarName -> AttrSet (NValue t f m) -> m a
attrGetOr a
fallback v -> m a
fun VarName
name AttrSet (NValue t f m)
attrs =
  m a -> (NValue t f m -> m a) -> Maybe (NValue t f m) -> m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
fallback)
    (v -> m a
fun (v -> m a) -> (NValue t f m -> m v) -> NValue t f m -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m v
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue)
    (VarName -> 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 VarName
name AttrSet (NValue t f m)
attrs)


--  NOTE: It is a part of the implementation taken from:
--  https://github.com/haskell-nix/hnix/pull/755
--  look there for `sha256` and/or `filterSource`
pathNix :: forall e t f m. MonadNix e t f m => NValue t f m -> m (NValue t f m)
pathNix :: NValue t f m -> m (NValue t f m)
pathNix NValue t f m
arg =
  do
    AttrSet (NValue t f m)
attrs <- 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
arg
    Path
path      <- (Text -> Path) -> m Text -> m Path
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Path
coerce (String -> Path) -> (Text -> String) -> Text -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString) (m Text -> m Path) -> m Text -> m Path
forall a b. (a -> b) -> a -> b
$ 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
coerceToPath (NValue t f m -> m NixString) -> m (NValue t f m) -> m NixString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet VarName
"path" AttrSet (NValue t f m)
attrs

    -- TODO: Fail on extra args
    -- XXX: This is a very common pattern, we could factor it out
    Text
name      <- Path -> Text
forall a. ToText a => a -> Text
toText (Path -> Text) -> m Path -> m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Path
-> (NixString -> m Path)
-> VarName
-> AttrSet (NValue t f m)
-> m Path
forall e t (f :: * -> *) (m :: * -> *) v a.
(MonadNix e t f m, FromValue v m (NValue t f m)) =>
a -> (v -> m a) -> VarName -> AttrSet (NValue t f m) -> m a
attrGetOr (Path -> Path
takeFileName Path
path) ((Text -> Path) -> m Text -> m Path
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Path
coerce (String -> Path) -> (Text -> String) -> Text -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString) (m Text -> m Path) -> (NixString -> m Text) -> NixString -> m Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext) VarName
"name" AttrSet (NValue t f m)
attrs
    Bool
recursive <- Bool
-> (Bool -> m Bool) -> VarName -> AttrSet (NValue t f m) -> m Bool
forall e t (f :: * -> *) (m :: * -> *) v a.
(MonadNix e t f m, FromValue v m (NValue t f m)) =>
a -> (v -> m a) -> VarName -> AttrSet (NValue t f m) -> m a
attrGetOr Bool
True Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure VarName
"recursive" AttrSet (NValue t f m)
attrs

    Right (Text -> VarName
coerce (Text -> VarName) -> (StorePath -> Text) -> StorePath -> VarName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. ToText a => a -> Text
toText (String -> Text) -> (StorePath -> String) -> StorePath -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coercible StorePath String => StorePath -> String
coerce @StorePath @String -> VarName
s) <- Text -> Path -> Bool -> Bool -> m (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadStore m =>
Text -> Path -> Bool -> Bool -> m (Either ErrorCall StorePath)
addToStore Text
name Path
path Bool
recursive Bool
False
    -- TODO: Ensure that s matches sha256 when not empty
    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
mkNVStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ StringContext -> VarName -> NixString
mkNixStringWithSingletonContext (ContextFlavor -> VarName -> StringContext
StringContext ContextFlavor
DirectPath VarName
s) VarName
s
 where
  coerceToPath :: NValue t f m -> m NixString
coerceToPath = (NValue t f m -> NValue t f m -> m (NValue t f m))
-> CopyToStoreMode -> CoercionLevel -> NValue t f m -> m NixString
forall e t (f :: * -> *) (m :: * -> *).
(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

dirOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
dirOfNix :: NValue t f m -> m (NValue t f m)
dirOfNix NValue t f m
nvdir =
  do
    NValue t f m
dir <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvdir

    case NValue t f m
dir of
      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
mkNVStr (NixString -> NValue t f m) -> NixString -> NValue t f m
forall a b. (a -> b) -> a -> b
$ (Text -> Text) -> NixString -> NixString
modifyNixContents (String -> Text
forall a. IsString a => String -> a
fromString (String -> Text) -> (Text -> String) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Path -> Path) -> ShowS
coerce Path -> Path
takeDirectory ShowS -> (Text -> String) -> Text -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString) NixString
ns
      NVPath Path
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
$ Path -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Path -> NValue t f m
mkNVPath (Path -> NValue t f m) -> Path -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Path -> Path
takeDirectory Path
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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"dirOf: expected string or path, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
v

-- jww (2018-04-28): This should only be a string argument, and not coerced?
unsafeDiscardStringContextNix
  :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
unsafeDiscardStringContextNix :: NValue t f m -> m (NValue t f m)
unsafeDiscardStringContextNix =
  (NixString -> NixString) -> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> a2) -> v -> m b
inHask (Text -> NixString
mkNixStringWithoutContext (Text -> NixString)
-> (NixString -> Text) -> NixString -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
ignoreContext)

-- | Evaluate `a` to WHNF to collect its topmost effect.
seqNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
seqNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
seqNix NValue t f m
a NValue t f m
b = NValue t f m
b NValue t f m -> m (NValue t f m) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
a

-- | Evaluate 'a' to NF to collect all of its effects, therefore data cycles are ignored.
deepSeqNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
deepSeqNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
deepSeqNix 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

elemNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
elemNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
elemNix NValue t f m
x = ([NValue t f m] -> m Bool) -> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> m a2) -> v -> m b
inHaskM ((NValue t f m -> m Bool) -> [NValue t f m] -> m Bool
forall a. Monad m => (a -> m Bool) -> [a] -> m Bool
anyMNix ((NValue t f m -> m Bool) -> [NValue t f m] -> m Bool)
-> (NValue t f m -> m Bool) -> [NValue t f m] -> m Bool
forall a b. (a -> b) -> a -> b
$ NValue t f m -> NValue t f m -> m Bool
forall t (m :: * -> *) (f :: * -> *).
(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)
 where
  anyMNix :: Monad m => (a -> m Bool) -> [a] -> m Bool
  anyMNix :: (a -> m Bool) -> [a] -> m Bool
anyMNix a -> m Bool
p =
    m Bool -> ([a] -> m Bool) -> [a] -> m Bool
forall (t :: * -> *) b a. Foldable t => b -> (t a -> b) -> t a -> b
list
      (Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False)
      (\ (a
x : [a]
xss) ->
        m Bool -> m Bool -> Bool -> m Bool
forall a. a -> a -> Bool -> a
bool
          ((a -> m Bool) -> [a] -> m Bool
forall a. Monad m => (a -> m Bool) -> [a] -> m Bool
anyMNix a -> m Bool
p [a]
xss)
          (Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True)
          (Bool -> m Bool) -> m Bool -> m Bool
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> m Bool
p a
x
      )

elemAtNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
elemAtNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
elemAtNix NValue t f m
xs NValue t f m
n =
  do
    Int
n' <- NValue t f m -> m Int
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
n
    [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
xs
    m (NValue t f m)
-> (NValue t f m -> m (NValue t f m))
-> Maybe (NValue t f m)
-> m (NValue t f m)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
      (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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.elem: Index " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall b a. (Show a, IsString b) => a -> b
show Int
n' String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" too large for list of length " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall b a. (Show a, IsString b) => a -> b
show ([NValue t f m] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NValue t f m]
xs'))
      NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      ([NValue t f m]
xs' [NValue t f m] -> Int -> Maybe (NValue t f m)
forall a. [a] -> Int -> Maybe a
!!? Int
n')

genListNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
genListNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
genListNix NValue t f m
f NValue t f m
nixN =
  do
    Integer
n <- NValue t f m -> m Integer
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @Integer NValue t f m
nixN
    m (NValue t f m) -> m (NValue t f m) -> Bool -> m (NValue t f m)
forall a. a -> a -> Bool -> a
bool
      (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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.genList: Expected a non-negative number, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall b a. (Show a, IsString b) => a -> b
show Integer
n)
      ([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 -> m (NValue t f m)) -> [Integer] -> m [NValue t f m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (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))
-> (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 -> 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))
-> (Integer -> m (NValue t f m)) -> Integer -> m (NValue t f m)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Integer -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue) [Integer
0 .. Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1])
      (Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0)

genericClosureNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
genericClosureNix :: NValue t f m -> m (NValue t f m)
genericClosureNix NValue t f m
c =
  do
  AttrSet (NValue t f m)
s <- 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
c

  case (VarName -> 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 VarName
"startSet" AttrSet (NValue t f m)
s, VarName -> 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 VarName
"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
$ String -> ErrorCall
ErrorCall String
"builtins.genericClosure: 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
$ String -> ErrorCall
ErrorCall String
"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
$ String -> ErrorCall
ErrorCall String
"builtins.genericClosure: Attribute 'operator' required"
    (Just NValue t f m
startSet, Just NValue t f m
operator) ->
      do
        [NValue t f m]
ss <- 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 -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
startSet
        NValue t f m
op <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
operator
        let
          go
            :: Set (WValue t f m)
            -> [NValue t f m]
            -> m (Set (WValue t f m), [NValue t f m])
          go :: Set (WValue t f m)
-> [NValue t f m] -> m (Set (WValue t f m), [NValue t f m])
go 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, [NValue t f m]
forall a. Monoid a => a
mempty)
          go Set (WValue t f m)
ks (NValue t f m
t : [NValue t f m]
ts) =
            do
              NValue t f m
v <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
t
              NValue t f m
k <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand (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
=<< VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet VarName
"key" (AttrSet (NValue t f m) -> m (NValue t f m))
-> m (AttrSet (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 -> 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 (Set (WValue t f m), [NValue t f m])
-> m (Set (WValue t f m), [NValue t f m])
-> Bool
-> m (Set (WValue t f m), [NValue t f m])
forall a. a -> a -> Bool -> a
bool
                (do
                  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 -> m ()) -> NValue t f m -> m ()
forall a b. (a -> b) -> a -> b
$
                    NValue t f m
-> ([WValue t f m] -> NValue t f m)
-> [WValue t f m]
-> NValue t f m
forall (t :: * -> *) b a. Foldable t => b -> (t a -> b) -> t a -> b
list
                      NValue t f m
k
                      (\ (WValue NValue t f m
j:[WValue t f m]
_) -> NValue t f m
j)
                      (Set (WValue t f m) -> [WValue t f m]
forall a. Set a -> [a]
S.toList 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 (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
(<<$>>) (NValue t f m
v NValue t f m -> [NValue t f m] -> [NValue t f m]
forall a. a -> [a] -> [a]
:) (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]))
-> [NValue t f m]
-> m (Set (WValue t f m), [NValue t f m])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set (WValue t f m)
-> [NValue t f m] -> m (Set (WValue t f m), [NValue t f m])
go (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) ([NValue t f m] -> m (Set (WValue t f m), [NValue t f m]))
-> ([NValue t f m] -> [NValue t f m])
-> [NValue t f m]
-> m (Set (WValue t f m), [NValue t f m])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NValue t f m] -> [NValue t f m] -> [NValue t f m]
forall a. Semigroup a => a -> a -> a
(<>) [NValue t f m]
ts ([NValue t f m] -> m (Set (WValue t f m), [NValue t f m]))
-> m [NValue t f m] -> m (Set (WValue t f m), [NValue t f m])
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m 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])
-> 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
op NValue t f m
v
                )
                (Set (WValue t f m)
-> [NValue t f m] -> m (Set (WValue t f m), [NValue t f m])
go Set (WValue t f m)
ks [NValue t f m]
ts)
                (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)

        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
<$> Set (WValue t f m)
-> [NValue t f m] -> m (Set (WValue t f m), [NValue t f m])
go Set (WValue t f m)
forall a. Monoid a => a
mempty [NValue t f m]
ss

-- | Takes:
-- 1. List of strings to match.
-- 2. List of strings to replace corresponding match occurance. (arg 1 & 2 lists matched by index)
-- 3. String to process
-- -> returns the string with requested replacements.
--
-- Example:
-- builtins.replaceStrings ["ll" "e"] [" " "i"] "Hello world" == "Hi o world".
replaceStringsNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
replaceStringsNix :: NValue t f m -> NValue t f m -> NValue t f m -> m (NValue t f m)
replaceStringsNix NValue t f m
tfrom NValue t f m
tto NValue t f m
ts =
  do
    -- NixStrings have context - remember
    ([NixString]
fromKeys :: [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
tfrom)
    ([NixString]
toVals   :: [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)
    (NixString
string   ::  NixString ) <- NValue t f m -> m NixString
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
ts

    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([NixString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NixString]
fromKeys Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [NixString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NixString]
toVals) (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
$ String -> ErrorCall
ErrorCall String
"builtins.replaceStrings: Arguments `from`&`to` construct a key-value map, so the number of their elements must always match."

    let
      --  2021-02-18: NOTE: if there is no match - the process does not changes the context, simply slides along the string.
      --  So it isbe more effective to pass the context as the first argument.
      --  And moreover, the `passOneCharNgo` passively passes the context, to context can be removed from it and inherited directly.
      --  Then the solution would've been elegant, but the Nix bug prevents elegant implementation.
      go :: HashSet StringContext -> Text -> Builder -> NixString
go HashSet StringContext
ctx Text
input Builder
output =
        NixString
-> ((Text, NixString, Text) -> NixString)
-> Maybe (Text, NixString, Text)
-> NixString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
            -- Passively pass the chars
          NixString
passOneChar
          (Text, NixString, Text) -> NixString
replace
          Maybe (Text, NixString, Text)
maybePrefixMatch

       where
        -- When prefix matched something - returns (match, replacement, remainder)
        maybePrefixMatch :: Maybe (Text, NixString, Text)
        maybePrefixMatch :: Maybe (Text, NixString, Text)
maybePrefixMatch = (Text, NixString) -> (Text, NixString, Text)
formMatchReplaceTailInfo ((Text, NixString) -> (Text, NixString, Text))
-> Maybe (Text, NixString) -> Maybe (Text, NixString, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((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
input) (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)]
fromKeysToValsMap
         where
          formMatchReplaceTailInfo :: (Text, NixString) -> (Text, NixString, Text)
formMatchReplaceTailInfo (Text
m, NixString
r) = (Text
m, NixString
r, Int -> Text -> Text
Text.drop (Text -> Int
Text.length Text
m) Text
input)

          fromKeysToValsMap :: [(Text, NixString)]
fromKeysToValsMap = [Text] -> [NixString] -> [(Text, NixString)]
forall a b. [a] -> [b] -> [(a, b)]
zip (NixString -> Text
ignoreContext (NixString -> Text) -> [NixString] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [NixString]
fromKeys) [NixString]
toVals

        -- Not passing args => It is constant that gets embedded into `go` => It is simple `go` tail recursion
        passOneChar :: NixString
passOneChar =
          NixString
-> ((Char, Text) -> NixString) -> Maybe (Char, Text) -> NixString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
            (HashSet StringContext -> Builder -> NixString
finish HashSet StringContext
ctx Builder
output)  -- The base case - there is no chars left to process -> finish
            (\(Char
c, Text
i) -> HashSet StringContext -> Text -> Builder -> NixString
go HashSet StringContext
ctx Text
i (Builder
output Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Char -> Builder
Builder.singleton Char
c)) -- If there are chars - pass one char & continue
            (Text -> Maybe (Char, Text)
Text.uncons Text
input)  -- chip first char

        --  2021-02-18: NOTE: rly?: toStrict . toLazyText
        --  Maybe `text-builder`, `text-show`?
        finish :: HashSet StringContext -> Builder -> NixString
finish HashSet StringContext
ctx Builder
output = HashSet StringContext -> Text -> NixString
mkNixString HashSet StringContext
ctx (Text -> Text
forall l s. LazyStrict l s => l -> s
toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Builder -> Text
Builder.toLazyText Builder
output)

        replace :: (Text, NixString, Text) -> NixString
replace (Text
key, NixString
replacementNS, Text
unprocessedInput) = Text -> Builder -> NixString
replaceWithNixBug Text
unprocessedInput Builder
updatedOutput

         where
          replaceWithNixBug :: Text -> Builder -> NixString
replaceWithNixBug =
            (Text -> Builder -> NixString)
-> (Text -> Builder -> NixString)
-> Bool
-> Text
-> Builder
-> NixString
forall a. a -> a -> Bool -> a
bool
              (HashSet StringContext -> Text -> Builder -> NixString
go HashSet StringContext
updatedCtx)  -- tail recursion
              -- Allowing match on "" is a inherited bug of Nix,
              -- when "" is checked - it always matches. And so - when it checks - it always insers a replacement, and then process simply passesthrough the char that was under match.
              --
              -- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
              -- " H e l l o   w o r l d "
              -- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
              -- "iHie ioi iwioirilidi"
              --  2021-02-18: NOTE: There is no tests for this
              Text -> Builder -> NixString
bugPassOneChar  -- augmented recursion
              Bool
isNixBugCase

          isNixBugCase :: Bool
isNixBugCase = Text
key Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
forall a. Monoid a => a
mempty

          updatedOutput :: Builder
updatedOutput  = Builder
output Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
replacement
          updatedCtx :: HashSet StringContext
updatedCtx     = HashSet StringContext
ctx HashSet StringContext
-> HashSet StringContext -> HashSet StringContext
forall a. Semigroup a => a -> a -> a
<> HashSet StringContext
replacementCtx

          replacement :: Builder
replacement    = Text -> Builder
Builder.fromText (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ NixString -> Text
ignoreContext NixString
replacementNS
          replacementCtx :: HashSet StringContext
replacementCtx = NixString -> HashSet StringContext
getStringContext NixString
replacementNS

          -- The bug modifies the content => bug demands `pass` to be a real function =>
          -- `go` calls `pass` function && `pass` calls `go` function
          -- => mutual recusion case, so placed separately.
          bugPassOneChar :: Text -> Builder -> NixString
bugPassOneChar Text
input Builder
output =
            NixString
-> ((Char, Text) -> NixString) -> Maybe (Char, Text) -> NixString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
              (HashSet StringContext -> Builder -> NixString
finish HashSet StringContext
updatedCtx Builder
output)  -- The base case - there is no chars left to process -> finish
              (\(Char
c, Text
i) -> HashSet StringContext -> Text -> Builder -> NixString
go HashSet StringContext
updatedCtx Text
i (Builder -> NixString) -> Builder -> NixString
forall a b. (a -> b) -> a -> b
$ Builder
output Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Char -> Builder
Builder.singleton Char
c) -- If there are chars - pass one char & continue
              (Text -> Maybe (Char, Text)
Text.uncons Text
input)  -- chip first char

    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
$ HashSet StringContext -> Text -> Builder -> NixString
go (NixString -> HashSet StringContext
getStringContext NixString
string) (NixString -> Text
ignoreContext NixString
string) Builder
forall a. Monoid a => a
mempty

removeAttrsNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
removeAttrsNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
removeAttrsNix NValue t f m
set NValue t f m
v =
  do
    (AttrSet (NValue t f m)
m, PositionSet
p) <- NValue t f m -> m (AttrSet (NValue t f m), PositionSet)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), PositionSet) NValue t f m
set
    ([NixString]
nsToRemove :: [NixString]) <- 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])
-> Deeper (NValue t f m) -> m [NixString]
forall a b. (a -> b) -> a -> b
$ NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper NValue t f m
v
    ([Text] -> [VarName]
coerce -> [VarName]
toRemove) <- (NixString -> m Text) -> [NixString] -> m [Text]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NixString -> m Text
forall e (m :: * -> *). Framed e m => NixString -> m Text
fromStringNoContext [NixString]
nsToRemove
    (AttrSet (NValue t f m), PositionSet) -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (AttrSet (NValue t f m) -> [VarName] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => HashMap k v -> [k] -> HashMap k v
fun AttrSet (NValue t f m)
m [VarName]
toRemove, PositionSet -> [VarName] -> PositionSet
forall k v. (Eq k, Hashable k) => HashMap k v -> [k] -> HashMap k v
fun PositionSet
p [VarName]
toRemove)
 where
  fun :: forall k v . (Eq k, Hashable k) => HashMap k v -> [k] -> HashMap k v
  fun :: HashMap k v -> [k] -> HashMap k v
fun = (HashMap k v -> k -> HashMap k v)
-> HashMap k v -> [k] -> HashMap k v
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((k -> HashMap k v -> HashMap k v)
-> HashMap k v -> k -> HashMap k v
forall a b c. (a -> b -> c) -> b -> a -> c
flip k -> HashMap k v -> HashMap k v
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
M.delete)

intersectAttrsNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
intersectAttrsNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
intersectAttrsNix NValue t f m
set1 NValue t f m
set2 =
  do
    (AttrSet (NValue t f m)
s1, PositionSet
p1) <- NValue t f m -> m (AttrSet (NValue t f m), PositionSet)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), PositionSet) NValue t f m
set1
    (AttrSet (NValue t f m)
s2, PositionSet
p2) <- NValue t f m -> m (AttrSet (NValue t f m), PositionSet)
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue @(AttrSet (NValue t f m), PositionSet) NValue t f m
set2

    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
$ PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet (PositionSet
p2 PositionSet -> PositionSet -> PositionSet
forall k v w.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k w -> HashMap k v
`M.intersection` PositionSet
p1) (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)

functionArgsNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
functionArgsNix :: NValue t f m -> m (NValue t f m)
functionArgsNix NValue t f m
nvfun =
  do
    NValue t f m
fun <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvfun
    case NValue t f m
fun of
      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
$ Bool -> NValue t f m
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Bool -> NValue t f m
mkNVBool (Bool -> NValue t f m)
-> HashMap VarName Bool -> AttrSet (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
          case Params ()
p of
            Param VarName
name     -> OneItem (HashMap VarName Bool) -> HashMap VarName Bool
forall x. One x => OneItem x -> x
one (VarName
name, Bool
False)
            ParamSet Maybe VarName
_ Variadic
_ ParamSet ()
pset -> Maybe () -> Bool
forall a. Maybe a -> Bool
isJust (Maybe () -> Bool)
-> HashMap VarName (Maybe ()) -> HashMap VarName Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParamSet () -> HashMap VarName (Maybe ())
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ParamSet ()
pset
      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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.functionArgs: expected function, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
_v

toFileNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
toFileNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
toFileNix 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
    StorePath
mres  <-
      Path -> Text -> m StorePath
forall e (m :: * -> *).
(Framed e m, MonadStore m) =>
Path -> Text -> m StorePath
toFile_
        (String -> Path
coerce (String -> Path) -> String -> Path
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. ToString a => a -> String
toString Text
name')
        (NixString -> Text
ignoreContext NixString
s')

    let
      storepath :: VarName
storepath  = (String -> Text) -> StorePath -> VarName
coerce (IsString Text => String -> Text
forall a. IsString a => String -> a
fromString @Text) StorePath
mres
      sc :: StringContext
sc = ContextFlavor -> VarName -> StringContext
StringContext ContextFlavor
DirectPath VarName
storepath

    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
$ StringContext -> VarName -> NixString
mkNixStringWithSingletonContext StringContext
sc VarName
storepath

toPathNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
toPathNix :: NValue t f m -> m (NValue t f m)
toPathNix = (Path -> Path) -> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> a2) -> v -> m b
inHask @Path Path -> Path
forall a. a -> a
id

pathExistsNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
pathExistsNix :: NValue t f m -> m (NValue t f m)
pathExistsNix NValue t f m
nvpath =
  do
    NValue t f m
path <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvpath
    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
=<<
      case NValue t f m
path of
        NVPath Path
p  -> Path -> m Bool
forall (m :: * -> *). MonadFile m => Path -> m Bool
doesPathExist Path
p
        NVStr  NixString
ns -> Path -> m Bool
forall (m :: * -> *). MonadFile m => Path -> m Bool
doesPathExist (Path -> m Bool) -> Path -> m Bool
forall a b. (a -> b) -> a -> b
$ String -> Path
coerce (String -> Path) -> String -> Path
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. ToString a => a -> String
toString (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ NixString -> Text
ignoreContext NixString
ns
        NValue t f m
_v -> 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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.pathExists: expected path, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
_v

isPathNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isPathNix :: NValue t f m -> m (NValue t f m)
isPathNix = 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 Path m (NValue t f m)) =>
NValue t f m -> m (NValue t f m)
hasKind @Path

isAttrsNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isAttrsNix :: NValue t f m -> m (NValue t f m)
isAttrsNix = 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))

isListNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isListNix :: NValue t f m -> m (NValue t f m)
isListNix = 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]

isIntNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isIntNix :: NValue t f m -> m (NValue t f m)
isIntNix = 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

isFloatNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isFloatNix :: NValue t f m -> m (NValue t f m)
isFloatNix = 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

isBoolNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isBoolNix :: NValue t f m -> m (NValue t f m)
isBoolNix = 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

isNullNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
isNullNix :: NValue t f m -> m (NValue t f m)
isNullNix = 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 @()

-- isString cannot use `hasKind` because it coerces derivationNixs to strings.
isStringNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
isStringNix :: NValue t f m -> m (NValue t f m)
isStringNix NValue t f m
nv =
  do
    NValue t f m
v <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nv

    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
$
      case NValue t f m
v of
        NVStr{} -> Bool
True
        NValue t f m
_       -> Bool
False

isFunctionNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
isFunctionNix :: NValue t f m -> m (NValue t f m)
isFunctionNix NValue t f m
nv =
  do
    NValue t f m
v <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nv

    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
$
      case NValue t f m
v of
        NVClosure{} -> Bool
True
        NValue t f m
_           -> Bool
False

throwNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
throwNix :: NValue t f m -> m (NValue t f m)
throwNix =
  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))
-> (NixString -> ErrorCall) -> NixString -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ErrorCall
ErrorCall (String -> ErrorCall)
-> (NixString -> String) -> NixString -> ErrorCall
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString (Text -> String) -> (NixString -> Text) -> NixString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
ignoreContext
    (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
<=< CopyToStoreMode -> NValue t f m -> m NixString
forall e t (f :: * -> *) (m :: * -> *).
(Framed e m, MonadStore m, MonadThrow m,
 MonadDataErrorContext t f m, MonadValue (NValue t f m) m) =>
CopyToStoreMode -> NValue t f m -> m NixString
coerceStringlikeToNixString CopyToStoreMode
CopyToStore

-- | Implementation of Nix @import@ clause.
--
-- Because Nix @import@s work strictly
-- (import gets fully evaluated befor bringing it into the scope it was called from)
-- - that property raises a requirement for execution phase of the interpreter go into evaluation phase
-- & then also go into parsing phase on the imports.
-- So it is not possible (more precise - not practical) to do a full parse Nix code phase fully & then go into evaluation phase.
-- As it is not possible to "import them lazily", as import is strict & it is not possible to establish
-- what imports whould be needed up until where it would be determined & they import strictly
--
importNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
importNix :: NValue t f m -> m (NValue t f m)
importNix = 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)
scopedImportNix (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 a b. (a -> b) -> a -> b
$ PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet PositionSet
forall a. Monoid a => a
mempty AttrSet (NValue t f m)
forall a. Monoid a => a
mempty

-- | @scopedImport scope path@
-- An undocumented secret powerful function.
--
-- At the same time it is strongly forbidden to be used, as prolonged use of it would bring devastating consequences.
-- As it is essentially allows rewriting(redefinition) paradigm.
--
-- Allows to import the environment into the scope of a file expression that gets imported.
-- It is as if the contents at @path@ were given to @import@ wrapped as: @with scope; path@
-- meaning:
--
-- > -- Nix pseudocode:
-- > import (with scope; path)
--
-- For example, it allows to use itself as:
-- > bar = scopedImport pkgs ./bar.nix;
-- > -- & declare @./bar.nix@ without a header, so as:
-- > stdenv.mkDerivation { ... buildInputs = [ libfoo ]; }
--
-- But that breaks the evaluation/execution sharing of the @import@s.
--
-- Function also allows to redefine or extend the builtins.
--
-- For instance, to trace all calls to function ‘map’:
--
-- >  let
-- >    overrides = {
-- >      map = f: xs: builtins.trace "call of map!" (map f xs);
--
-- >      # Propagate override by calls to import&scopedImport.
-- >      import = fn: scopedImport overrides fn;
-- >      scopedImport = attrs: fn: scopedImport (overrides // attrs) fn;
--
-- >      # Update ‘builtins’.
-- >      builtins = builtins // overrides;
-- >    };
-- >  in scopedImport overrides ./bla.nix
--
-- In the related matter the function can be added and passed around as builtin.
scopedImportNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
scopedImportNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
scopedImportNix NValue t f m
asetArg NValue t f m
pathArg =
  do
    (AttrSet (NValue t f m) -> Scope (NValue t f m)
coerce -> Scope (NValue t f m)
scope) <- 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
    Path
p <- NValue t f m -> m Path
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue t f m
pathArg

    Path
path  <- Path -> m Path
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
Path -> m Path
pathToDefaultNix @t @f @m Path
p
    Path
path' <-
      m Path
-> (NValue t f m -> m Path) -> Maybe (NValue t f m) -> m Path
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        (do
          String -> m ()
forall (m :: * -> *). Monad m => String -> m ()
traceM String
"No known current directory"
          pure Path
path
        )
        (\ NValue t f m
res ->
          do
            Path
p' <- 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) -> m (NValue t f m) -> m Path
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
res

            String -> m ()
forall (m :: * -> *). Monad m => String -> m ()
traceM (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ String
"Current file being evaluated is: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Path -> String
forall b a. (Show a, IsString b) => a -> b
show Path
p'
            pure $ Path -> Path
takeDirectory Path
p' Path -> Path -> Path
</> Path
path
        )
        (Maybe (NValue t f m) -> m Path)
-> m (Maybe (NValue t f m)) -> m Path
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< VarName -> m (Maybe (NValue t f m))
forall a (m :: * -> *). Scoped a m => VarName -> m (Maybe a)
lookupVar VarName
"__cur_file"

    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 Path -> 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 Path -> m r -> m r
withNixContext (Path -> Maybe Path
forall (f :: * -> *) a. Applicative f => a -> f a
pure Path
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
$ Scope (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
forall a (m :: * -> *) r. Scoped a m => Scope a -> m r -> m r
pushScope Scope (NValue t f m)
scope
      (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
$ Path -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
Path -> m (NValue t f m)
importPath @t @f @m Path
path'

getEnvNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
getEnvNix :: NValue t f m -> m (NValue t f m)
getEnvNix NValue t f m
v =
  (NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m))
-> (Maybe Text -> NixString) -> Maybe Text -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
mkNixStringWithoutContext (Text -> NixString)
-> (Maybe Text -> Text) -> Maybe Text -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Text -> Text
forall m. Monoid m => Maybe m -> m
maybeToMonoid) (Maybe Text -> m (NValue t f m))
-> m (Maybe Text) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> m (Maybe Text)
forall (m :: * -> *). MonadEnv m => Text -> m (Maybe Text)
getEnvVar (Text -> m (Maybe Text)) -> m Text -> m (Maybe Text)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< 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
v

sortNix
  :: forall e t f m
  . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
sortNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
sortNix NValue t f m
comp =
  ([NValue t f m] -> m [NValue t f m])
-> NValue t f m -> m (NValue t f m)
forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> m a2) -> v -> m b
inHaskM ((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 -> m Ordering
cmp)
 where
  cmp :: NValue t f m -> NValue t f m -> m Ordering
  cmp :: NValue t f m -> NValue t f m -> m Ordering
cmp NValue t f m
a NValue t f m
b =
    m Ordering -> m Ordering -> Bool -> m Ordering
forall a. a -> a -> Bool -> a
bool
      ((Bool -> Ordering) -> m Bool -> m Ordering
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
         (Ordering -> Ordering -> Bool -> Ordering
forall a. a -> a -> Bool -> a
bool Ordering
EQ Ordering
GT)
         (NValue t f m -> NValue t f m -> m Bool
compare NValue t f m
b NValue t f m
a)
      )
      (Ordering -> m Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure Ordering
LT)
      (Bool -> m Ordering) -> m Bool -> m Ordering
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> NValue t f m -> m Bool
compare NValue t f m
a NValue t f m
b
   where
    compare :: NValue t f m -> NValue t f m -> m Bool
    compare :: NValue t f m -> NValue t f m -> m Bool
compare NValue t f m
a2 NValue t f m
a1 = NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m Bool) -> m (NValue t f m) -> m Bool
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
a1) (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
comp NValue t f m
a2

lessThanNix
  :: MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
lessThanNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
lessThanNix NValue t f m
ta NValue t f m
tb =
  do
    NValue t f m
va <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
ta
    NValue t f m
vb <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
tb

    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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.lessThan: expected two numbers or two strings, got '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
va String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"' and '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
vb String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"'."

    Bool -> NValue t f m
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Bool -> NValue t f m
mkNVBool (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
            (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, 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
            (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
ignoreContext NixString
a Text -> Text -> Bool
forall a. Ord a => a -> a -> Bool
< NixString -> Text
ignoreContext NixString
b
        (NValue t f m, NValue t f m)
_ -> m Bool
badType

-- | Helper function, generalization of @concat@ operations.
concatWith
  :: forall e t f m
   . MonadNix e t f m
  => (NValue t f m -> m (NValue t f m))
  -> NValue t f m
  -> m (NValue t f m)
concatWith :: (NValue t f m -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
concatWith NValue t f m -> m (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]] -> [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 ([[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 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 :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m (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
<=< 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]

-- | Nix function of Haskell:
-- > concat :: [[a]] -> [a]
--
-- Concatenate a list of lists into a single list.
concatListsNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
concatListsNix :: NValue t f m -> m (NValue t f m)
concatListsNix = (NValue t f m -> 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 -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
concatWith NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand

-- | Nix function of Haskell:
-- > concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
concatMapNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
concatMapNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
concatMapNix NValue t f m
f = (NValue t f m -> 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 -> m (NValue t f m))
-> NValue t f m -> m (NValue t f m)
concatWith (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)

listToAttrsNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
listToAttrsNix :: NValue t f m -> m (NValue t f m)
listToAttrsNix NValue t f m
lst =
  do
    [NValue t f m]
l <- 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
lst
    ([(VarName, NValue t f m)] -> NValue t f m)
-> m [(VarName, NValue t f m)] -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
      (PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet PositionSet
forall a. Monoid a => a
mempty (AttrSet (NValue t f m) -> NValue t f m)
-> ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> [(VarName, NValue t f m)]
-> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> ([(VarName, NValue t f m)] -> [(VarName, NValue t f m)])
-> [(VarName, NValue t f m)]
-> AttrSet (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(VarName, NValue t f m)] -> [(VarName, NValue t f m)]
forall a. [a] -> [a]
reverse)
      ((NValue t f m -> m (VarName, NValue t f m))
-> [NValue t f m] -> m [(VarName, 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
nvattrset ->
          do
            AttrSet (NValue t f m)
a <- 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)))
-> m (NValue t f m) -> m (AttrSet (NValue t f m))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvattrset
            (Text -> VarName
coerce -> VarName
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 -> m NixString) -> m (NValue t f m) -> m NixString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand (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
=<< VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet VarName
"name" AttrSet (NValue t f m)
a
            NValue t f m
val  <- VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet VarName
"value" AttrSet (NValue t f m)
a

            pure (VarName
name, NValue t f m
val)
        )
        [NValue t f m]
l
      )

-- prim_hashString from nix/src/libexpr/primops.cc
-- fail if context in the algo arg
-- propagate context from the s arg
-- | The result coming out of hashString is base16 encoded
hashStringNix
  :: forall e t f m. MonadNix e t f m => NixString -> NixString -> Prim m NixString
hashStringNix :: NixString -> NixString -> Prim m NixString
hashStringNix 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
modifyNixContents Text -> Text
g NixString
ns

      case Text
algo of
        --  2021-03-04: Pattern can not be taken-out because hashes represented as different types
        Text
"md5"    -> (Text -> Text) -> m NixString
f (MD5 -> Text
forall b a. (Show a, IsString b) => a -> b
show (MD5 -> Text) -> (Text -> MD5) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Show MD5, HashAlgorithm MD5) => Text -> MD5
forall a. (Show a, HashAlgorithm a) => Text -> a
mkHash @MD5.MD5)
        Text
"sha1"   -> (Text -> Text) -> m NixString
f (SHA1 -> Text
forall b a. (Show a, IsString b) => a -> b
show (SHA1 -> Text) -> (Text -> SHA1) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Show SHA1, HashAlgorithm SHA1) => Text -> SHA1
forall a. (Show a, HashAlgorithm a) => Text -> a
mkHash @SHA1.SHA1)
        Text
"sha256" -> (Text -> Text) -> m NixString
f (SHA256 -> Text
forall b a. (Show a, IsString b) => a -> b
show (SHA256 -> Text) -> (Text -> SHA256) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Show SHA256, HashAlgorithm SHA256) => Text -> SHA256
forall a. (Show a, HashAlgorithm a) => Text -> a
mkHash @SHA256.SHA256)
        Text
"sha512" -> (Text -> Text) -> m NixString
f (SHA512 -> Text
forall b a. (Show a, IsString b) => a -> b
show (SHA512 -> Text) -> (Text -> SHA512) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Show SHA512, HashAlgorithm SHA512) => Text -> SHA512
forall a. (Show a, HashAlgorithm a) => Text -> a
mkHash @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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.hashString: expected \"md5\", \"sha1\", \"sha256\", or \"sha512\", got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall b a. (Show a, IsString b) => a -> b
show Text
algo

       where
        -- This intermidiary `a` is only needed because of the type application
        mkHash :: (Show a, HashAlgorithm a) => Text -> a
        mkHash :: Text -> a
mkHash Text
s = ByteString -> a
forall a. HashAlgorithm a => ByteString -> a
hash (ByteString -> a) -> ByteString -> a
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
s


-- | hashFileNix
-- use hashStringNix to hash file content
hashFileNix
  :: forall e t f m . MonadNix e t f m => NixString -> Path -> Prim m NixString
hashFileNix :: NixString -> Path -> Prim m NixString
hashFileNix NixString
nsAlgo Path
nvfilepath = 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
$ NixString -> m NixString
hash (NixString -> m NixString) -> m NixString -> m NixString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m NixString
fileContent
 where
  hash :: NixString -> m NixString
hash = Prim m NixString -> m NixString
forall (m :: * -> *) a. Prim m a -> m a
outPrim (Prim m NixString -> m NixString)
-> (NixString -> Prim m NixString) -> NixString -> m NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> NixString -> Prim m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NixString -> NixString -> Prim m NixString
hashStringNix NixString
nsAlgo
  outPrim :: Prim m a -> m a
outPrim (Prim m a
x) = m a
x
  fileContent :: m NixString
  fileContent :: m NixString
fileContent = Text -> NixString
mkNixStringWithoutContext (Text -> NixString) -> m Text -> m NixString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Path -> m Text
forall (m :: * -> *). MonadFile m => Path -> m Text
Nix.Render.readFile Path
nvfilepath


-- | groupByNix
-- Groups elements of list together by the string returned from the function f called on 
-- each element. It returns an attribute set where each attribute value contains the 
-- elements of list that are mapped to the same corresponding attribute name returned by f.
groupByNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
groupByNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
groupByNix NValue t f m
nvfun NValue t f m
nvlist = do
  NValue t f m
list   <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvlist
  NValue t f m
fun    <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvfun
  (NValue t f m -> m (NValue t f m)
f, [NValue t f m]
l) <- (NValue t f m, NValue t f m)
-> m (NValue t f m -> m (NValue t f m), [NValue t f m])
forall (w :: * -> *) (w :: * -> *) (f :: * -> *) e a a t
       (m :: * -> *) t (m :: * -> *).
(Comonad w, Comonad w, MonadReader e f, Has e Frames, MonadThrow f,
 Show a, Show a) =>
(Free (NValue' t w m) a, Free (NValue' t w m) a)
-> f (NValue t w m -> m (Free (NValue' t w m) a),
      [Free (NValue' t w m) a])
extractP (NValue t f m
fun, NValue t f m
list)
  PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet PositionSet
forall a. Monoid a => a
mempty
    (AttrSet (NValue t f m) -> NValue t f m)
-> ([(VarName, [NValue t f m])] -> AttrSet (NValue t f m))
-> [(VarName, [NValue t f m])]
-> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   ([NValue t f m] -> NValue t f m)
-> HashMap VarName [NValue t f m] -> AttrSet (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
mkNVList ([NValue t f m] -> NValue t f m)
-> ([NValue t f m] -> [NValue t f m])
-> [NValue t f m]
-> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NValue t f m] -> [NValue t f m]
forall a. [a] -> [a]
reverse)
    (HashMap VarName [NValue t f m] -> AttrSet (NValue t f m))
-> ([(VarName, [NValue t f m])] -> HashMap VarName [NValue t f m])
-> [(VarName, [NValue t f m])]
-> AttrSet (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   ([NValue t f m] -> [NValue t f m] -> [NValue t f m])
-> [(VarName, [NValue t f m])] -> HashMap VarName [NValue t f m]
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> [(k, v)] -> HashMap k v
M.fromListWith [NValue t f m] -> [NValue t f m] -> [NValue t f m]
forall a. Semigroup a => a -> a -> a
(<>)
    ([(VarName, [NValue t f m])] -> NValue t f m)
-> m [(VarName, [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 -> m (VarName, [NValue t f m]))
-> [NValue t f m] -> m [(VarName, [NValue t f m])]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((OneItem [NValue t f m] -> m (NValue t f m))
-> OneItem [NValue t f m] -> m (VarName, [NValue t f m])
forall (f :: * -> *) v b.
(Monad f, FromValue Text f v, One b) =>
(OneItem b -> f v) -> OneItem b -> f (VarName, b)
app NValue t f m -> m (NValue t f m)
OneItem [NValue t f m] -> m (NValue t f m)
f) [NValue t f m]
l
 where
  app :: (OneItem b -> f v) -> OneItem b -> f (VarName, b)
app OneItem b -> f v
f OneItem b
x = do
    Text
name <- forall a (m :: * -> *) v. FromValue a m v => v -> m a
forall (m :: * -> *) v. FromValue Text m v => v -> m Text
fromValue @Text (v -> f Text) -> f v -> f Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< OneItem b -> f v
f OneItem b
x
    pure (Text -> VarName
VarName Text
name, OneItem b -> b
forall x. One x => OneItem x -> x
one OneItem b
x)
  extractP :: (Free (NValue' t w m) a, Free (NValue' t w m) a)
-> f (NValue t w m -> m (Free (NValue' t w m) a),
      [Free (NValue' t w m) a])
extractP (NVBuiltin VarName
_ NValue t w m -> m (Free (NValue' t w m) a)
f, NVList [Free (NValue' t w m) a]
l) = (NValue t w m -> m (Free (NValue' t w m) a),
 [Free (NValue' t w m) a])
-> f (NValue t w m -> m (Free (NValue' t w m) a),
      [Free (NValue' t w m) a])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t w m -> m (Free (NValue' t w m) a)
f, [Free (NValue' t w m) a]
l)
  extractP (NVClosure Params ()
_ NValue t w m -> m (Free (NValue' t w m) a)
f, NVList [Free (NValue' t w m) a]
l) = (NValue t w m -> m (Free (NValue' t w m) a),
 [Free (NValue' t w m) a])
-> f (NValue t w m -> m (Free (NValue' t w m) a),
      [Free (NValue' t w m) a])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t w m -> m (Free (NValue' t w m) a)
f, [Free (NValue' t w m) a]
l)
  extractP (Free (NValue' t w m) a, Free (NValue' t w m) a)
_v =
    ErrorCall
-> f (NValue t w m -> m (Free (NValue' t w m) a),
      [Free (NValue' t w m) a])
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
      (ErrorCall
 -> f (NValue t w m -> m (Free (NValue' t w m) a),
       [Free (NValue' t w m) a]))
-> ErrorCall
-> f (NValue t w m -> m (Free (NValue' t w m) a),
      [Free (NValue' t w m) a])
forall a b. (a -> b) -> a -> b
$  String -> ErrorCall
ErrorCall
      (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$  String
"builtins.groupBy: expected function and list, got "
      String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (Free (NValue' t w m) a, Free (NValue' t w m) a) -> String
forall b a. (Show a, IsString b) => a -> b
show (Free (NValue' t w m) a, Free (NValue' t w m) a)
_v


placeHolderNix :: forall t f m e . MonadNix e t f m => NValue t f m -> m (NValue t f m)
placeHolderNix :: NValue t f m -> m (NValue t f m)
placeHolderNix NValue t f m
p =
  do
    Text
t <- 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
p
    NixString
h <-
      Coercible (Prim m NixString) (m NixString) =>
Prim m NixString -> m NixString
coerce @(Prim m NixString) @(m NixString) (Prim m NixString -> m NixString)
-> Prim m NixString -> m NixString
forall a b. (a -> b) -> a -> b
$
        (NixString -> NixString -> Prim m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NixString -> NixString -> Prim m NixString
hashStringNix (NixString -> NixString -> Prim m NixString)
-> (Text -> NixString) -> Text -> Text -> Prim m NixString
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Text -> NixString
mkNixStringWithoutContext)
          Text
"sha256"
          (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
mkNixStringWithoutContext
      (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
      -- Please, stop Text -> Bytestring here after migration to Text
      (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ case ByteString -> Either String ByteString
Base16.decode (NixString -> ByteString
bytes NixString
h) of -- The result coming out of hashString is base16 encoded
#if MIN_VERSION_base16_bytestring(1,0,0)
        -- Please, stop Text -> String here after migration to Text
        Left String
e -> Text -> ByteString
forall a t. (HasCallStack, IsText t) => t -> a
error (Text -> ByteString) -> Text -> ByteString
forall a b. (a -> b) -> a -> b
$ Text
"Couldn't Base16 decode the text: '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> NixString -> Text
body NixString
h Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'.\nThe Left fail content: '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall b a. (Show a, IsString b) => a -> b
show String
e Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'."
        Right ByteString
d -> ByteString
d
#else
        (d, "") -> d
        (_, e) -> error $ "Couldn't Base16 decode the text: '" <> body h <> "'.\nUndecodable remainder: '" <> show e <> "'."
#endif
    where
      bytes :: NixString -> ByteString
      bytes :: NixString -> ByteString
bytes = Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 (Text -> ByteString)
-> (NixString -> Text) -> NixString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
body

      body :: NixString -> Text
body = NixString -> Text
ignoreContext

readFileNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
readFileNix :: NValue t f m -> m (NValue t f m)
readFileNix = Text -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Text -> m (NValue t f m))
-> (NValue t f m -> m Text) -> 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
<=< Path -> m Text
forall (m :: * -> *). MonadFile m => Path -> m Text
Nix.Render.readFile (Path -> m Text)
-> (NValue t f m -> m Path) -> NValue t f m -> m Text
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m Path
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m Path
absolutePathFromValue (NValue t f m -> m Path)
-> (NValue t f m -> m (NValue t f m)) -> NValue t f m -> m Path
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 v (m :: * -> *). MonadValue v m => v -> m v
demand

findFileNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
findFileNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
findFileNix NValue t f m
nvaset NValue t f m
nvfilepath =
  do
    NValue t f m
aset <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvaset
    NValue t f m
filePath <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvfilepath

    case (NValue t f m
aset, NValue t f m
filePath) of
      (NVList [NValue t f m]
x, NVStr NixString
ns) ->
        do
          Path
mres <- [NValue t f m] -> Path -> m Path
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
[NValue t f m] -> Path -> m Path
findPath @t @f @m [NValue t f m]
x (Path -> m Path) -> Path -> m Path
forall a b. (a -> b) -> a -> b
$ String -> Path
coerce (String -> Path) -> String -> Path
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. ToString a => a -> String
toString (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ NixString -> Text
ignoreContext NixString
ns

          pure $ Path -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Path -> NValue t f m
mkNVPath Path
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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"expected a string, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"expected a list, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Invalid types for builtins.findFile: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (NValue t f m, NValue t f m) -> String
forall b a. (Show a, IsString b) => a -> b
show (NValue t f m
_x, NValue t f m
_y)

readDirNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
readDirNix :: NValue t f m -> m (NValue t f m)
readDirNix NValue t f m
nvpath =
  do
    Path
path           <- NValue t f m -> m Path
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NValue t f m -> m Path
absolutePathFromValue (NValue t f m -> m Path) -> m (NValue t f m) -> m Path
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvpath
    [Path]
items          <- Path -> m [Path]
forall (m :: * -> *). MonadFile m => Path -> m [Path]
listDirectory Path
path

    let
      -- | Function indeed binds filepaths as keys ('VarNames') in Nix attrset.
      detectFileTypes :: Path -> m (VarName, FileType)
      detectFileTypes :: Path -> m (VarName, FileType)
detectFileTypes Path
item =
        do
          FileStatus
s <- Path -> m FileStatus
forall (m :: * -> *). MonadFile m => Path -> m FileStatus
getSymbolicLinkStatus (Path -> m FileStatus) -> Path -> m FileStatus
forall a b. (a -> b) -> a -> b
$ Path
path Path -> Path -> Path
</> Path
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

          (VarName, FileType) -> m (VarName, FileType)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> Text) -> Path -> VarName
coerce @(String -> Text) String -> Text
forall a. IsString a => String -> a
fromString Path
item, FileType
t)

    [(VarName, FileType)]
itemsWithTypes <-
      (Path -> m (VarName, FileType))
-> [Path] -> m [(VarName, FileType)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
        Path -> m (VarName, FileType)
detectFileTypes
        [Path]
items

    (Deeper (NValue t f m) -> NValue t f m
coerce :: CoerceDeeperToNValue t f m) (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 VarName FileType -> m (Deeper (NValue t f m))
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue ([(VarName, FileType)] -> HashMap VarName FileType
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [(VarName, FileType)]
itemsWithTypes)

fromJSONNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
fromJSONNix :: NValue t f m -> m (NValue t f m)
fromJSONNix NValue t f m
nvjson =
  do
    NValue t f m
j <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvjson
    Text
jText <- 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
j

    (String -> m (NValue t f m))
-> (Value -> m (NValue t f m))
-> Either String Value
-> m (NValue t f m)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
      (\ String
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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.fromJSON: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
jsonError)
      Value -> m (NValue t f m)
jsonToNValue
      -- do we really need to marshall Text -> ByteString -> Aeson.Value (that is a Text)
      (FromJSON Value => ByteString -> Either String Value
forall a. FromJSON a => ByteString -> Either String a
A.eitherDecodeStrict' @A.Value (ByteString -> Either String Value)
-> ByteString -> Either String Value
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
jText)

 where
  jsonToNValue :: (A.Value -> m (NValue t f m))
  jsonToNValue :: Value -> m (NValue t f m)
jsonToNValue =
    \case
      A.Object Object
m ->
        (HashMap VarName (NValue t f m) -> NValue t f m)
-> HashMap VarName Value -> m (NValue t f m)
forall (t0 :: * -> *) b.
Traversable t0 =>
(t0 (NValue t f m) -> b) -> t0 Value -> m b
traverseToNValue
          (PositionSet -> HashMap VarName (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet PositionSet
forall a. Monoid a => a
mempty)
#if MIN_VERSION_aeson(2,0,0)
          ((Key -> VarName) -> HashMap Key Value -> HashMap VarName Value
forall k2 k1 v.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> HashMap k1 v -> HashMap k2 v
M.mapKeys (Text -> VarName
coerce (Text -> VarName) -> (Key -> Text) -> Key -> VarName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Text
AKM.toText)  (HashMap Key Value -> HashMap VarName Value)
-> HashMap Key Value -> HashMap VarName Value
forall a b. (a -> b) -> a -> b
$ Object -> HashMap Key Value
forall v. KeyMap v -> HashMap Key v
AKM.toHashMap Object
m)
#else
          (M.mapKeys coerce m)
#endif
      A.Array  Array
l -> ([NValue t f m] -> NValue t f m) -> [Value] -> m (NValue t f m)
forall (t0 :: * -> *) b.
Traversable t0 =>
(t0 (NValue t f m) -> b) -> t0 Value -> m b
traverseToNValue [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
mkNVList (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
$ Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext 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
mkNVConstant (NAtom -> NValue t f m) -> NAtom -> NValue t f m
forall a b. (a -> b) -> a -> b
$
            (Float -> NAtom)
-> (Integer -> NAtom) -> Either Float Integer -> NAtom
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
              Float -> NAtom
NFloat
              Integer -> NAtom
NInt
              (Scientific -> Either Float Integer
forall r i. (RealFloat r, Integral i) => Scientific -> Either r i
floatingOrInteger Scientific
n)
      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
$ Bool -> NValue t f m
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Bool -> NValue t f m
mkNVBool 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
forall (f :: * -> *) t (m :: * -> *). Applicative f => NValue t f m
nvNull
   where
    traverseToNValue :: Traversable t0 => (t0 (NValue t f m) -> b) -> t0 A.Value -> m b
    traverseToNValue :: (t0 (NValue t f m) -> b) -> t0 Value -> m b
traverseToNValue t0 (NValue t f m) -> b
f t0 Value
v = t0 (NValue t f m) -> b
f (t0 (NValue t f m) -> b) -> m (t0 (NValue t f m)) -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value -> m (NValue t f m)) -> t0 Value -> m (t0 (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 t0 Value
v

toJSONNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
toJSONNix :: NValue t f m -> m (NValue t f m)
toJSONNix = ((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
mkNVStr (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
toJSONNixString) (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 v (m :: * -> *). MonadValue v m => v -> m v
demand

toXMLNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
toXMLNix :: NValue t f m -> m (NValue t f m)
toXMLNix = ((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
mkNVStr (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) (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 v (m :: * -> *). MonadValue v m => v -> m v
demand

typeOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
typeOfNix :: NValue t f m -> m (NValue t f m)
typeOfNix NValue t f m
nvv =
  do
    NValue t f m
v <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
nvv
    let
      detectType :: Text
detectType =
        case NValue t f m
v of
          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     PositionSet
_ AttrSet (NValue t f m)
_ -> Text
"set"
          NVClosure{}   -> Text
"lambda"
          NVPath    Path
_   -> Text
"path"
          NVBuiltin VarName
_ NValue t f m -> m (NValue t f m)
_ -> Text
"lambda"
          NValue t f m
_             -> Text -> Text
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"Pattern synonyms obscure complete patterns"

    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
mkNixStringWithoutContext Text
detectType

tryEvalNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
tryEvalNix :: NValue t f m -> m (NValue t f m)
tryEvalNix 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 -> 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))
  (NValue t f m -> NValue t f m
forall t (f :: * -> *) (m :: * -> *) e.
(Scoped (NValue t f m) m, MonadReader e m, Has e Frames,
 Has e SrcSpan, Has e Options, MonadFix m, MonadCatch m,
 Alternative m, MonadEffects t f m, MonadThunk t m (NValue t f m),
 Comonad f, Traversable f, HasCitations m (NValue t f m) t,
 HasCitations1 m (NValue t f m) f, MonadValue (NValue t f m) m,
 Applicative f, Show t, Typeable m, Typeable f, Typeable t) =>
NValue t f m -> NValue t f m
onSuccess (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
<$> NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
e)
 where
  onSuccess :: NValue t f m -> NValue t f m
onSuccess NValue t f m
v =
    PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet
      PositionSet
forall a. Monoid a => a
mempty
      (AttrSet (NValue t f m) -> NValue t f m)
-> AttrSet (NValue t f m) -> NValue t f m
forall a b. (a -> b) -> a -> b
$ [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
        [ (VarName
"success", Bool -> NValue t f m
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Bool -> NValue t f m
mkNVBool Bool
True)
        , (VarName
"value"  , NValue t f m
v            )
        ]

  onError :: SomeException -> NValue t f m
  onError :: SomeException -> NValue t f m
onError SomeException
_ =
    PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet
      PositionSet
forall a. Monoid a => a
mempty
      (AttrSet (NValue t f m) -> NValue t f m)
-> AttrSet (NValue t f m) -> NValue t f m
forall a b. (a -> b) -> a -> b
$ [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
        ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall a b. (a -> b) -> a -> b
$ (, Bool -> NValue t f m
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Bool -> NValue t f m
mkNVBool Bool
False) (VarName -> (VarName, NValue t f m))
-> [VarName] -> [(VarName, NValue t f m)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
          [ VarName
"success"
          , VarName
"value"
          ]

traceNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
traceNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
traceNix NValue t f m
msg NValue t f m
action =
  do
    MonadEffects t f m => String -> m ()
forall t (f :: * -> *) (m :: * -> *).
MonadEffects t f m =>
String -> m ()
traceEffect @t @f @m (String -> m ()) -> (NixString -> String) -> NixString -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString (Text -> String) -> (NixString -> Text) -> NixString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> Text
ignoreContext (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
    pure NValue t f m
action

-- Please, can function remember fail context
addErrorContextNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m  -- action
  -> m (NValue t f m)
addErrorContextNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
addErrorContextNix NValue t f m
_ = NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure

execNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
execNix :: NValue t f m -> m (NValue t f m)
execNix NValue t f m
xs =
  do
    [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 (CopyToStoreMode -> NValue t f m -> m NixString
forall e t (f :: * -> *) (m :: * -> *).
(Framed e m, MonadStore m, MonadThrow m,
 MonadDataErrorContext t f m, MonadValue (NValue t f m) m) =>
CopyToStoreMode -> NValue t f m -> m NixString
coerceStringlikeToNixString CopyToStoreMode
DontCopyToStore) ([NValue t f m] -> m [NixString])
-> m [NValue t f m] -> m [NixString]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< 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
    -- 2018-11-19: NOTE: Still need to do something with the context here
    -- See prim_exec in nix/src/libexpr/primops.cc
    -- Requires the implementation of EvalState::realiseContext
    [Text] -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, MonadInstantiate m) =>
[Text] -> m (NValue t f m)
exec ([Text] -> m (NValue t f m)) -> [Text] -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ NixString -> Text
ignoreContext (NixString -> Text) -> [NixString] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [NixString]
xs'

fetchurlNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
fetchurlNix :: NValue t f m -> m (NValue t f m)
fetchurlNix =
  (\case
    NVSet PositionSet
_ AttrSet (NValue t f m)
s -> Maybe (NValue t f m) -> NValue t f m -> m (NValue t f m)
go (VarName -> 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 VarName
"sha256" AttrSet (NValue t f m)
s) (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 -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand (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
=<< VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
attrsetGet VarName
"url" 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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.fetchurl: Expected URI or set, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
v
  ) (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 v (m :: * -> *). MonadValue v m => v -> m v
demand

 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 ->
        (ErrorCall -> m (NValue t f m))
-> (StorePath -> m (NValue t f m))
-> Either ErrorCall StorePath
-> m (NValue t f m)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either -- msha
          ErrorCall -> m (NValue t f m)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError
          StorePath -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue
          (Either ErrorCall StorePath -> m (NValue t f m))
-> m (Either ErrorCall StorePath) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> m (Either ErrorCall StorePath)
forall (m :: * -> *).
MonadHttp m =>
Text -> m (Either ErrorCall StorePath)
getURL (Text -> m (Either ErrorCall StorePath))
-> m Text -> m (Either ErrorCall StorePath)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NixString -> m Text
forall e (m :: * -> *).
(MonadReader e m, Has e Frames, MonadThrow m) =>
NixString -> m Text
noContextAttrs 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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"builtins.fetchurl: Expected URI or string, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
v

  noContextAttrs :: NixString -> m Text
noContextAttrs NixString
ns =
    m Text -> (Text -> m Text) -> Maybe Text -> m Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
      (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
$ String -> ErrorCall
ErrorCall String
"builtins.fetchurl: unsupported arguments to url")
      Text -> m Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      (NixString -> Maybe Text
getStringNoContext NixString
ns)

partitionNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
partitionNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
partitionNix NValue t f m
f NValue t f m
nvlst =
  do
    [NValue t f m]
l <- 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
nvlst
    let
      match :: NValue t f m -> m (Bool, NValue t f m)
match NValue t f m
t = (, NValue t f m
t) (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
<$> (NValue t f m -> m Bool
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m Bool) -> m (NValue t f m) -> m Bool
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 NValue t f m
t)
    [(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
      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
mkNVList ([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 (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (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
$ [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList
          [ (VarName
"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)
          , (VarName
"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)
          ]

currentSystemNix :: MonadNix e t f m => m (NValue t f m)
currentSystemNix :: m (NValue t f m)
currentSystemNix =
  do
    Text
os   <- m Text
forall (m :: * -> *). MonadEnv m => m Text
getCurrentSystemOS
    Text
arch <- m Text
forall (m :: * -> *). MonadEnv m => m Text
getCurrentSystemArch

    pure $ Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext (Text -> NValue t f m) -> Text -> NValue t f m
forall a b. (a -> b) -> a -> b
$ 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

currentTimeNix :: MonadNix e t f m => m (NValue t f m)
currentTimeNix :: m (NValue t f m)
currentTimeNix =
  do
    Options
opts <- m Options
forall e (m :: * -> *).
(MonadReader e m, Has e Options) =>
m Options
askOptions
    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 (UTCTime -> POSIXTime) -> UTCTime -> POSIXTime
forall a b. (a -> b) -> a -> b
$ Options -> UTCTime
getTime Options
opts

derivationStrictNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
derivationStrictNix :: NValue t f m -> m (NValue t f m)
derivationStrictNix = 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

getRecursiveSizeNix :: (MonadIntrospect m, Applicative f) => a -> m (NValue t f m)
getRecursiveSizeNix :: a -> m (NValue t f m)
getRecursiveSizeNix = (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
mkNVConstant (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

getContextNix
  :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
getContextNix :: NValue t f m -> m (NValue t f m)
getContextNix =
  \case
    (NVStr NixString
ns) ->
      PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet PositionSet
forall a. Monoid a => a
mempty (AttrSet (NValue t f m) -> NValue t f m)
-> m (AttrSet (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap VarName NixLikeContextValue -> m (AttrSet (NValue t f m))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue (NixLikeContext -> HashMap VarName NixLikeContextValue
getNixLikeContext (NixLikeContext -> HashMap VarName NixLikeContextValue)
-> NixLikeContext -> HashMap VarName 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
getStringContext NixString
ns)
    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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Invalid type for builtins.getContext: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
x
  (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 v (m :: * -> *). MonadValue v m => v -> m v
demand

appendContextNix
  :: forall e t f m
   . MonadNix e t f m
  => NValue t f m
  -> NValue t f m
  -> m (NValue t f m)
appendContextNix :: NValue t f m -> NValue t f m -> m (NValue t f m)
appendContextNix NValue t f m
tx NValue t f m
ty =
  do
    NValue t f m
x <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
tx
    NValue t f m
y <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
ty

    case (NValue t f m
x, NValue t f m
y) of
      (NVStr NixString
ns, NVSet PositionSet
_ AttrSet (NValue t f m)
attrs) ->
        do
          let
            getPathNOuts :: NValue t f m -> m NixLikeContextValue
            getPathNOuts :: NValue t f m -> m NixLikeContextValue
getPathNOuts NValue t f m
tx =
              do
                NValue t f m
x <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
tx

                case NValue t f m
x of
                  NVSet PositionSet
_ AttrSet (NValue t f m)
atts ->
                    do
                      -- TODO: Fail for unexpected keys.

                      let
                        getK :: VarName -> m Bool
                        getK :: VarName -> m Bool
getK VarName
k =
                          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 -> 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)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand)
                            (Maybe (NValue t f m) -> m Bool) -> Maybe (NValue t f m) -> m Bool
forall a b. (a -> b) -> a -> b
$ VarName -> 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 VarName
k AttrSet (NValue t f m)
atts

                        getOutputs :: m [Text]
                        getOutputs :: m [Text]
getOutputs =
                          m [Text]
-> (NValue t f m -> m [Text]) -> Maybe (NValue t f m) -> m [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
                            m [Text]
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
                            (\ NValue t f m
touts ->
                              do
                                NValue t f m
outs <- NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
touts

                                case NValue t f m
outs of
                                  NVList [NValue t f m]
vs -> (NValue t f m -> m Text) -> [NValue t f m] -> m [Text]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((NixString -> Text) -> m NixString -> m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NixString -> Text
ignoreContext (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]
vs
                                  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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Invalid types for context value outputs in builtins.appendContext: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
_x
                            )
                            (VarName -> 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 VarName
"outputs" AttrSet (NValue t f m)
atts)

                      Bool
path <- VarName -> m Bool
getK VarName
"path"
                      Bool
allOutputs <- VarName -> m Bool
getK VarName
"allOutputs"

                      Bool -> Bool -> [Text] -> NixLikeContextValue
NixLikeContextValue Bool
path Bool
allOutputs ([Text] -> NixLikeContextValue)
-> m [Text] -> m NixLikeContextValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m [Text]
getOutputs

                  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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Invalid types for context value in builtins.appendContext: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NValue t f m -> String
forall b a. (Show a, IsString b) => a -> b
show NValue t f m
_x
            addContext :: HashMap VarName NixLikeContextValue -> NixString
            addContext :: HashMap VarName NixLikeContextValue -> NixString
addContext HashMap VarName NixLikeContextValue
newContextValues =
              HashSet StringContext -> Text -> NixString
mkNixString
                (NixLikeContext -> HashSet StringContext
fromNixLikeContext (NixLikeContext -> HashSet StringContext)
-> NixLikeContext -> HashSet StringContext
forall a b. (a -> b) -> a -> b
$
                  HashMap VarName NixLikeContextValue -> NixLikeContext
NixLikeContext (HashMap VarName NixLikeContextValue -> NixLikeContext)
-> HashMap VarName NixLikeContextValue -> NixLikeContext
forall a b. (a -> b) -> a -> b
$
                    (NixLikeContextValue -> NixLikeContextValue -> NixLikeContextValue)
-> HashMap VarName NixLikeContextValue
-> HashMap VarName NixLikeContextValue
-> HashMap VarName 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 VarName NixLikeContextValue
newContextValues
                      (HashMap VarName NixLikeContextValue
 -> HashMap VarName NixLikeContextValue)
-> HashMap VarName NixLikeContextValue
-> HashMap VarName NixLikeContextValue
forall a b. (a -> b) -> a -> b
$ NixLikeContext -> HashMap VarName NixLikeContextValue
getNixLikeContext (NixLikeContext -> HashMap VarName NixLikeContextValue)
-> NixLikeContext -> HashMap VarName 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
getStringContext NixString
ns
                )
                (Text -> NixString) -> Text -> NixString
forall a b. (a -> b) -> a -> b
$ NixString -> Text
ignoreContext NixString
ns

          NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m))
-> (HashMap VarName NixLikeContextValue -> NixString)
-> HashMap VarName NixLikeContextValue
-> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap VarName NixLikeContextValue -> NixString
addContext (HashMap VarName NixLikeContextValue -> m (NValue t f m))
-> m (HashMap VarName NixLikeContextValue) -> m (NValue t f m)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (NValue t f m -> m NixLikeContextValue)
-> AttrSet (NValue t f m)
-> m (HashMap VarName NixLikeContextValue)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NValue t f m -> m NixLikeContextValue
getPathNOuts AttrSet (NValue t f m)
attrs

      (NValue t f m, NValue t f m)
_xy -> 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
$ String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"Invalid types for builtins.appendContext: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (NValue t f m, NValue t f m) -> String
forall b a. (Show a, IsString b) => a -> b
show (NValue t f m, NValue t f m)
_xy


nixVersionNix :: MonadNix e t f m => m (NValue t f m)
nixVersionNix :: m (NValue t f m)
nixVersionNix = 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
mkNixStringWithoutContext Text
"2.3"

langVersionNix :: MonadNix e t f m => m (NValue t f m)
langVersionNix :: m (NValue t f m)
langVersionNix = Int -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Int
5 :: Int)

-- ** @builtinsList@

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 :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA
    [ BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
throwNix -- for now
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
baseNameOfNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
TopLevel VarName
"derivation"       m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
(MonadNix e t f m, Scoped (NValue t f m) m) =>
m (NValue t f m)
derivationNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
derivationStrictNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
dirOfNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
importNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
isNullNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
TopLevel VarName
"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)
mapNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
TopLevel VarName
"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)
mapAttrsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"placeholder"      NValue t f m -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *) e.
MonadNix e t f m =>
NValue t f m -> m (NValue t f m)
placeHolderNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
TopLevel VarName
"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)
removeAttrsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
TopLevel VarName
"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)
scopedImportNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
throwNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
TopLevel VarName
"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)
toStringNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
TopLevel VarName
"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)
traceNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"nixVersion"       m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
nixVersionNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"langVersion"      m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
langVersionNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
addNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
addErrorContextNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
allNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
anyNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
appendContextNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
attrNamesNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
attrValuesNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
bitAndNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
bitOrNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
bitXorNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"builtins"         m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
builtinsBuiltinNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
catAttrsNix
    , BuiltinType
-> VarName
-> (Float -> Prim m Integer)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"ceil"             ((Float -> Integer) -> Float -> Prim m Integer
forall a b. (a -> b) -> a -> Prim m b
arity1 (Integral Integer => Float -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
ceiling @Float @Integer))
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
compareVersionsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
concatListsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
concatMapNix
    , BuiltinType
-> VarName
-> (NixString -> [NixString] -> Prim m NixString)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"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
intercalateNixString)
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"currentSystem"    m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
currentSystemNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"currentTime"      m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
currentTimeNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
deepSeqNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
divNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
elemNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
elemAtNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
execNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"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
$ Bool -> NValue t f m
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Bool -> NValue t f m
mkNVBool Bool
False)
    --, add  Normal   "fetchGit"         fetchGit
    --, add  Normal   "fetchMercurial"   fetchMercurial
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
fetchurlNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
filterNix
    --, add  Normal   "filterSource"     filterSource
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
findFileNix
    , BuiltinType
-> VarName
-> (Float -> Prim m Integer)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"floor"            ((Float -> Integer) -> Float -> Prim m Integer
forall a b. (a -> b) -> a -> Prim m b
arity1 (Integral Integer => Float -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
floor @Float @Integer))
    , BuiltinType
-> VarName
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add3 BuiltinType
Normal   VarName
"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'Nix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
fromJSONNix
    --, add  Normal   "fromTOML"         fromTOML
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
functionArgsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
genericClosureNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
genListNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
getAttrNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
getContextNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
getEnvNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"groupBy"          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)
groupByNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
hasAttrNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
hasContextNix
    , BuiltinType
-> VarName
-> (NixString -> NixString -> Prim m NixString)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"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
hashStringNix @e @t @f @m)
    , BuiltinType
-> VarName
-> (NixString -> Path -> Prim m NixString)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"hashFile"         NixString -> Path -> Prim m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
NixString -> Path -> Prim m NixString
hashFileNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
headNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
intersectAttrsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
isAttrsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
isBoolNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
isFloatNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
isFunctionNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
isIntNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
isListNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
isStringNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"isPath"           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)
isPathNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
lengthNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
lessThanNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
listToAttrsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
matchNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
mulNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"nixPath"          m (NValue t f m)
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
m (NValue t f m)
nixPathNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"null"             (NValue t f m -> m (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue t f m
forall (f :: * -> *) t (m :: * -> *). Applicative f => NValue t f m
nvNull)
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
parseDrvNameNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
partitionNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"path"             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)
pathNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
pathExistsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
readDirNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
readFileNix
    , BuiltinType
-> VarName
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add3 BuiltinType
Normal   VarName
"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)
replaceStringsNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
seqNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
sortNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
splitNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
splitVersionNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"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
$ Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext Text
"/nix/store")
    --, add  Normal   "storePath"        storePath
    , BuiltinType
-> VarName
-> (NixString -> Prim m Int)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"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
ignoreContext)
    , BuiltinType
-> VarName
-> (Integer -> Integer -> Prim m Integer)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"sub"              ((Integer -> Integer -> Integer)
-> Integer -> Integer -> Prim m Integer
forall a b c. (a -> b -> c) -> a -> b -> Prim m c
arity2 ((-) @Integer))
    , BuiltinType
-> VarName
-> (Int -> Int -> NixString -> Prim m NixString)
-> m (Builtin (NValue t f m))
forall a.
ToBuiltin t f m a =>
BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' BuiltinType
Normal   VarName
"substring"        Int -> Int -> NixString -> Prim m NixString
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Int -> Int -> NixString -> Prim m NixString
substringNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
tailNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
toFileNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
toJSONNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
toPathNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
toXMLNix
    , BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 BuiltinType
Normal   VarName
"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
$ Bool -> NValue t f m
forall e t (f :: * -> *) (m :: * -> *).
MonadNix e t f m =>
Bool -> NValue t f m
mkNVBool Bool
True)
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
tryEvalNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
typeOfNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"unsafeDiscardOutputDependency" 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)
unsafeDiscardOutputDependencyNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
unsafeDiscardStringContextNix
    , BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 BuiltinType
Normal   VarName
"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)
unsafeGetAttrPosNix
    , BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add  BuiltinType
Normal   VarName
"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)
getRecursiveSizeNix
    ]
 where

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

  arity1 :: (a -> b) -> (a -> Prim m b)
  arity1 :: (a -> b) -> a -> Prim m b
arity1 a -> b
g = b -> Prim m b
forall a. a -> Prim m a
arity0 (b -> Prim m b) -> (a -> b) -> a -> Prim m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
g

  arity2 :: (a -> b -> c) -> (a -> b -> Prim m c)
  arity2 :: (a -> b -> c) -> a -> b -> Prim m c
arity2 a -> b -> c
f = (b -> c) -> b -> Prim m c
forall a b. (a -> b) -> a -> Prim m b
arity1 ((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

  mkBuiltin :: BuiltinType -> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
  mkBuiltin :: BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
mkBuiltin BuiltinType
t VarName
n m (NValue t f m)
v = BuiltinType -> VarName -> NValue t f m -> Builtin (NValue t f m)
forall v. BuiltinType -> VarName -> v -> Builtin v
wrap BuiltinType
t VarName
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
<$> VarName -> m (NValue t f m) -> m (NValue t f m)
mkThunk VarName
n m (NValue t f m)
v
   where
    wrap :: BuiltinType -> VarName -> v -> Builtin v
    wrap :: BuiltinType -> VarName -> v -> Builtin v
wrap BuiltinType
t VarName
n v
f = BuiltinType -> (VarName, v) -> Builtin v
forall v. BuiltinType -> (VarName, v) -> Builtin v
Builtin BuiltinType
t (VarName
n, v
f)

    mkThunk :: VarName -> m (NValue t f m) -> m (NValue t f m)
    mkThunk :: VarName -> m (NValue t f m) -> m (NValue t f m)
mkThunk VarName
n = 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))
-> 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
Info (String -> ErrorCall
ErrorCall (String -> ErrorCall) -> String -> ErrorCall
forall a b. (a -> b) -> a -> b
$ String
"While calling builtin " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> VarName -> String
forall a. ToString a => a -> String
toString VarName
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n")

  hAdd
    :: ( VarName
      -> fun
      -> m (NValue t f m)
      )
    -> BuiltinType
    -> VarName
    -> fun
    -> m (Builtin (NValue t f m))
  hAdd :: (VarName -> fun -> m (NValue t f m))
-> BuiltinType -> VarName -> fun -> m (Builtin (NValue t f m))
hAdd VarName -> fun -> m (NValue t f m)
f BuiltinType
t VarName
n fun
v = BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
mkBuiltin BuiltinType
t VarName
n (m (NValue t f m) -> m (Builtin (NValue t f m)))
-> m (NValue t f m) -> m (Builtin (NValue t f m))
forall a b. (a -> b) -> a -> b
$ VarName -> fun -> m (NValue t f m)
f VarName
n fun
v

  add0
    :: BuiltinType
    -> VarName
    -> m (NValue t f m)
    -> m (Builtin (NValue t f m))
  add0 :: BuiltinType
-> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
add0 = (VarName -> m (NValue t f m) -> m (NValue t f m))
-> BuiltinType
-> VarName
-> m (NValue t f m)
-> m (Builtin (NValue t f m))
forall fun.
(VarName -> fun -> m (NValue t f m))
-> BuiltinType -> VarName -> fun -> m (Builtin (NValue t f m))
hAdd (\ VarName
_ m (NValue t f m)
x -> m (NValue t f m)
x)

  add
    :: BuiltinType
    -> VarName
    -> ( NValue t f m
      -> m (NValue t f m)
      )
    -> m (Builtin (NValue t f m))
  add :: BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add = (VarName -> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m))
-> BuiltinType
-> VarName
-> (NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall fun.
(VarName -> fun -> m (NValue t f m))
-> BuiltinType -> VarName -> fun -> m (Builtin (NValue t f m))
hAdd VarName -> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
forall (m :: * -> *) (f :: * -> *) t.
(MonadThunk t m (NValue t f m), MonadDataContext f m) =>
VarName -> (NValue t f m -> m (NValue t f m)) -> m (NValue t f m)
builtin

  add2
    :: BuiltinType
    -> VarName
    -> ( NValue t f m
      -> NValue t f m
      -> m (NValue t f m)
      )
    -> m (Builtin (NValue t f m))
  add2 :: BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add2 = (VarName
 -> (NValue t f m -> NValue t f m -> m (NValue t f m))
 -> m (NValue t f m))
-> BuiltinType
-> VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall fun.
(VarName -> fun -> m (NValue t f m))
-> BuiltinType -> VarName -> fun -> m (Builtin (NValue t f m))
hAdd VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (NValue t f m)
forall t (m :: * -> *) (f :: * -> *).
(MonadThunk t m (NValue t f m), MonadDataContext f m) =>
VarName
-> (NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (NValue t f m)
builtin2

  add3
    :: BuiltinType
    -> VarName
    -> ( NValue t f m
      -> NValue t f m
      -> NValue t f m
      -> m (NValue t f m)
      )
    -> m (Builtin (NValue t f m))
  add3 :: BuiltinType
-> VarName
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
add3 = (VarName
 -> (NValue t f m
     -> NValue t f m -> NValue t f m -> m (NValue t f m))
 -> m (NValue t f m))
-> BuiltinType
-> VarName
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (Builtin (NValue t f m))
forall fun.
(VarName -> fun -> m (NValue t f m))
-> BuiltinType -> VarName -> fun -> m (Builtin (NValue t f m))
hAdd VarName
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (NValue t f m)
forall t (m :: * -> *) (f :: * -> *).
(MonadThunk t m (NValue t f m), MonadDataContext f m) =>
VarName
-> (NValue t f m
    -> NValue t f m -> NValue t f m -> m (NValue t f m))
-> m (NValue t f m)
builtin3

  add'
    :: ToBuiltin t f m a
    => BuiltinType
    -> VarName
    -> a
    -> m (Builtin (NValue t f m))
  add' :: BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
add' = (VarName -> a -> m (NValue t f m))
-> BuiltinType -> VarName -> a -> m (Builtin (NValue t f m))
forall fun.
(VarName -> fun -> m (NValue t f m))
-> BuiltinType -> VarName -> fun -> m (Builtin (NValue t f m))
hAdd (Text -> a -> m (NValue t f m)
forall t (f :: * -> *) (m :: * -> *) a.
ToBuiltin t f m a =>
Text -> a -> m (NValue t f m)
toBuiltin (Text -> a -> m (NValue t f m))
-> (VarName -> Text) -> VarName -> a -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarName -> Text
coerce)


-- * Exported

-- | Evaluate expression in the default context.
withNixContext
  :: forall e t f m r
   . (MonadNix e t f m, Has e Options)
  => Maybe Path
  -> m r
  -> m r
withNixContext :: Maybe Path -> m r -> m r
withNixContext Maybe Path
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 <- m Options
forall e (m :: * -> *).
(MonadReader e m, Has e Options) =>
m Options
askOptions

    Scope (NValue t f m) -> m r -> m r
forall a (m :: * -> *) r. Scoped a m => Scope a -> m r -> m r
pushScope
      (OneItem (Scope (NValue t f m)) -> Scope (NValue t f m)
forall x. One x => OneItem x -> x
one (VarName
"__includes", [NValue t f m] -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
[NValue t f m] -> NValue t f m
mkNVList ([NValue t f m] -> NValue t f m) -> [NValue t f m] -> NValue t f m
forall a b. (a -> b) -> a -> b
$ Text -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Text -> NValue t f m
mkNVStrWithoutContext (Text -> NValue t f m) -> (Path -> Text) -> Path -> NValue t f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. IsString a => String -> a
fromString (String -> Text) -> (Path -> String) -> Path -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> String
coerce (Path -> NValue t f m) -> [Path] -> [NValue t f m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Options -> [Path]
getInclude Options
opts))
      (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
$
        (m r -> m r) -> (Path -> m r -> m r) -> Maybe Path -> m r -> m r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
          m r -> m r
forall a. a -> a
id
          (\ Path
path m r
act ->
            do
              String -> m ()
forall (m :: * -> *). Monad m => String -> m ()
traceM (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ String
"Setting __cur_file = " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Path -> String
forall b a. (Show a, IsString b) => a -> b
show Path
path
              Scope (NValue t f m) -> m r -> m r
forall a (m :: * -> *) r. Scoped a m => Scope a -> m r -> m r
pushScope (OneItem (Scope (NValue t f m)) -> Scope (NValue t f m)
forall x. One x => OneItem x -> x
one (VarName
"__cur_file", Path -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
Path -> NValue t f m
mkNVPath Path
path)) m r
act
          )
          Maybe Path
mpath
          m r
action
      )

builtins
  :: forall e t f m
  . ( 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
$ PositionSet -> AttrSet (NValue t f m) -> NValue t f m
forall (f :: * -> *) t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet (NValue t f m) -> NValue t f m
mkNVSet PositionSet
forall a. Monoid a => a
mempty (AttrSet (NValue t f m) -> NValue t f m)
-> m (AttrSet (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (AttrSet (NValue t f m))
buildMap
    (Scope (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 => Scope a -> m r -> m r
`pushScope` m (Scopes m (NValue t f m))
forall a (m :: * -> *). Scoped a m => m (Scopes m a)
askScopes) (Scope (NValue t f m) -> m (Scopes m (NValue t f m)))
-> ([(VarName, NValue t f m)] -> Scope (NValue t f m))
-> [(VarName, NValue t f m)]
-> m (Scopes m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AttrSet (NValue t f m) -> Scope (NValue t f m)
coerce (AttrSet (NValue t f m) -> Scope (NValue t f m))
-> ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> [(VarName, NValue t f m)]
-> Scope (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> ([(VarName, NValue t f m)] -> [(VarName, NValue t f m)])
-> [(VarName, NValue t f m)]
-> AttrSet (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (OneItem [(VarName, NValue t f m)] -> [(VarName, NValue t f m)]
forall x. One x => OneItem x -> x
one (VarName
"builtins", NValue t f m
ref) [(VarName, NValue t f m)]
-> [(VarName, NValue t f m)] -> [(VarName, NValue t f m)]
forall a. Semigroup a => a -> a -> a
<>) ([(VarName, NValue t f m)] -> m (Scopes m (NValue t f m)))
-> m [(VarName, NValue t f m)] -> m (Scopes m (NValue t f m))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m [(VarName, NValue t f m)]
topLevelBuiltins
 where
  buildMap :: m (HashMap VarName (NValue t f m))
  buildMap :: m (AttrSet (NValue t f m))
buildMap         =  [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> ([Builtin (NValue t f m)] -> [(VarName, NValue t f m)])
-> [Builtin (NValue t f m)]
-> AttrSet (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Builtin (NValue t f m) -> (VarName, NValue t f m)
forall v. Builtin v -> (VarName, v)
mapping (Builtin (NValue t f m) -> (VarName, NValue t f m))
-> [Builtin (NValue t f m)] -> [(VarName, NValue t f m)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ([Builtin (NValue t f m)] -> AttrSet (NValue t f m))
-> m [Builtin (NValue t f m)] -> m (AttrSet (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 [(VarName, NValue t f m)]
  topLevelBuiltins :: m [(VarName, NValue t f m)]
topLevelBuiltins = Builtin (NValue t f m) -> (VarName, NValue t f m)
forall v. Builtin v -> (VarName, v)
mapping (Builtin (NValue t f m) -> (VarName, NValue t f m))
-> m [Builtin (NValue t f m)] -> m [(VarName, NValue t f m)]
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> m [Builtin (NValue t f m)]
fullBuiltinsList

  fullBuiltinsList :: m [Builtin (NValue t f m)]
  fullBuiltinsList :: m [Builtin (NValue t f m)]
fullBuiltinsList = Builtin (NValue t f m) -> Builtin (NValue t f m)
forall v. Builtin v -> Builtin v
nameBuiltins (Builtin (NValue t f m) -> Builtin (NValue t f m))
-> m [Builtin (NValue t f m)] -> m [Builtin (NValue t f m)]
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g 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
    nameBuiltins :: Builtin v -> Builtin v
    nameBuiltins :: Builtin v -> Builtin v
nameBuiltins b :: Builtin v
b@(Builtin BuiltinType
TopLevel (VarName, v)
_) = Builtin v
b
    nameBuiltins (Builtin BuiltinType
Normal (VarName, v)
nB) =
      BuiltinType -> (VarName, v) -> Builtin v
forall v. BuiltinType -> (VarName, v) -> Builtin v
Builtin BuiltinType
TopLevel ((VarName, v) -> Builtin v) -> (VarName, v) -> Builtin v
forall a b. (a -> b) -> a -> b
$ (VarName -> VarName) -> (VarName, v) -> (VarName, v)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Text -> Text) -> VarName -> VarName
coerce @(Text -> Text) (Text
"__" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>)) (VarName, v)
nB