packages feed

nova-nix-0.11.0.0 / src / Nix / Eval / Types.hs

{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE LambdaCase #-}{-# LANGUAGE PatternSynonyms #-}-- | Shared types for the Nix evaluator, in a module of their own so that-- "Nix.Eval.Operator", "Nix.Eval.StringInterp", and "Nix.Builtins" can-- reference 'NixValue', t'Thunk', and t'Env' without an import cycle.module Nix.Eval.Types  ( -- * Values    NixValue (.., VStr, VPath),    HeldBytes (..),    BuiltinArg (..),    Thunk (..),    readThunkValue,    readComputed,    thunkUnderEvaluation,    -- * Attribute sets (C-backed sorted arrays)    AttrSet (..),    CAttrSet,    AttrEntry (..),    attrSetLookup,    attrSetLookupSymbol,    attrSetLookupEntry,    attrSetLookupSymbolEntry,    attrSetKeys,    attrSetFoldM,    attrSetFindName,    attrSetAllPairs,    attrSetToMap,    emptyAttrSet,    attrSetFromMap,    attrSetFromEntries,    attrSetFromSymbols,    attrSetFromSymbolEntries,    attrSetFromSymbolEntriesSizedFor,    attrSetMember,    attrSetMemberSymbol,    attrSetNull,    attrSetSize,    attrSetElems,    attrSetToAscList,    attrSetMapWithKey,    attrSetRemoveKeys,    FlatCAttrSet,    flatCAttrSet,    buildCAttrSetKeys,    fillCAttrSetValues,    -- * Lists (C-backed)    CList (..),    emptyCList,    clistFromThunks,    clistThunks,    clistLen,    -- * String context    StringContextElement (..),    StringContext (..),    emptyContext,    mkStr,    mkStrBytes,    bytesToTextLossy,    -- * Environment    Env (..),    NnEnv,    emptyEnv,    envLookup,    envLookupResolved,    newEnv,    newScopeEnv,    pushLazyWithScope,    buildCaptureEnv,    buildBcCaptureEnv,    withScopeAt,    resolveWithScopeAt,    envWithScopesRaw,    -- * Eval-time formals (re-exported from EvalFormals)    EvalFormals (..),    EvalFormal (..),    -- * Thunk operations    compile,    compileSource,    cheapThunk,    cheapThunkBc,    mkThunkBc,    mkApplyThunk,    mkApply2Thunk,    Pending (..),    readPending,    evaluated,    tieFrame,    frameThunkBc,    shareSlot,    envSlotCells,    -- * Lambda marshalling    marshalLambda,    -- * Display    typeName,    typeOfValue,    -- * C thunk-state and value tags (mirror cbits/nn_thunk.h)    pattern ThunkPending,    pattern ThunkComputed,    pattern ThunkBlackhole,    pattern ValueInt,    pattern ValueFloat,    pattern ValueBool,    pattern ValueNull,    pattern ValueStr,    pattern ValuePath,    pattern ValueList,    pattern ValueAttrs,    pattern ValueCtxStr,    pattern ValueLambda,    -- * Cells holding strings    heldString,    StringCell (..),    stringCell,    pathCell,    PrimopCell (..),    primopCell,    -- * Evaluation monad    MonadEval (..),    addErrorTrace,    warnMessage,    PathExistence (..),    EvalPolicy (..),    unrestrictedPolicy,    PureEval,    runPureEval,    storePathOrThrow,  )whereimport Control.Monad (forM_, void, zipWithM_)import Data.Bits (bit, complement, shiftL, testBit, (.&.), (.|.))import Data.ByteString (ByteString)import qualified Data.ByteString as BSimport Data.Int (Int64)import Data.Map.Strict (Map)import qualified Data.Map.Strict as Mapimport Data.Set (Set)import qualified Data.Set as Setimport Data.Text (Text)import qualified Data.Text as Timport qualified Data.Text.Encoding as TEimport Data.Text.Encoding.Error (lenientDecode)import Data.Word (Word32, Word64, Word8)import Foreign.Marshal.Array (peekArray)import Foreign.Ptr (Ptr, WordPtr, castPtr, nullPtr, ptrToWordPtr, wordPtrToPtr)import Foreign.Storable (peekElemOff, pokeElemOff)import GHC.Float (castWord64ToDouble)import Nix.Derivation (Derivation)import Nix.Eval.AllocSite (EnvSite (..), SetSite (..))import Nix.Eval.Builtin (Builtin, builtinAtIndex, builtinIndex, builtinName)import Nix.Eval.CAttrSet (CAttrSet, CAttrSetStep (..), FlatCAttrSet, cattrsetEmpty, cattrsetFind, cattrsetFreeze, cattrsetFromNames, cattrsetIndex, cattrsetInsert, cattrsetLookup, cattrsetMember, cattrsetNew, cattrsetRemoveKeys, cattrsetSetValue, cattrsetSize, cattrsetWalk, cattrsetWalkNext, flatCAttrSet)import Nix.Eval.CBytecode (cbcArg1, cbcArg2, cbcCountedPayload, cbcData, cbcOpcode, cbcShortArg, cbcString, cbcStringBytes, strpartLit, pattern OpList, pattern OpLitBool, pattern OpLitFloat, pattern OpLitInt, pattern OpLitNull, pattern OpLitPath, pattern OpLitUri, pattern OpResolvedVar, pattern OpStr, pattern OpVar)import Nix.Eval.CCtxStr (CCtxStrPtr, cctxstrCtxCount, cctxstrElemHash, cctxstrElemName, cctxstrElemOutput, cctxstrElemTag, cctxstrNew, cctxstrSetAllOutputs, cctxstrSetDrvOutput, cctxstrSetPlain, cctxstrText, pattern SceAllOutputs, pattern SceDrvOutput, pattern ScePlain)import Nix.Eval.CData (CData, MonadCData (..), cdataIO, runCDataPure)import Nix.Eval.CEnv (NnEnv, cenvEmpty, cenvLookupResolved, cenvNameTable, cenvNew, cenvNewScoped, cenvParent, cenvPushWith, cenvResolveWith, cenvSlotCount, cenvSlots, cenvWithCount, cenvWithOwner, cenvWithScopes)import Nix.Eval.CLambda (clambdaAllowExtra, clambdaBody, clambdaEntryDefault, clambdaEntryHasDefault, clambdaEntryName, clambdaEnv, clambdaFormalCount, clambdaFormalsType, clambdaLambdaNameSym, clambdaNameSym, clambdaNew, clambdaSetEntry, pattern FormalsName, pattern FormalsNamedSet, pattern FormalsSet)import Nix.Eval.CList (CList (..), clistFromThunks, clistLen, clistThunks, emptyCList)import Nix.Eval.CPrimop (CPrimopAppPtr, cprimopAppArg, cprimopAppForced, cprimopAppNew, cprimopAppPrimop, cprimopAppTaken)import Nix.Eval.CStatus (cInvariantViolated)import Nix.Eval.CStr (CStrPtr, cstrBytes, cstrFromText, cstrNew, cstrText)import Nix.Eval.CThunk (CThunkPtr, cthunkApplyCell, cthunkBool, cthunkEmptyList, cthunkGetBcIdx, cthunkGetBool, cthunkGetFloat, cthunkGetInt, cthunkGetPrimop, cthunkNewApply1, cthunkNewApply2, cthunkNewBc, cthunkNewComputedAttrs, cthunkNewComputedCtxStr, cthunkNewComputedFloat, cthunkNewComputedInt, cthunkNewComputedLambda, cthunkNewComputedList, cthunkNewComputedPath, cthunkNewComputedPrimop, cthunkNewComputedPrimopApp, cthunkNewComputedStr, cthunkNull, cthunkPayload, cthunkPendingForm, cthunkState, cthunkValueTag, noCode)import Nix.Eval.CallDepth (CallDepth, defaultMaxCallDepth, enterCallFrame, topLevelCallDepth)import Nix.Eval.CanonPath (canonPathValue)import Nix.Eval.Compile (BcCaptureInfo (..), BcCapturedSlots (..), compileExpr, compileFormalsToEval, compileParsed, foldBcCapturedSlots, formalDefaultFromWords, formalDefaultWords)import Nix.Eval.EvalFormals (EvalFormal (..), EvalFormals (..), lambdaNameFromSymbol, lambdaNameSymbol)import Nix.Eval.Policy (EvalPolicy (..), unrestrictedPolicy)import Nix.Eval.RealiseContext (ContextRefusal, PendingOrigin)import Nix.Eval.SourcePos (PosIdx, noPos)import Nix.Eval.Symbol (Symbol (..), invalidSymbolId, symbolIntern, symbolText)import Nix.Expr.Types (CaptureInfo (..), Expr (..), NixAtom (..))import Nix.Fetch.Archive (FetchFailure)import Nix.Parser (ParsedSource)import Nix.Store.Path (BadStorePath, StorePath, badStorePathText)import Nix.Store.Path.Internal (StorePath (StorePath))-- ----------------------------------------------------------------------------- String context-- ----------------------------------------------------------------------------- | A single element of string context, tracking where a string-- references store paths.data StringContextElement  = -- | Plain store path reference (inputSrcs).    SCPlain !StorePath  | -- | Derivation output reference (inputDrvs): .drv path + output name.    SCDrvOutput !StorePath !Text  | -- | All outputs of a derivation (for drvPath itself).    SCAllOutputs !StorePath  deriving (Eq, Ord, Show)-- | Context carried by Nix strings, tracking store path dependencies.newtype StringContext = StringContext {unStringContext :: Set StringContextElement}  deriving (Eq, Ord, Show, Semigroup, Monoid)-- | Empty string context (alias for 'mempty').emptyContext :: StringContextemptyContext = mempty-- | Smart constructor for context-free strings from 'Text'.-- Encodes to the UTF-8 bytes that ARE the string's value: a Nix string-- is a byte string, and Text-producing sites go through here.mkStr :: Text -> NixValuemkStr t = VStr (TE.encodeUtf8 t) emptyContext-- | Smart constructor for context-free strings from raw bytes-- (@builtins.readFile@, substring slices - anything already byte-shaped).mkStrBytes :: ByteString -> NixValuemkStrBytes b = VStr b emptyContext-- | Decode string-value bytes for DISPLAY ONLY (trace output, error-- messages, value pretty-printing): invalid UTF-8 becomes U+FFFD.-- Never feed the result back into a value, a hash, or an attr name --- identity-bearing boundaries decode strictly and error instead.bytesToTextLossy :: ByteString -> TextbytesToTextLossy = TE.decodeUtf8With lenientDecode-- ----------------------------------------------------------------------------- Thunks-- ----------------------------------------------------------------------------- | A thunk: a C arena-allocated memoization cell.---- Each thunk points to an @nn_thunk_t@ in the C arena.-- On first force, the cell transitions PENDING to BLACKHOLE to COMPUTED,-- which detects infinite recursion (BLACKHOLE) and overwrites the-- pending work ('Pending') with the value, as upstream mutates a thunk in-- place.---- A pending cell holds its env as the C pointer, so the env exists before-- the cell does: 'mkThunkBc' takes a built t'Env', and 'tieFrame' builds a-- frame whose bindings point back at it before filling its slots.  A-- deferred application's cell ('mkApplyThunk') holds the cells it applies-- and no env.  A cell lives until the outermost evaluation session ends-- ('Nix.Eval.IO.withEvalSession') unless, under-- 'Nix.Eval.CRegion.RegionsCollect', the close of the force that allocated-- it finds nothing reaching it and frees its line; the static cells-- ('Nix.Eval.CThunk.cthunkNull' and its kin) live for the life of the-- process.---- Two thunks are equal when they are one cell, and 'show' gives the-- cell's address.  Neither reads the cell, whose state changes as it is-- forced; 'readThunkValue' reads it, in order with the evaluation.newtype Thunk = Thunk {unThunk :: CThunkPtr}  deriving (Eq, Show)-- | Named constants for the C thunk-state byte and value-tag byte, mirroring-- the @NN_THUNK_*@ / @NN_VALUE_*@ macros in @cbits\/nn_thunk.h@.  Bidirectional-- pattern synonyms over literals, so a @case@ on a tag keeps the jump-table-- compilation a bare-literal @case@ gets, while the state dispatches (the-- 'forceThunk' of each evaluator) and the value dispatch ('readComputed')-- share one source of truth instead of repeating literals.  These MUST stay-- in lockstep with @cbits\/nn_thunk.h@.pattern ThunkPending, ThunkComputed, ThunkBlackhole :: Word8pattern ThunkPending = 0pattern ThunkComputed = 1pattern ThunkBlackhole = 2-- | The forms of a pending application, mirroring the-- @NN_PENDING_APPLY*@ macros in @cbits\/nn_thunk.h@: a function applied-- to one argument or to two in turn.  A code thunk is told apart by its-- instruction ('noCode').pattern PendingApply1Form, PendingApply2Form :: Word8pattern PendingApply1Form = 1pattern PendingApply2Form = 2-- | Value-kind tags for the C value layout, mirroring @cbits\/nn_thunk.h@.-- The tag selects which payload a COMPUTED thunk carries.pattern ValueInt, ValueFloat, ValueBool, ValueNull, ValueStr, ValuePath, ValueList, ValueAttrs, ValueCtxStr, ValueLambda, ValuePrimop, ValuePrimopApp :: Word8pattern ValueInt = 0pattern ValueFloat = 1pattern ValueBool = 2pattern ValueNull = 3pattern ValueStr = 4pattern ValuePath = 5pattern ValueList = 6pattern ValueAttrs = 7pattern ValueCtxStr = 8pattern ValueLambda = 9pattern ValuePrimop = 10pattern ValuePrimopApp = 11-- | Whether a thunk is being forced right now, so it holds no value yet:-- the cell's BLACKHOLE state.  An answer about this moment: the state-- changes when the force finishes or is unwound.thunkUnderEvaluation :: (MonadCData m) => Thunk -> m BoolthunkUnderEvaluation (Thunk ptr) = (== ThunkBlackhole) <$> cthunkState ptr-- | Read a COMPUTED thunk's value without forcing.-- Returns 'Nothing' for PENDING or BLACKHOLE thunks.readThunkValue :: (MonadCData m) => Thunk -> m (Maybe NixValue)readThunkValue (Thunk ptr) = do  state <- cthunkState ptr  if state == ThunkComputed    then Just <$> readComputed ptr    else pure Nothing-- | The value a COMPUTED cell holds, by its value tag: a scalar or a-- builtin's index read in place, any other value from the C object the-- cell points to.  Every value lives in C, so a tag outside the-- @NN_VALUE_*@ constants is corrupt memory.readComputed :: (MonadCData m) => CThunkPtr -> m NixValuereadComputed ptr = do  tag <- cthunkValueTag ptr  case tag of    ValueInt -> VInt <$> cthunkGetInt ptr    ValueFloat -> VFloat <$> cthunkGetFloat ptr    ValueBool -> (\b -> VBool (b /= 0)) <$> cthunkGetBool ptr    ValueNull -> pure VNull    ValueStr -> cthunkPayload ptr >>= heldString . castPtr    ValuePath -> do      str <- castPtr <$> cthunkPayload ptr      (`VPathHeld` HeldStr str) <$> cstrText str    ValueList -> VList . CList . castPtr <$> cthunkPayload ptr    ValueAttrs -> VAttrs . AttrSet . castPtr <$> cthunkPayload ptr    ValueCtxStr -> do      csptr <- castPtr <$> cthunkPayload ptr      (\(bytes, ctx) -> VStrHeld bytes ctx (HeldCtxStr csptr)) <$> unmarshalStringContext csptr    ValueLambda -> cthunkPayload ptr >>= unmarshalLambdaValue    ValuePrimop -> (`VBuiltin` []) <$> (cthunkGetPrimop ptr >>= knownBuiltin)    ValuePrimopApp -> cthunkPayload ptr >>= readPrimopApp . castPtr    other -> cdataIO (cInvariantViolated "nn_thunk_value_tag" ("no value has tag " ++ show other))-- | The builtin at an index a cell holds.knownBuiltin :: (MonadCData m) => Word32 -> m BuiltinknownBuiltin index = maybe unknown pure (builtinAtIndex index)  where    unknown = cdataIO (cInvariantViolated "nn_thunk_get_primop" ("no builtin has index " ++ show index))-- | A builtin's partial application, read from its C object: an argument-- taken forced as the value its cell holds, any other as the cell.readPrimopApp :: (MonadCData m) => CPrimopAppPtr -> m NixValuereadPrimopApp app = do  builtin <- cprimopAppPrimop app >>= knownBuiltin  taken <- cprimopAppTaken app  forced <- cprimopAppForced app  VBuiltin builtin <$> traverse (argumentAt forced) (take (fromIntegral taken) [0 ..])  where    argumentAt forced i = do      cell <- cprimopAppArg app i      if testBit forced (fromIntegral i)        then ArgForced <$> (readThunkValue (Thunk cell) >>= maybe notComputed pure)        else pure (ArgDeferred (Thunk cell))    notComputed = cdataIO (cInvariantViolated "nn_primop_app_arg" "an argument taken forced is not COMPUTED")-- | A Nix value - the result of evaluating an expression.data NixValue  = -- | 64-bit signed integer (matching Nix semantics).    VInt !Int64  | -- | Floating-point.    VFloat !Double  | -- | Boolean.    VBool !Bool  | -- | The null value.    VNull  | -- | String with dependency context, and where its bytes already are    -- in C ('VStr' builds and matches one without).  The payload is a    -- BYTE string, as upstream: @stringLength@/@substring@ index bytes,    -- regexes match bytes, and @readFile@ carries raw file bytes (so    -- invalid UTF-8 is representable).  Text enters via 'mkStr' (UTF-8    -- encode) and leaves through explicit decode at boundaries (attr    -- names strictly, display lossily via 'bytesToTextLossy').    VStrHeld !ByteString !StringContext !HeldBytes  | -- | Path, and where its bytes already are in C ('VPath' builds and    -- matches one without).    VPathHeld !Text !HeldBytes  | -- | List of thunks (lazy elements), backed by C array.    VList !CList  | -- | Attribute set: unified lazy/eager representation.    VAttrs !AttrSet  | -- | Lambda closure: captures environment, formals, body bytecode index.    VLambda !Env !EvalFormals !Word32  | -- | Built-in function, with the arguments a partial application has    -- taken so far, first to last.    VBuiltin !Builtin ![BuiltinArg]  deriving (Eq, Show)-- | A string with dependency context: its bytes and their context.  Built-- with it, a value's bytes are held in Haskell only, and a cell that-- stores it takes a copy.pattern VStr :: ByteString -> StringContext -> NixValuepattern VStr bytes ctx <- VStrHeld bytes ctx _  where    VStr bytes ctx = VStrHeld bytes ctx NotHeld-- | A path.  Built with it, a path's bytes are held in Haskell only, and-- a cell that stores it takes a copy.pattern VPath :: Text -> NixValuepattern VPath path <- VPathHeld path _  where    VPath path = VPathHeld path NotHeld{-# COMPLETE VInt, VFloat, VBool, VNull, VStr, VPath, VList, VAttrs, VLambda, VBuiltin #-}-- | Where a string or path value's bytes already are in the C data-- layer.  A value read from a cell, and a literal's value, carry the C-- object their bytes are in, and a cell that stores the value points at-- that object rather than at a copy, as upstream copies a @Value@ with-- the pointer to its bytes (value.hh at 2.24.9): a string passed from-- cell to cell takes its bytes once.  A value is built held only where-- its bytes are read from the object it holds, and the objects never-- change once made, so a value's bytes stay its object's.data HeldBytes  = -- | In Haskell only.    NotHeld  | -- | A context-free string's or a path's bytes.    HeldStr !CStrPtr  | -- | A string with context, whole.    HeldCtxStr !CCtxStrPtr-- | Every two are equal.  The held object is where a value's bytes live,-- not part of the value, so two values with equal bytes and context are-- equal wherever their bytes are.instance Eq HeldBytes where  _ == _ = Trueinstance Show HeldBytes where  showsPrec _ held = showString $ case held of    NotHeld -> "NotHeld"    HeldStr _ -> "HeldStr"    HeldCtxStr _ -> "HeldCtxStr"-- | The value of a context-free string whose bytes are a C string: a-- cell's, or a literal's in the code.heldString :: (MonadCData m) => CStrPtr -> m NixValueheldString str = (\bytes -> VStrHeld bytes emptyContext (HeldStr str)) <$> cstrBytes str-- | An argument a builtin has taken.  A primop that forces an argument-- before anything it does can be observed has it forced as it is-- applied.  One that forces it later, after another argument or only on-- some paths, has it held as the thunk it arrived as, and forces it where-- upstream does.  An argument that arrives already forced is held forced-- either way.data BuiltinArg  = ArgForced !NixValue  | ArgDeferred !Thunk  deriving (Eq, Show)-- ----------------------------------------------------------------------------- Attribute sets (C-backed sorted arrays)-- ----------------------------------------------------------------------------- | An attribute set: a C set (@cbits/nn_attrset.h@) whose entries are-- sorted by symbol id, each an interned name, the position it was-- defined at and its value's cell, so a lookup is a binary search of-- each of its layers, top first.---- Two sets are equal when they are one C set, and 'show' gives its-- address.  Neither reads the set; 'attrSetToMap' reads its entries.newtype AttrSet = AttrSet {unAttrSet :: CAttrSet}  deriving (Eq, Show)-- | An attribute's value and the position it was defined at, as a set-- holds them.data AttrEntry = AttrEntry  { entryPos :: {-# UNPACK #-} !PosIdx,    entryThunk :: {-# UNPACK #-} !Thunk  }-- | The value a set binds to an interned name, found by a binary search-- of each of its layers, top first, each sorted by symbol.attrSetLookupSymbol :: (MonadCData m) => Symbol -> AttrSet -> m (Maybe Thunk)attrSetLookupSymbol sym (AttrSet cset) = fmap Thunk <$> cattrsetLookup cset sym-- | Whether a set binds an interned name, a name table whose entry is not-- filled yet among them.attrSetMemberSymbol :: (MonadCData m) => Symbol -> AttrSet -> m BoolattrSetMemberSymbol sym (AttrSet cset) = cattrsetMember cset sym-- | 'attrSetLookupSymbol' for a name given as text, which is interned-- first, so a name no symbol holds yet takes its place in symbol order.attrSetLookup :: (MonadCData m) => Text -> AttrSet -> m (Maybe Thunk)attrSetLookup key attrs = do  sym <- symbolIntern key  attrSetLookupSymbol sym attrs-- | 'attrSetLookupSymbol' with the attribute's position.attrSetLookupSymbolEntry :: (MonadCData m) => Symbol -> AttrSet -> m (Maybe AttrEntry)attrSetLookupSymbolEntry sym (AttrSet cset) = fmap (\(pos, ptr) -> AttrEntry pos (Thunk ptr)) <$> cattrsetFind cset sym-- | 'attrSetLookup' with the attribute's position.attrSetLookupEntry :: (MonadCData m) => Text -> AttrSet -> m (Maybe AttrEntry)attrSetLookupEntry key attrs = do  sym <- symbolIntern key  attrSetLookupSymbolEntry sym attrs-- | All keys, as symbol text from C, in the set's order: by interned-- symbol, not by name ('attrSetFoldM').attrSetKeys :: (MonadCData m) => AttrSet -> m [Text]attrSetKeys attrs = reverse <$> attrSetFoldM (\names name _ -> pure (symbolText name : names)) [] attrs-- | Fold an action over a set's attributes, each its name and its entry,-- first to last by interned symbol, as upstream walks the @Attr@s of a-- set's @Bindings@, which are sorted by @Symbol@ (@Attr@'s comparison and-- the @Bindings@ iterators in attr-set.hh), merging its layers' at 2.34.9.-- It reads a flat set one entry at a time, and a layered one from the-- entries 'cattrsetWalk' copies out of its layers first, and forces the-- accumulator at each.{-# INLINE attrSetFoldM #-}attrSetFoldM :: (MonadCData m) => (a -> Symbol -> AttrEntry -> m a) -> a -> AttrSet -> m aattrSetFoldM step initial (AttrSet cset) = cattrsetWalk cset >>= go initial  where    go !acc walk =      cattrsetWalkNext walk >>= \case        CAttrSetDone -> pure acc        CAttrSetEntry name pos ptr rest -> step acc name (AttrEntry pos (Thunk ptr)) >>= \next -> go next rest-- | The first name of a set, in the order 'attrSetFoldM' walks it, that a-- test holds for, or 'Nothing' if it holds for none.  The walk stops at-- that name and forces no value.attrSetFindName :: (MonadCData m) => (Symbol -> Bool) -> AttrSet -> m (Maybe Symbol)attrSetFindName holds (AttrSet cset) = cattrsetWalk cset >>= go  where    go walk =      cattrsetWalkNext walk >>= \case        CAttrSetDone -> pure Nothing        CAttrSetEntry name _ _ rest          | holds name -> pure (Just name)          | otherwise -> go rest-- | Whether an action answers 'True' for every pair of attributes, each a-- name and its value, that two sets hold at one place in the order-- 'attrSetFoldM' walks them, asked first to last.  The walk stops at the-- first pair the action answers 'False' for, so it asks of no pair past-- it, and answers 'False' where one set ends before the other.attrSetAllPairs :: (MonadCData m) => ((Symbol, Thunk) -> (Symbol, Thunk) -> m Bool) -> AttrSet -> AttrSet -> m BoolattrSetAllPairs holds (AttrSet left) (AttrSet right) = do  leftWalk <- cattrsetWalk left  rightWalk <- cattrsetWalk right  go leftWalk rightWalk  where    go leftWalk rightWalk = do      leftStep <- cattrsetWalkNext leftWalk      rightStep <- cattrsetWalkNext rightWalk      case (leftStep, rightStep) of        (CAttrSetEntry leftName _ leftPtr leftRest, CAttrSetEntry rightName _ rightPtr rightRest) -> do          held <- holds (leftName, Thunk leftPtr) (rightName, Thunk rightPtr)          if held then go leftRest rightRest else pure False        (CAttrSetDone, CAttrSetDone) -> pure True        _ -> pure False-- | Check key membership without materializing thunks.attrSetMember :: (MonadCData m) => Text -> AttrSet -> m BoolattrSetMember key attrs = do  sym <- symbolIntern key  attrSetMemberSymbol sym attrs-- | Check if the attribute set is empty.attrSetNull :: (MonadCData m) => AttrSet -> m BoolattrSetNull (AttrSet cset) = (== 0) <$> cattrsetSize cset-- | Number of attributes.attrSetSize :: (MonadCData m) => AttrSet -> m IntattrSetSize (AttrSet cset) = fromIntegral <$> cattrsetSize cset-- | Full materialization: build a 'Map Text Thunk' from all entries.-- Walks the set ('attrSetFoldM') and builds a Haskell Map.  Expensive-- on large sets - avoid on the hot path.attrSetToMap :: (MonadCData m) => AttrSet -> m (Map Text Thunk)attrSetToMap = attrSetFoldM (\acc name entry -> pure (Map.insert (symbolText name) (entryThunk entry) acc)) Map.empty-- | All thunk values (materialized).attrSetElems :: (MonadCData m) => AttrSet -> m [Thunk]attrSetElems attrs = Map.elems <$> attrSetToMap attrs-- | Sorted key-value pairs (materialized).attrSetToAscList :: (MonadCData m) => AttrSet -> m [(Text, Thunk)]attrSetToAscList attrs = Map.toAscList <$> attrSetToMap attrs-- | A set at a site binding each name of a set to what an action makes-- of the name and its value, as upstream's @mapAttrs@ builds its result-- (@prim_mapAttrs@ in primops.cc): the action runs on each attribute in-- the order the set holds them ('attrSetFoldM'), and the result is sized-- for the set and binds its names, each defined nowhere in source.  An-- empty set gives 'emptyAttrSet', as upstream's @allocBindings@ answers-- a size of 0 with its shared bindings (attr-set.cc).attrSetMapWithKey :: (MonadCData m) => SetSite -> (Symbol -> Thunk -> m Thunk) -> AttrSet -> m AttrSetattrSetMapWithKey site f attrs =  attrSetSize attrs >>= \case    0 -> pure emptyAttrSet    size -> do      mapped <- cattrsetNew site size      attrSetFoldM (\() name entry -> f name (entryThunk entry) >>= cattrsetInsert mapped name noPos . unThunk) () attrs      cattrsetFreeze mapped      pure (AttrSet mapped)-- | Remove a list of keys, returning a new CAttrSet made at a site.-- Uses the C-side @nn_attrset_remove_keys@, which scans the keys for-- each attribute.  Upstream-- sizes the result for the set it removes from (@prim_removeAttrs@ in-- primops.cc), so removing from an empty set gives 'emptyAttrSet' and-- removing every attribute of another gives an empty set of its own.-- Every key is interned either way, as upstream interns each name-- whatever the set holds.attrSetRemoveKeys :: (MonadCData m) => SetSite -> [Text] -> AttrSet -> m AttrSetattrSetRemoveKeys site keys (AttrSet cset) = do  syms <- mapM symbolIntern keys  size <- cattrsetSize cset  case size of    0 -> pure emptyAttrSet    _ -> AttrSet <$> cattrsetRemoveKeys site cset syms-- | The set with no attributes: a C static ('cattrsetEmpty'), one address-- for the life of the process, as upstream's @emptyBindings@ is one for-- its evaluator (@EvalState@'s member in eval.hh at 2.24.9, a static of-- @Bindings@ in attr-set.hh at 2.36.0).  Every set built with no attributes-- is this one and is allocated nowhere, as upstream's @allocBindings@-- answers a size of 0 with @emptyBindings@ (attr-set.cc), except a set-- upstream sizes before it knows it holds nothing-- ('attrSetFromSymbolEntriesSizedFor', 'attrSetRemoveKeys').  Upstream's-- printers enter a set by its bindings, so which empty sets are this one-- shows where a value is printed.emptyAttrSet :: AttrSetemptyAttrSet = AttrSet cattrsetEmpty-- | Build a CAttrSet at a site from a Haskell 'Map' of attributes defined-- nowhere in source, as those of a set a builtin makes from scratch are;-- a builtin that copies attributes keeps their positions with-- 'attrSetFromEntries'.  The keys are interned in name order, so a name-- no symbol holds yet takes its place in every set by name; a builtin-- that meets new names in an order of its own interns each where-- upstream's does and builds with 'attrSetFromSymbols'.  An empty map-- gives 'emptyAttrSet'.attrSetFromMap :: (MonadCData m) => SetSite -> Map Text Thunk -> m AttrSetattrSetFromMap site m = AttrSet <$> newFrozenAttrSet site symbolIntern (const noPos) id m-- | 'attrSetFromMap' for attributes that keep the positions they were-- defined at, as an attribute set literal's and a copied attribute's do.attrSetFromEntries :: (MonadCData m) => SetSite -> Map Text AttrEntry -> m AttrSetattrSetFromEntries site m = AttrSet <$> newFrozenAttrSet site symbolIntern entryPos entryThunk m-- | 'attrSetFromMap' over names already interned.attrSetFromSymbols :: (MonadCData m) => SetSite -> Map Symbol Thunk -> m AttrSetattrSetFromSymbols site m = AttrSet <$> newFrozenAttrSet site pure (const noPos) id m-- | 'attrSetFromEntries' over names already interned.attrSetFromSymbolEntries :: (MonadCData m) => SetSite -> Map Symbol AttrEntry -> m AttrSetattrSetFromSymbolEntries site m = AttrSet <$> newFrozenAttrSet site pure entryPos entryThunk m-- | 'attrSetFromSymbolEntries' for a set upstream sizes, before it knows-- what the set holds, for the given number of attributes, as it sizes a-- literal for its names (@ExprAttrs::eval@ in eval.cc) and-- @intersectAttrs@'s result for the smaller of its arguments-- (@prim_intersectAttrs@ in primops.cc).  Upstream's @allocBindings@ shares-- its empty set only for a size of 0 (attr-set.cc), so such a set holding-- nothing is 'emptyAttrSet' when sized for none and an empty set of its own-- otherwise.  A set holding attributes is made with room for those alone,-- whatever the size.attrSetFromSymbolEntriesSizedFor :: (MonadCData m) => SetSite -> Int -> Map Symbol AttrEntry -> m AttrSetattrSetFromSymbolEntriesSizedFor site size m  | Map.null m && size > 0 = ownEmptyAttrSet site  | otherwise = attrSetFromSymbolEntries site m-- | An empty set at a site that is not 'emptyAttrSet', made with room for-- one entry: room for none asks @nn_attrset_new@ for its default room,-- and is what the census counts as a set upstream would share.ownEmptyAttrSet :: (MonadCData m) => SetSite -> m AttrSetownEmptyAttrSet site = do  cset <- cattrsetNew site 1  cattrsetFreeze cset  pure (AttrSet cset)-- | Insert every entry of a map into a new set at a site, under the-- symbol, at the position and with the thunk the projections give it,-- and freeze it; the shared empty set for an empty map.{-# INLINE newFrozenAttrSet #-}newFrozenAttrSet :: (MonadCData m) => SetSite -> (k -> m Symbol) -> (a -> PosIdx) -> (a -> Thunk) -> Map k a -> m CAttrSetnewFrozenAttrSet site symbolOf posOf thunkOf m  | Map.null m = pure cattrsetEmpty  | otherwise = do      cset <- cattrsetNew site (Map.size m)      forM_ (Map.toList m) $ \(key, entry) -> do        sym <- symbolOf key        cattrsetInsert cset sym (posOf entry) (unThunk (thunkOf entry))      cattrsetFreeze cset      pure cset-- | Allocate a CAttrSet skeleton with keys only (NULL values), for an-- environment's name table ('cattrsetFromNames').-- Used for two-phase construction in @rec {}@ and @let@ (knot-tying):-- keys are known before thunks, so the CAttrSet is built first, then-- values are filled via 'fillCAttrSetValues'.  The table is never a-- value, so its entries carry no position.  A table of no keys is the-- shared empty set, which 'fillCAttrSetValues' finds no key in.buildCAttrSetKeys :: (MonadCData m) => [Symbol] -> m FlatCAttrSetbuildCAttrSetKeys = cattrsetFromNames SetScope-- | Fill values into a pre-allocated CAttrSet (two-phase construction).-- Looks up each key's index and writes the CThunkPtr at that position.-- A key the set does not hold has no entry to fill and is skipped.fillCAttrSetValues :: (MonadCData m) => FlatCAttrSet -> Map Symbol Thunk -> m ()fillCAttrSetValues cset thunkMap =  forM_ (Map.toList thunkMap) $ \(sym, Thunk ptr) -> do    midx <- cattrsetIndex cset sym    case midx of      Just idx -> cattrsetSetValue cset idx ptr      Nothing -> pure ()-- | Evaluation environment - C-backed scope chain.---- An arena-allocated @nn_env_t@: a word holding its parent and slot-- count or a header, its slots inline, then, after a header, its parent-- when it has one and its name table and with-scopes when it has them-- (@cbits/nn_env.h@).  Haskell holds only the pointer.  Each kind of-- variable the resolver makes has one lookup:---- * 'envLookupResolved': a single C call, for 'EResolvedVar'-- * 'envLookup': a walk of the name tables on the parent chain, for 'EVar'-- * 'envWithScopesRaw': the with-scopes an 'EWithVar' is looked up in,--   innermost first---- Two envs are equal when they are one C env, and 'show' gives its-- address.  Neither reads the env.newtype Env = Env (Ptr NnEnv)  deriving (Eq, Show)-- | Empty environment (no variables in scope): a C static, one address-- for the life of the process.emptyEnv :: EnvemptyEnv = Env cenvEmpty-- | An env at a site holding the thunks in its slots, in order, under a-- parent or none.  It sees its parent's with-scopes.newEnv :: (MonadCData m) => EnvSite -> Maybe Env -> [Thunk] -> m EnvnewEnv site parent thunks = do  env <- newBlankEnv site parent (length thunks)  fillSlots env thunks  pure env-- | An env at a site of empty slots under a parent or none, for-- 'tieFrame' to fill.newBlankEnv :: (MonadCData m) => EnvSite -> Maybe Env -> Int -> m EnvnewBlankEnv site parent slotCount =  Env <$> cenvNew site (fromIntegral slotCount) (parentPtr parent)-- | An env at a site with no slots whose name table is a set, binding its-- names, under a parent or none: the root env's builtins, scopedImport's-- names over them, or the static names of a frame that also has dynamic-- ones.newScopeEnv :: (MonadCData m) => EnvSite -> Maybe Env -> AttrSet -> m EnvnewScopeEnv site parent (AttrSet cset) =  Env <$> cenvNewScoped site 0 (parentPtr parent) (castPtr cset) nullPtr 0-- | The C pointer of a parent, NULL for none.parentPtr :: Maybe Env -> Ptr NnEnvparentPtr = maybe nullPtr (\(Env p) -> p)-- | Look up a resolved variable by level and index: O(level) parent hops-- in C, then a slot read, in one call.  A level or index outside the env,-- or a slot a frame has not filled yet, raises-- 'Nix.Eval.CStatus.CInvariantViolated' ('cenvLookupResolved').envLookupResolved :: (MonadCData m) => Word32 -> Word32 -> Env -> m ThunkenvLookupResolved level idx (Env envPtr) = Thunk <$> cenvLookupResolved envPtr level idx-- | Name-based variable lookup for 'EVar': the name's thunk in the first-- name table on the parent chain, the env's own included, that binds it-- to a filled entry; an entry not filled yet is passed over as if the-- table did not bind the name.-- Positional slots are not searched.  The resolver makes an 'EVar' only-- of a name a frame's name table or the root env binds, and closure-- trimming keeps the parent chain of every env an 'EVar' is evaluated in,-- so the name is found unless the expression was built without the-- resolver.envLookup :: (MonadCData m) => Symbol -> Env -> m (Maybe Thunk)envLookup name (Env envPtr) = walk envPtr  where    walk ep = do      table <- cenvNameTable ep      found <-        if table == nullPtr          then pure Nothing          else attrSetLookupSymbol name (AttrSet (castPtr table))      case found of        Just thunk -> pure (Just thunk)        Nothing -> cenvParent ep >>= up    up parent      | parent == nullPtr = pure Nothing      | otherwise = walk parent-- | Push a with-scope whose set is not evaluated yet onto the scope chain-- (innermost position): the thunk that evaluates to it, tagged lazy-- (bit 0 set) so that a lookup forces it on first use-- ('resolveWithScopeAt').  Allocates a copy of the env, with a with-scopes-- array one longer than the one it sees.pushLazyWithScope :: (MonadCData m) => Thunk -> Env -> m EnvpushLazyWithScope (Thunk thunkPtr) (Env envPtr) =  Env <$> cenvPushWith EnvWith envPtr (wordPtrToPtr (ptrToWordPtr (castPtr thunkPtr) .|. lazyWithScopeTag))-- | @NN_ENV_LAZY_WITH@ of @cbits/nn_env.h@, and must stay in lockstep-- with it: the bit that marks a with-scope entry as the thunk of a set-- not yet evaluated, rather than the set.  A thunk cell is word-aligned,-- so the bit is otherwise zero.lazyWithScopeTag :: WordPtrlazyWithScopeTag = 1-- | The entry at an index of a with-scopes array ('envWithScopesRaw'):-- 'Left' the thunk of a set not yet evaluated ('pushLazyWithScope'), or-- 'Right' the set.withScopeAt :: (MonadCData m) => Ptr (Ptr ()) -> Int -> m (Either Thunk AttrSet)withScopeAt withArr idx = classify . ptrToWordPtr <$> cdataIO (peekElemOff withArr idx)  where    classify word      | word .&. lazyWithScopeTag /= 0 = Left (Thunk (wordPtrToPtr (word .&. complement lazyWithScopeTag)))      | otherwise = Right (AttrSet (wordPtrToPtr word))-- | Replace a lazy with-scope entry with the set its thunk evaluated to,-- so later lookups read the set directly.  Every env sharing the array-- would evaluate the thunk to the same set.resolveWithScopeAt :: (MonadCData m) => Ptr (Ptr ()) -> Int -> AttrSet -> m ()resolveWithScopeAt withArr idx (AttrSet cset) = cenvResolveWith withArr (fromIntegral idx) (castPtr cset)-- | The with-scopes an env sees, innermost first, and their count: those-- of the first env on its parent chain, itself included, that has-- with-scopes of its own (a with's env, or a trimmed closure's), or none.envWithScopesRaw :: (MonadCData m) => Env -> m (Ptr (Ptr ()), Word32)envWithScopesRaw (Env envPtr) = do  owner <- cenvWithOwner envPtr  if owner == nullPtr    then pure (nullPtr, 0)    else (,) <$> cenvWithScopes owner <*> cenvWithCount owner-- | Compile an expression to bytecode, answering the index of its code.-- Appends to the bytecode store; the same expression compiled twice-- answers two indices running the same code.compile :: (MonadCData m) => Expr -> m Word32compile expr = cdataIO (compileExpr expr)-- | 'compile' for parsed source, whose names are interned first, in the-- order upstream's parser interns them ('compileParsed').compileSource :: (MonadCData m) => ParsedSource -> m Word32compileSource parsed = cdataIO (compileParsed parsed)-- | Create an unevaluated thunk with a fresh C arena-allocated cell,-- compiling the Expr to bytecode first.mkThunk :: (MonadCData m) => Env -> Expr -> m ThunkmkThunk env thunkExpr = compile thunkExpr >>= mkThunkBc env-- | Like 'mkThunk' but makes no pending thunk for trivial expressions.-- Resolved variables reuse the existing thunk from the env (no wrapper).-- Literals use inline C scalars.-- Lambdas compile formals+body to bytecode and produce VLambda directly.-- Everything else falls back to 'mkThunk'.cheapThunk :: (MonadCData m) => Env -> Expr -> m ThunkcheapThunk env (EResolvedVar level idx) = envLookupResolved (fromIntegral level) (fromIntegral idx) envcheapThunk _ (ELit (NixInt n)) = Thunk <$> cthunkNewComputedInt ncheapThunk _ (ELit (NixFloat n)) = Thunk <$> cthunkNewComputedFloat ncheapThunk _ (ELit (NixBool b)) = pure (Thunk (cthunkBool b))cheapThunk _ (ELit NixNull) = pure (Thunk cthunkNull)cheapThunk env (ELambda formals body captureInfo) = do  closureEnv <- buildCaptureEnv EnvClosure env captureInfo  bodyBcIdx <- compile body  evalFormals <- cdataIO (compileFormalsToEval formals)  evaluated (VLambda closureEnv evalFormals bodyBcIdx)cheapThunk env expr = mkThunk env expr-- | The env, at a site, that a closure made in an env runs in, by its-- capture info: 'NoCaptureInfo' keeps the env as it is, 'Captures' makes-- an env of the captured slots with no parent, and 'CapturesWithScopes'-- gives that env the with-scopes the env it is made in sees as its own,-- the same array ('cenvNewScoped').  When that env sees none, the-- closure's with-variables are all under withs inside it, and its env is-- the one 'Captures' makes.buildCaptureEnv :: (MonadCData m) => EnvSite -> Env -> CaptureInfo -> m EnvbuildCaptureEnv _ env NoCaptureInfo = pure envbuildCaptureEnv site env (Captures captureList) = captureListEnv site env NoWithScopes captureListbuildCaptureEnv site env (CapturesWithScopes captureList) = captureListEnv site env EnclosingWithScopes captureList-- | 'buildCaptureEnv' for capture info the bytecode holds: each captured-- slot is filled as its pair is read from the data buffer.buildBcCaptureEnv :: (MonadCData m) => EnvSite -> Env -> BcCaptureInfo -> m EnvbuildBcCaptureEnv _ env BcNoCaptureInfo = pure envbuildBcCaptureEnv site env (BcCaptures slots) = capturedSlotsEnv site env NoWithScopes slotsbuildBcCaptureEnv site env (BcCapturesWithScopes slots) = capturedSlotsEnv site env EnclosingWithScopes slots-- | The with-scopes the env of a closure's captured slots sees.data CapturedScopes  = -- | None ('Captures').    NoWithScopes  | -- | Those the env the closure is made in sees ('CapturesWithScopes').    EnclosingWithScopes-- | The env of a closure's captured slots, from a list of their pairs.captureListEnv :: (MonadCData m) => EnvSite -> Env -> CapturedScopes -> [(Int, Int)] -> m EnvcaptureListEnv site env scopes captureList = do  thunks <- capturedThunks env captureList  captured <- newCaptureEnv site env scopes (length thunks)  fillSlots captured thunks  pure captured-- | The env of a closure's captured slots, from their pairs in the data-- buffer.capturedSlotsEnv :: (MonadCData m) => EnvSite -> Env -> CapturedScopes -> BcCapturedSlots -> m EnvcapturedSlotsEnv site env scopes slots@(BcCapturedSlots count _) = do  captured@(Env capturedPtr) <- newCaptureEnv site env scopes count  dest <- cenvSlots capturedPtr  let fillSlot i level idx = do        Thunk cell <- envLookupResolved level idx env        cdataIO (pokeElemOff dest i cell)        pure (i + 1)  void (foldBcCapturedSlots fillSlot 0 slots)  pure captured-- | An env at a site of empty slots and no parent, for the slots a-- closure made in an env captures from it, seeing the with-scopes-- 'buildCaptureEnv' describes.newCaptureEnv :: (MonadCData m) => EnvSite -> Env -> CapturedScopes -> Int -> m EnvnewCaptureEnv site _ NoWithScopes slotCount = newBlankEnv site Nothing slotCountnewCaptureEnv site env EnclosingWithScopes slotCount = do  (withArr, withCount) <- envWithScopesRaw env  if withCount == 0    then newBlankEnv site Nothing slotCount    else Env <$> cenvNewScoped site (fromIntegral slotCount) nullPtr nullPtr withArr withCount-- | The thunks a closure captures, read from the env it is made in by-- their resolved level and index.capturedThunks :: (MonadCData m) => Env -> [(Int, Int)] -> m [Thunk]capturedThunks env = mapM (\(lvl, idx) -> envLookupResolved (fromIntegral lvl) (fromIntegral idx) env)-- | A pending thunk running the code at a bytecode index in an env: a-- fresh PENDING cell holding the index and the env's C pointer, neither-- of them on the GHC heap.  Forcing the t'Thunk' runs the code in that-- env.  The env's slots need not be filled yet: 'tieFrame' builds a-- recursive frame's env before the thunks that point at it and fills its-- slots after.mkThunkBc :: (MonadCData m) => Env -> Word32 -> m ThunkmkThunkBc (Env envPtr) bcIdx = Thunk <$> cthunkNewBc bcIdx (castPtr envPtr)-- | A pending thunk applying the function in one cell to the argument in-- another when it is forced, as upstream's app value does (@mkApp@,-- value.hh at 2.24.9): a fresh PENDING cell holding the two cells, which-- live in the env pages, and no env.  Unlike an app value, which upstream-- never blackholes, the cell is blackholed while it is forced, as every-- pending cell is.mkApplyThunk :: (MonadCData m) => Thunk -> Thunk -> m ThunkmkApplyThunk (Thunk fn) (Thunk arg) = Thunk <$> cthunkNewApply1 fn arg-- | 'mkApplyThunk' applying the function to a first argument and the-- result to a second, as upstream's @mapAttrs@ and @zipAttrsWith@ nest-- two app values.  The inner application's result is not kept, where-- upstream's inner app value holds it once forced, so a force retried-- after a failure applies the function to the first argument again.mkApply2Thunk :: (MonadCData m) => Thunk -> Thunk -> Thunk -> m ThunkmkApply2Thunk (Thunk fn) (Thunk first) (Thunk second) = Thunk <$> cthunkNewApply2 fn first second-- | What a pending thunk does when it is forced.data Pending  = -- | Run the code at a bytecode index in an env ('mkThunkBc').    PendingCode !Env !Word32  | -- | Apply the function in a cell to the argument in another    -- ('mkApplyThunk').    PendingApply !Thunk !Thunk  | -- | Apply the function to the first argument and the result to the    -- second ('mkApply2Thunk').    PendingApply2 !Thunk !Thunk !Thunk-- | What a PENDING or BLACKHOLE thunk does, as 'mkThunkBc' or-- 'mkApplyThunk' and 'mkApply2Thunk' stored it.  Its instruction is read-- first, so a code thunk, nearly every thunk forced, takes two reads:-- its instruction and its env.readPending :: (MonadCData m) => CThunkPtr -> m PendingreadPending ptr =  cthunkGetBcIdx ptr >>= \case    bcIdx      | bcIdx /= noCode -> (\env -> PendingCode (Env (castPtr env)) bcIdx) <$> cthunkPayload ptr      | otherwise ->          cthunkPendingForm ptr >>= \case            PendingApply1Form -> PendingApply <$> cell 0 <*> cell 1            PendingApply2Form -> PendingApply2 <$> cell 0 <*> cell 1 <*> cell 2            other -> cdataIO (cInvariantViolated "nn_thunk_pending_form" ("no pending application has form " ++ show other))  where    cell i = Thunk <$> cthunkApplyCell ptr i-- | The cell a binding, a list element, a call's argument or a with's-- scope holds for the code at a bytecode index in an env, as upstream's-- @maybeThunk@ makes it (eval.cc).  A variable is its own cell, found by its-- slot or, when the resolver left it to a name table, by its name-- ('envLookup'), so the binding and the variable are one value, as @==@-- shows of a function.  A constant is a cell of its value.  Anything else is-- a pending thunk ('mkThunkBc'): a variable only a with supplies among them,-- since upstream looks in no with until the variable is forced-- (@EvalState::lookupVar@ in eval.cc), and a name no table binds, which-- fails when the thunk is forced.---- Every slot and name table the variable is read from must be filled: a-- frame that is being tied makes its values with 'frameThunkBc'.cheapThunkBc :: (MonadCData m) => Env -> Word32 -> m ThunkcheapThunkBc = maybeThunkBc OwnSlotsFilled-- | 'cheapThunkBc' for a value made in a frame that is being tied-- ('tieFrame'), whose own slots are not filled yet: a variable naming one-- of them is a thunk of its own, as upstream's @maybeThunk@ makes it for-- a slot it has not filled (@ExprVar::maybeThunk@ in eval.cc).  The frame's-- shares then give it the slot's cell where upstream's would have it-- ('Nix.Eval.Compile.siblingShares').frameThunkBc :: (MonadCData m) => Env -> Word32 -> m ThunkframeThunkBc = maybeThunkBc OwnSlotsEmpty-- | Whether the env a value is made in has filled its own slots: every-- env has once it is built, but a frame that is being tied has not.data OwnSlots = OwnSlotsFilled | OwnSlotsEmpty-- | 'cheapThunkBc' and 'frameThunkBc', by whether the env's own slots-- are filled.maybeThunkBc :: (MonadCData m) => OwnSlots -> Env -> Word32 -> m ThunkmaybeThunkBc ownSlots env bcIdx =  cbcOpcode bcIdx >>= \case    OpResolvedVar ->      cbcArg1 bcIdx >>= \case        0 | OwnSlotsEmpty <- ownSlots -> mkThunkBc env bcIdx        level -> cbcArg2 bcIdx >>= \idx -> envLookupResolved level idx env    OpVar -> cbcArg1 bcIdx >>= \sym -> envLookup (Symbol sym) env >>= maybe (mkThunkBc env bcIdx) pure    OpLitInt -> do      w64 <- literalWord64 bcIdx      Thunk <$> cthunkNewComputedInt (fromIntegral w64)    OpLitFloat -> do      w64 <- literalWord64 bcIdx      Thunk <$> cthunkNewComputedFloat (castWord64ToDouble w64)    OpLitBool -> do      flag <- cbcShortArg bcIdx      pure (Thunk (cthunkBool (flag /= 0)))    OpLitNull -> pure (Thunk cthunkNull)    OpLitUri -> Thunk <$> (cbcArg1 bcIdx >>= cbcString >>= cthunkNewComputedStr)    OpStr ->      literalStr bcIdx >>= \case        Just str -> Thunk <$> cthunkNewComputedStr str        Nothing -> mkThunkBc env bcIdx    OpLitPath ->      constantPathValue bcIdx >>= \case        Just resolved -> Thunk <$> cthunkNewComputedPath resolved        Nothing -> mkThunkBc env bcIdx    OpList -> do      empty <- emptyBcList bcIdx      if empty        then pure emptyListThunk        else mkThunkBc env bcIdx    _ -> mkThunkBc env bcIdx-- | The one cell every empty list literal evaluates to, as upstream's-- @vEmptyList@ is one value: a C static, like the @null@ cell, so it is-- the same in every arena window.emptyListThunk :: ThunkemptyListThunk = Thunk cthunkEmptyList-- | The 64 bits of an integer or float literal, split across the-- instruction's two 32-bit arguments, low word first.literalWord64 :: (MonadCData m) => Word32 -> m Word64literalWord64 bcIdx = do  lo <- cbcArg1 bcIdx  hi <- cbcArg2 bcIdx  pure (fromIntegral lo .|. (fromIntegral hi `shiftL` 32))-- | The bytes of a path literal's value, when producing it is not work:-- the literal's own in the code when they are already canonical, as they-- are for any path written canonically, and a copy of the canonical form-- otherwise.---- Parsing resolves every relative path literal against the file's directory-- ('Nix.Expr.Resolve.resolveRelativePaths'), so what reaches here is already-- absolute and only wants the lexical canonicalisation every path value-- carries.  The exception is a home-relative literal, which parsing leaves-- alone: expanding it reads @HOME@, and that is an effect this cannot run.constantPathValue :: (MonadCData m) => Word32 -> m (Maybe CStrPtr)constantPathValue bcIdx = do  code <- cbcArg1 bcIdx >>= cbcString  text <- cstrText code  let canonical = canonPathValue text      held = if canonical == text then HeldStr code else NotHeld  if T.isPrefixOf "~/" text    then pure Nothing    else Just <$> pathCell canonical held-- | Whether a list expression has no elements, and so is the same empty list-- however many times it is written.  Upstream hands back one shared-- @vEmptyList@ for this rather than a thunk; here it is 'emptyListThunk'.emptyBcList :: (MonadCData m) => Word32 -> m BoolemptyBcList bcIdx = do  (count, _) <- cbcCountedPayload bcIdx =<< cbcArg1 bcIdx  pure (count == 0)-- | The string a string expression makes when every part of it is-- literal, or 'Nothing' when one is an interpolation and producing the-- value is work.  A string with no interpolation is one literal part,-- since the lexer joins a run of characters and escapes into one chunk,-- and its value is that part's own string in the code, which the cell-- points at rather than a copy, as upstream's @ExprString@ hands out the-- value it holds (@ExprString::maybeThunk@ in eval.cc).---- Upstream draws this line in the parser: a fully literal string folds into-- an @ExprString@ carrying a ready-made value, and only a string with-- interpolation survives as @ExprConcatStrings@.  The split is made here-- instead by reading the parts back, because this compiler emits one opcode-- for both shapes.---- Deliberately not extended to @OpIndStr@, whose common indentation is-- stripped during evaluation rather than in the parser, so its value is not-- yet a constant at this point.  Upstream can treat one as a constant-- because its parser has already done that stripping.literalStr :: (MonadCData m) => Word32 -> m (Maybe CStrPtr)literalStr bcIdx = do  (count, dataOff) <- cbcCountedPayload bcIdx =<< cbcArg1 bcIdx  partsLiteral count dataOff >>= \case    False -> pure Nothing    True      | count == 1 -> Just <$> (cbcData (dataOff + 1) >>= cbcString)      | otherwise -> Just <$> (partsBytes count dataOff >>= cstrNew . BS.concat)  where    -- Whether each of the parts, the count given from a data offset on,    -- is a literal, read before any part is collected.  Each helper takes    -- the offset as a strict argument rather than closing over it, so the    -- offset is never boxed for a string that is not all literal.    partsLiteral 0 !_ = pure True    partsLiteral n !off =      cbcData off >>= \case        tag          | tag == strpartLit -> partsLiteral (n - 1 :: Int) (off + 2)          | otherwise -> pure False    -- The bytes of each literal part, the count given from a data offset    -- on.    partsBytes 0 !_ = pure []    partsBytes n !off = (:) <$> (cbcData (off + 1) >>= cbcStringBytes) <*> partsBytes (n - 1 :: Int) (off + 2)-- | Wrap an already-computed value as a thunk.-- Ints, floats and a builtin with no argument taken are inline in a new-- cell; booleans and null are the shared static cells, as upstream hands-- out one @vTrue@, @vFalse@ and @vNull@ (eval.cc at 2.24.9,-- @EvalState::getBool@).  Every other value is a new cell pointing at its-- C object, a string or a path over the bytes it holds or a copy of-- them, and a partial application over a new object holding the cells-- of its arguments.evaluated :: (MonadCData m) => NixValue -> m Thunkevaluated val =  Thunk <$> case val of    VInt n -> cthunkNewComputedInt n    VFloat d -> cthunkNewComputedFloat d    VBool b -> pure (cthunkBool b)    VNull -> pure cthunkNull    VAttrs (AttrSet cset) -> cthunkNewComputedAttrs (castPtr cset)    VPathHeld p held -> pathCell p held >>= cthunkNewComputedPath    VStrHeld t ctx held ->      stringCell t ctx held >>= \case        StrCell str -> cthunkNewComputedStr str        CtxStrCell csptr -> cthunkNewComputedCtxStr (castPtr csptr)    VList (CList clistPtr) -> cthunkNewComputedList (castPtr clistPtr)    VLambda (Env envPtr) formals bodyBcIdx -> marshalLambda envPtr formals bodyBcIdx >>= cthunkNewComputedLambda    VBuiltin builtin args ->      primopCell builtin args >>= \case        PrimopCell primop -> cthunkNewComputedPrimop primop        PrimopAppCell app -> cthunkNewComputedPrimopApp (castPtr app)-- | What a cell holding a builtin value holds.data PrimopCell  = -- | A builtin with no argument taken: its index, inline.    PrimopCell !Word32  | -- | A partial application.    PrimopAppCell !CPrimopAppPtr-- | The C form of a builtin value: its index, or a partial application-- holding the cells of the arguments it has taken, an argument taken-- forced in a new cell for its value and any other as the cell it is.primopCell :: (MonadCData m) => Builtin -> [BuiltinArg] -> m PrimopCellprimopCell builtin [] = pure (PrimopCell (builtinIndex builtin))primopCell builtin args = do  cells <- traverse argumentCell args  PrimopAppCell <$> cprimopAppNew (builtinIndex builtin) forced cells  where    argumentCell (ArgForced val) = unThunk <$> evaluated val    argumentCell (ArgDeferred (Thunk cell)) = pure cell    forced = foldl' (.|.) 0 [bit i | (i, ArgForced _) <- zip [0 ..] args]-- | The C object a cell holding a string value points at.data StringCell  = -- | A context-free string's bytes.    StrCell !CStrPtr  | -- | A string with context.    CtxStrCell !CCtxStrPtr-- | The C object for a cell holding a string: the one its bytes are held-- in, or a new one made from them.  A string with context held whole is-- reused only while it still has context, and a context-free one only-- while it is context-free, which is how a cell read gives them.stringCell :: (MonadCData m) => ByteString -> StringContext -> HeldBytes -> m StringCellstringCell bytes ctx held  | ctx == emptyContext = StrCell <$> heldOr (cstrNew bytes) held  | otherwise = case held of      HeldCtxStr csptr -> pure (CtxStrCell csptr)      _ -> CtxStrCell <$> marshalStringContext bytes ctx  where    heldOr _ (HeldStr str) = pure str    heldOr make _ = make-- | The C string for a cell holding a path: the one its bytes are held-- in, or a copy of its text's bytes.pathCell :: (MonadCData m) => Text -> HeldBytes -> m CStrPtrpathCell _ (HeldStr str) = pure strpathCell path _ = cstrFromText path-- | Marshal Env + EvalFormals + body bc_idx to a C nn_lambda_t, which-- holds each name as the symbol the formals hold.marshalLambda :: (MonadCData m) => Ptr NnEnv -> EvalFormals -> Word32 -> m (Ptr ())marshalLambda envPtr formals bodyBcIdx = case formals of  EFName (Symbol nameSym) -> do    lam <- clambdaNew envPtr bodyBcIdx FormalsName nameSym invalidSymbolId 0 0    pure (castPtr lam)  EFSet lambdaName entries allowExtra -> do    let count = fromIntegral (length entries) :: Word32        extraFlag = if allowExtra then 1 else 0 :: Word8    lam <- clambdaNew envPtr bodyBcIdx FormalsSet invalidSymbolId (lambdaNameSymbol lambdaName) extraFlag count    fillEntries lam 0 entries    pure (castPtr lam)  EFNamedSet lambdaName (Symbol nameSym) entries allowExtra -> do    let count = fromIntegral (length entries) :: Word32        extraFlag = if allowExtra then 1 else 0 :: Word8    lam <- clambdaNew envPtr bodyBcIdx FormalsNamedSet nameSym (lambdaNameSymbol lambdaName) extraFlag count    fillEntries lam 0 entries    pure (castPtr lam)  where    fillEntries _ _ [] = pure ()    fillEntries lam !idx (EvalFormal (Symbol nameSym) def : rest) = do      let (kind, code) = formalDefaultWords def      clambdaSetEntry lam idx nameSym kind code      fillEntries lam (idx + 1) rest-- | Read a C nn_lambda_t back into VLambda.-- Reconstructs Env (newtype wrap), EvalFormals (each name the symbol the-- closure holds), body bc_idx.unmarshalLambdaValue :: (MonadCData m) => Ptr () -> m NixValueunmarshalLambdaValue rawPtr = do  let lamPtr = castPtr rawPtr  envPtr <- clambdaEnv lamPtr  bodyIdx <- clambdaBody lamPtr  formalsType <- clambdaFormalsType lamPtr  formals <- case formalsType of    FormalsName -> EFName . Symbol <$> clambdaNameSym lamPtr    FormalsSet -> do      lambdaSym <- clambdaLambdaNameSym lamPtr      count <- clambdaFormalCount lamPtr      extra <- clambdaAllowExtra lamPtr      entries <- readLambdaEntries lamPtr count      pure (EFSet (lambdaNameFromSymbol lambdaSym) entries (extra /= 0))    FormalsNamedSet -> do      lambdaSym <- clambdaLambdaNameSym lamPtr      nameSym <- clambdaNameSym lamPtr      count <- clambdaFormalCount lamPtr      extra <- clambdaAllowExtra lamPtr      entries <- readLambdaEntries lamPtr count      pure (EFNamedSet (lambdaNameFromSymbol lambdaSym) (Symbol nameSym) entries (extra /= 0))    other -> cdataIO (cInvariantViolated "nn_lambda_formals_type" ("no lambda has formals type " ++ show other))  pure (VLambda (Env envPtr) formals bodyIdx)  where    -- Zero-formal set patterns ({}:, { ... }:) store count 0 and a NULL    -- entries array; count - 1 would underflow Word32.    readLambdaEntries lamPtr count      | count == 0 = pure []      | otherwise = mapM (readOneEntry lamPtr) [0 .. count - 1]    readOneEntry lamPtr idx = do      nameSym <- clambdaEntryName lamPtr idx      kind <- clambdaEntryHasDefault lamPtr idx      code <- clambdaEntryDefault lamPtr idx      case formalDefaultFromWords kind code of        Just def -> pure (EvalFormal (Symbol nameSym) def)        Nothing -> cdataIO (cInvariantViolated "nn_lambda_entry_kind" ("no formal has default kind " ++ show kind))-- | Marshal payload bytes + StringContext to a C nn_ctxstr_t, each a-- string's bytes: the payload as it is, and the StorePath and output-name-- fields as their text's UTF-8.marshalStringContext :: (MonadCData m) => ByteString -> StringContext -> m CCtxStrPtrmarshalStringContext textVal (StringContext ctxSet) = do  text <- cstrNew textVal  let elems = Set.toAscList ctxSet      count = fromIntegral (length elems) :: Word32  ptr <- cctxstrNew text count  fillElems ptr 0 elems  pure ptr  where    fillElems _ _ [] = pure ()    fillElems ptr !idx (e : es) = do      marshalElem ptr idx e      fillElems ptr (idx + 1) es    marshalElem eptr eidx (SCPlain (StorePath h n)) = do      hash <- cstrFromText h      name <- cstrFromText n      cctxstrSetPlain eptr eidx hash name    marshalElem eptr eidx (SCDrvOutput (StorePath h n) out) = do      hash <- cstrFromText h      name <- cstrFromText n      output <- cstrFromText out      cctxstrSetDrvOutput eptr eidx hash name output    marshalElem eptr eidx (SCAllOutputs (StorePath h n)) = do      hash <- cstrFromText h      name <- cstrFromText n      cctxstrSetAllOutputs eptr eidx hash name-- | Unmarshal a C nn_ctxstr_t back to (payload bytes, StringContext).unmarshalStringContext :: (MonadCData m) => CCtxStrPtr -> m (ByteString, StringContext)unmarshalStringContext ptr = do  textVal <- cctxstrText ptr >>= cstrBytes  count <- cctxstrCtxCount ptr  -- count - 1 underflows Word32 at 0 (defensive: marshal sites store  -- only non-empty contexts).  elems <-    if count == 0      then pure []      else mapM (readElem ptr) [0 .. count - 1]  pure (textVal, StringContext (Set.fromList elems))  where    readElem cptr idx =      cctxstrElemTag cptr idx >>= \case        ScePlain -> SCPlain <$> storePathAt cptr idx        SceDrvOutput -> SCDrvOutput <$> storePathAt cptr idx <*> (cctxstrElemOutput cptr idx >>= cstrText)        SceAllOutputs -> SCAllOutputs <$> storePathAt cptr idx        other -> cdataIO (cInvariantViolated "nn_ctxstr_elem_tag" ("no string context element has tag " ++ show other))    -- Raw construction (Nix.Store.Path.Internal): provenance-safe -    -- these strings were made from a validated StorePath's own fields    -- by marshalStringContext, so this is a round-trip, not a parse    -- boundary.    storePathAt cptr idx =      StorePath        <$> (cctxstrElemHash cptr idx >>= cstrText)        <*> (cctxstrElemName cptr idx >>= cstrText)-- | Tie a frame whose slots hold thunks that may point back at it: the-- env of @slotCount@ empty slots under the parent, at the site given, is-- allocated first, the thunks are made against it, and the slots are-- filled last, as upstream allocates @env2@ before it fills the values-- (@ExprLet::eval@, @ExprAttrs::eval@ and @EvalState::callFunction@,-- eval.cc at 2.24.9).-- @mkThunks@ answers one thunk per slot, in slot order, and must not-- read the frame's own slots, which are not all filled until this-- returns ('mkThunkBc' does not; 'envLookupResolved' refuses an unfilled-- one).  Nothing is evaluated in the frame before then, which a with's-- env relies on when it copies the slots of the env it is made in-- ('pushLazyWithScope').  Answers the env and those thunks.tieFrame :: (MonadCData m) => EnvSite -> Maybe Env -> Int -> (Env -> m [Thunk]) -> m (Env, [Thunk])tieFrame site parent slotCount mkThunks = do  env <- newBlankEnv site parent slotCount  thunks <- mkThunks env  fillSlots env (take slotCount thunks)  pure (env, thunks)-- | Give a slot of a frame 'tieFrame' has filled, and nothing has been-- evaluated in, the cell another slot holds: the first slot's binding is-- a variable naming the second, whose cell it shares.  Both slots are read-- from bytecode, so one past the frame's slots, which only corrupt-- bytecode holds, is handed to @pastFrame@ and nothing is written.shareSlot :: (MonadCData m) => (Word32 -> m ()) -> Env -> Word32 -> Word32 -> m ()shareSlot pastFrame (Env envPtr) slot target = cenvSlotCount envPtr >>= share  where    share slotCount      | slot >= slotCount = pastFrame slot      | target >= slotCount = pastFrame target      | otherwise = do          slots <- cenvSlots envPtr          cdataIO (peekElemOff slots (fromIntegral target) >>= pokeElemOff slots (fromIntegral slot))-- | The cells in an env's slots, the count given from slot 0.envSlotCells :: (MonadCData m) => Env -> Int -> m [Thunk]envSlotCells (Env envPtr) count = do  slots <- cenvSlots envPtr  cdataIO (map Thunk <$> peekArray count slots)-- | Write thunks into an env's slots, first to last from slot 0.  There-- are no more thunks than slots.fillSlots :: (MonadCData m) => Env -> [Thunk] -> m ()fillSlots (Env envPtr) thunks = do  slots <- cenvSlots envPtr  cdataIO (zipWithM_ (\i thunk -> pokeElemOff slots i (unThunk thunk)) [0 ..] thunks)-- | A value's type as an error message names it: upstream's @showType@ on-- a value (eval.cc at 2.24.9, the same in 2.33.2), which every type error-- there uses.  It names a primop and the string context the type alone-- does not show.  Upstream's other cases (a thunk, a black hole, a-- function application, an external value) have no 'NixValue': a value-- here is always forced, and there are no plugins.typeName :: NixValue -> TexttypeName val = case val of  VInt _ -> "an integer"  VFloat _ -> "a float"  VBool _ -> "a Boolean"  VNull -> "null"  VStr _ ctx    | ctx == emptyContext -> "a string"    | otherwise -> "a string with context"  VPath _ -> "a path"  VList _ -> "a list"  VAttrs _ -> "a set"  VLambda {} -> "a function"  VBuiltin builtin [] -> "the built-in function '" <> builtinName builtin <> "'"  VBuiltin builtin _ -> "the partially applied built-in function '" <> builtinName builtin <> "'"-- | @builtins.typeOf@: the name of upstream's @ValueType@, the-- classification its type checks compare (a lambda and a primop are both-- @nFunction@).typeOfValue :: NixValue -> TexttypeOfValue val = case val of  VInt _ -> "int"  VFloat _ -> "float"  VBool _ -> "bool"  VNull -> "null"  VStr _ _ -> "string"  VPath _ -> "path"  VList _ -> "list"  VAttrs _ -> "set"  VLambda {} -> "lambda"  VBuiltin _ _ -> "lambda"-- ----------------------------------------------------------------------------- Evaluation monad-- ----------------------------------------------------------------------------- | Upstream's @warn@ from its own code rather than an expression, which-- its logger prefixes @warning:@ (@Logger::warn@ in logging.cc).warnMessage :: (MonadEval m) => Text -> m ()warnMessage = traceMessage . ("warning: " <>)-- | What @builtins.pathExists@ asks of a path, decided from the-- argument's spelling as upstream's @prim_pathExists@ decides it-- (primops.cc at 2.24.9).data PathExistence  = -- | Whether an entry is there: the ancestors are resolved and the    -- final component is checked in place, so a symlink exists whatever    -- it points at (upstream's @SymlinkResolution::Ancestors@).    ExistsAsEntry  | -- | Whether a directory is there, asked by a string ending in @/@ or    -- @/.@: every symlink is followed, the final one included    -- (@SymlinkResolution::Full@), and a file answers false.    ExistsAsDirectory  deriving (Eq, Show)-- | Effect class for Nix evaluation.  Core logic is polymorphic in @m@-- so the same evaluator composes into t'PureEval' for tests or @IO@ for-- real file-system access (e.g. @import@, @readFile@).  Every evaluator-- runs the C data layer's actions in sequence with its own-- ('MonadCData').class (MonadCData m) => MonadEval m where  -- | Raise an evaluation error (type error, missing attribute, IO  -- failure).  NOT caught by @builtins.tryEval@: upstream Nix catches only  -- ThrownError\/AssertionError there, and every other EvalError escapes.  throwEvalError :: Text -> m a  -- | Raise a catchable error - @builtins.throw@, a failed @assert@, and a  -- search path lookup that matches no entry.  These are the only errors  -- 'catchEvalError' (tryEval) recovers from.  throwCatchableError :: Text -> m a  -- | Abort evaluation (uncatchable by tryEval, matching real Nix).  abortEvaluation :: Text -> m a  -- | tryEval semantics: recover from a 'throwCatchableError'  -- (throw, assert, a search path miss); eval errors and aborts propagate.  catchEvalError :: m a -> m (Either Text a)  -- | Run the action; when it fails with ANY evaluation failure  -- (catchable throw, eval error, or abort), run the cleanup and let  -- the failure propagate unchanged.  The error half of a bracket -  -- success runs no cleanup - for builtins that hold external state  -- (a scratch directory) across throwing calls.  'catchEvalError'  -- cannot express this: it recovers only catchable throws, and a  -- failed fetch is an eval error.  onEvalError :: m a -> m () -> m a  -- | @addEvaluatedTrace onFailedLine line action@ runs the action; a  -- failure escaping it (a throw, an eval error or an abort) runs @line@  -- for a line of context and gains that line outside the lines it  -- already carries, as upstream's @Error::addTrace@ adds one where a  -- @catch@ rethrows.  Nothing else about the failure changes: its message  -- stays the same, and a throw stays catchable by @builtins.tryEval@.  The  -- line is made only on failure.  When making it fails, the action's  -- failure still escapes, and gains @onFailedLine@ of that failure's  -- message as its line instead, as upstream rethrows the error it caught  -- whatever evaluating its context does (@prim_addErrorContext@ in  -- primops.cc at 2.36.0).  addEvaluatedTrace :: (Text -> Text) -> m Text -> m a -> m a  -- | Run an application one call frame deeper, or refuse it with  -- upstream's "stack overflow; max-call-depth exceeded" when the frames  -- already active exceed the ceiling ('Nix.Eval.CallDepth.enterCallFrame'  -- is the rule).  An eval error, not a throw: upstream raises an  -- EvalError, which its tryEval does not catch.  The frame ends with the  -- action, on failure too, so an error unwinding through a tryEval  -- leaves the count that tryEval was entered at.  withCallFrame :: m a -> m a  -- | Whether something is at the path, as @builtins.pathExists@ asks  -- it; the query says how the final component is read.  A path the  -- policy refuses is absent.  doesPathExist :: PathExistence -> Text -> m Bool  -- | List a directory, returning @(name, fileType)@ pairs.  -- @fileType@ is one of @"regular"@, @"directory"@, @"symlink"@ or  -- @"unknown"@ (matching Nix's @builtins.readDir@ semantics), and  -- 'Nothing' for an entry whose lstat is refused with @EACCES@ or  -- @EPERM@, which upstream's @readDirectory@ gives no type  -- (posix-source-accessor.cc at 2.24.9).  listDirectory :: Text -> m [(Text, Maybe Text)]  importFile :: Text -> m NixValue  -- | Look up an environment variable by the bytes of its name, answering  -- the bytes of its value, or @""@ if unset.  getEnvVar :: ByteString -> m ByteString  -- | Get the current epoch time (seconds since 1970-01-01).  getCurrentTime :: m Int64  -- | Write a named file to the store at upstream's text-path  -- (@text:\<refs\>@ scheme, flat sha256 of the contents), returning the  -- canonical store path.  The contents are raw bytes (a Nix string's  -- payload) - hashed and stored exactly as given.  The refs are the  -- contents' plain store-path references; they participate in the path  -- computation exactly as upstream's addTextToStore.  writeToStore :: Text -> ByteString -> [StorePath] -> m Text  -- | Import a file with a set's names bound over the builtins, each to  -- its value in the set.  scopedImportFile :: AttrSet -> Text -> m NixValue  -- | Read raw bytes from a file.  Used by @builtins.hashFile@.  readFileBytes :: Text -> m ByteString  -- | Classify a single filesystem path as @"regular"@, @"directory"@,  -- @"symlink"@, or @"unknown"@: an lstat of the path itself, refused  -- when a symlink sits above it beneath the accessor's root (for a path  -- in the store, beneath the store directory), as upstream's accessor  -- refuses one.  Used by @builtins.readFileType@ and, on a resolved  -- root, by the source tree walk.  getFileType :: Text -> m Text  -- | Follow every symlink in a path, as upstream's  -- @SourcePath::resolveSymlinks@ does before a tree is read  -- (@addPath@, @prim_filterSource@): no component of the result is a  -- symlink, so the lstats of the walk that follows never meet one  -- above the entry they classify.  resolveSymlinks :: Text -> m Text  -- | Run an external process: @(command, args, stdin) -> (exitCode, stdout, stderr)@.  runProcess :: Text -> [Text] -> Text -> m (Int, Text, Text)  -- | Create a fetcher scratch directory under the system temp dir: the  -- given name prefix plus an unpredictable suffix, exclusively created.  -- A fixed scratch name in the shared temp dir gives any other local  -- process a pre-place\/swap window between creation and the store  -- copy, so the name must be unguessable and creation must fail rather  -- than adopt an existing directory.  createScratchDir :: Text -> m Text  -- | Recursively remove a directory 'createScratchDir' created.  removeScratchDir :: Text -> m ()  -- | Resolve a path literal to a canonical path.  -- In IO evaluation, @~/@ resolves against the home directory and other  -- relative paths resolve against the current file's directory  -- (@esBaseDir@); this ensures that path values captured in closures  -- remain valid after the import scope ends.  In pure evaluation the  -- text is not absolutized.  Every implementation ends with lexical  -- canonicalization ('Nix.Eval.CanonPath.canonPath'), as upstream: no  -- dot segment or repeated separator survives into a path value.  resolvePathLiteral :: Text -> m Text  -- | Copy a path (file or directory) to the store, returning the store  -- path.  Content-addressed like upstream addToStore: recursive NAR  -- sha256 under the given name.  Arguments: how the copy came to be (a  -- fetcher's, or @builtins.path@'s), source path, store name, and an  -- optional sha256 pin as (expected digest, refusal): a copy whose  -- digest is another fails before anything is written, with the refusal  -- of the digest it has.  @builtins.path@ and @builtins.fetchTarball@  -- share this pin.  copyPathToStore :: PendingOrigin -> Text -> Text -> Maybe (ByteString, ByteString -> Text) -> m Text  -- | Recursive NAR sha256 digest of a path, without copying it anywhere.  -- Used by @builtins.fetchGit@ to report @narHash@.  narHashOfPath :: Text -> m ByteString  -- | Whether a regular file carries the executable bit (NAR encodes it).  isExecutableFile :: Text -> m Bool  -- | Mark a regular file executable (mode bit on Unix, an alternate data  -- stream on Windows). Run before the tree is copied to the store.  setExecutableFile :: Text -> m ()  -- | Look up a previously recorded fetch by a key naming exactly what was  -- fetched. 'Nothing' when nothing readable was recorded.  A record is  -- answered whether or not what it names is still on disk, which the  -- caller asks with 'adoptStorePath'.  lookupFetchCache :: Text -> m (Maybe Text)  -- | Take over a store path an earlier evaluation wrote: 'True' when it is  -- still there, on disk or in a store that takes writes as they are made,  -- and recorded as this evaluation's own store write in that case.  -- Reusing a path instead of rewriting it must still leave the  -- registration behind, or the build driver never registers it and a  -- derivation naming it fails with \"path \'...\' is not valid\".  adoptStorePath :: Text -> m Bool  -- | Whether the session's store already holds the path a pinned fetch  -- names, admitting it as a fetch result is admitted when it does:  -- upstream's early exit, which then neither downloads nor writes  -- (@fetch@ in fetchTree.cc at 2.24.9).  pinnedFetchHeld :: StorePath -> m Bool  -- | Record a fetch under that key. Failing to record is not an error -  -- the cache is an optimisation.  writeFetchCache :: Text -> Text -> m ()  -- | Read a symlink's target WITHOUT following it.  readSymlinkTarget :: Text -> m Text  -- | Unpack a serialized NAR into the store under the given name at its  -- canonical recursive fixed-output path (sha256 of the bytes),  -- returning the store path text.  Used by @builtins.path@ and  -- @builtins.filterSource@ with a filter, whose filtered tree exists  -- only as a NAR value and never on the source filesystem.  addSourceNar :: Text -> ByteString -> m Text  -- | Write raw bytes to the store at a canonical flat fixed-output path  -- (@makeFixedOutputPath name "sha256" FlatIngestion@), so a hash-pinned  -- fetch lands at the same store path C++ Nix computes - reproducible and  -- cacheable.  The origin says whether a fetcher or @builtins.path@  -- wrote them.  addFixedOutputFile :: PendingOrigin -> Text -> ByteString -> m Text  -- | A URL's body, whole: a @file:\/\/@ URL's file as it is on disk  -- ('Nix.Eval.Policy.fileUrlPath'), any other URL through the transfer  -- layer every download goes through ("Nix.Http"), a transient failure  -- retried as the session's policy says, or why it could not be read.  fetchUrlBytes :: Text -> m (Either Text ByteString)  -- | The archive a URL serves, unpacked as upstream's tarball fetcher  -- unpacks one and added to the store under the name, as its store  -- path.  The tree's NAR is held to the pin, when there is one, before  -- anything is written; one that differs fails with the message the  -- given function makes of the digest it has.  fetchArchiveToStore :: Text -> Text -> Maybe (ByteString, ByteString -> Text) -> m (Either FetchFailure Text)  -- | Report a trace/warning message.  -- IO evaluators tell their session's logger; pure evaluators silently  -- discard.  traceMessage :: Text -> m ()  -- | Force a thunk to a value, with memoization.  -- IO evaluators store the value in the thunk's C cell; pure  -- evaluators re-evaluate each time.  -- The first argument runs what a pending thunk holds ('readPending'),  -- passed in to break the Eval.Types to Eval circular dependency.  forceThunk :: (Pending -> m NixValue) -> Thunk -> m NixValue  -- | Look up a cached derivation modulo-hash (hex) by its @.drv@ store  -- path.  Populated bottom-up as each derivation is computed; used by the  -- derivation-hash algorithm to substitute input derivations.  Pure  -- evaluators have no cache and always return 'Nothing'.  lookupDrvHash :: Text -> m (Maybe Text)  -- | Cache a derivation's modulo hash (hex) under its @.drv@ store path.  -- A no-op in pure evaluators (no memoization).  cacheDrvHash :: Text -> Text -> m ()  -- | Record a derivation's serialized @.drv@ ATerm (the exact bytes whose  -- hash is its store path) under its @.drv@ store path, as each derivation  -- is computed (bottom-up).  Unlike 'cacheDrvHash' (which stores only the  -- modulo hash), this retains the full ATerm so the build driver can  -- materialize the entire input-@.drv@ closure to the store before a  -- dependency-aware build.  A no-op in pure evaluators.  recordDrvAterm :: Text -> ByteString -> m ()  -- | Read and parse a derivation from its @.drv@ in the store, or 'Nothing'  -- if it is absent or unreadable.  Used to resolve an input derivation's  -- modulo hash on a cache miss - a cross-session reference, or a path  -- fabricated by @builtins.appendContext@ - where the referenced derivation  -- was not evaluated this session.  Reading the store is an effect, so this  -- is an IO-evaluator capability: pure evaluators return 'Nothing', which  -- makes hashing a dependent derivation something only the IO evaluator can do.  readStoreDerivation :: StorePath -> m (Maybe Derivation)  -- | Look up a derivation evaluated earlier THIS session by its @.drv@ store  -- path, or 'Nothing' if it was not.  The IO evaluator reads the ATerm it  -- recorded bottom-up ('recordDrvAterm'); pure evaluators return 'Nothing'.  -- Used to recover a referenced derivation's output names for an all-outputs  -- (DrvDeep) context reference without a disk read - the in-session @.drv@ is  -- not on disk during evaluation.  lookupSessionDrv :: Text -> m (Maybe Derivation)  -- | The references recorded when this session wrote a store path (a  -- @builtins.toFile@'s text with context, a copied source's none), or  -- 'Nothing' if this session did not write it.  The walk of a  -- @drvPath@'s closure follows them as upstream's @computeFSClosure@  -- follows its database's.  Pure evaluators write nothing.  lookupSessionReferences :: StorePath -> m (Maybe [StorePath])  -- | Realise a string's context before a path reader reads the path it  -- spells, as upstream's @EvalState::realiseContext@ does: every path it  -- names found valid or made so, and every output it names built or  -- substituted, or why that cannot be ("Nix.Eval.RealiseContext").  Pure  -- evaluators have no store and realise nothing.  realiseContext :: StringContext -> m (Either ContextRefusal ())  -- | Compute the store path a source file/directory gets when copied into  -- the store (recursive NAR sha256 to a fixed-output path named by the  -- path's base name), WITHOUT performing the copy.  Used when a path  -- literal is coerced in a derivation argument or environment value.  Pure  -- evaluation has no store, so a path there answers itself.  storeSourcePath :: Text -> m Text  -- | The policy this evaluation runs under.  The builtins whose behaviour  -- upstream changes by setting consult it: @storePath@ is refused and the  -- fetchers demand a hash or revision under @pure-eval@, a search path  -- miss is worded for the mode, and @traceVerbose@ traces only under  -- @trace-verbose@.  The filesystem and URI gates are not decided here;  -- they live in the instance, where the allowed set is.  evalPolicy :: m EvalPolicy  -- | Upstream @checkURI@: refuse a fetch of this URI under @restrict-eval@  -- unless @allowed-uris@ admits it (a path or @file://@ URI is checked  -- against the allowed paths instead).  A no-op under any other policy.  -- Every fetcher calls it before anything is spawned or downloaded.  checkUri :: Text -> m ()  -- | A path named on the drive this evaluation reads it on  -- ('Nix.Store.Path.qualifyPathText' against its store directory): on  -- Windows a rooted path with no drive gains the store's.  For a  -- fetcher handing a local path to another program, which would  -- otherwise read it on whatever drive that program has current.  qualifyPath :: Text -> m Text-- | Unwrap the result of a construction that makes a store path (the-- @makeStorePath@ family in "Nix.Hash", and the @.drv@ files of-- "Nix.Derivation"), raising a refused name as upstream's @BadStorePath@-- text.  The message names the path and not the builtin, as upstream's-- does: upstream names the builtin in its trace.  Every eval-side path-- construction goes through this, so a refused name is an eval error-- before anything is written.storePathOrThrow :: (MonadEval m) => Either BadStorePath a -> m astorePathOrThrow = either (throwEvalError . badStorePathText) pure-- | 'addEvaluatedTrace' with a line that is already made, which cannot-- fail.addErrorTrace :: (MonadEval m) => Text -> m a -> m aaddErrorTrace line = addEvaluatedTrace (const line) (pure line)-- | Error raised during pure evaluation.  'PThrow' (@builtins.throw@, a-- failed @assert@) is the only kind @tryEval@ catches; 'PError' (type-- errors, missing attributes) and 'PAbort' (@abort@, infinite recursion)-- escape it, exactly as in C++ Nix.data PureError = PThrow !Text | PError !Text | PAbort !Text-- | Pure evaluation monad: given the call depth, a t'CData' action-- answering @Either PureError@, so its only effect is the C data layer.-- IO builtins (@builtins.readFile@, @import@) are unavailable, and-- everything else evaluates identically to the IO version, the-- @max-call-depth@ ceiling included, at its upstream default.  It carries-- no filesystem policy: there is no filesystem, environment or network-- behind it to gate, so it reports 'unrestrictedPolicy' and every URI-- check passes.  It writes no cell it did not allocate but a builtin's-- static cell, which it fills with the one value that cell ever holds-- ('Nix.Eval.CThunk.cthunkPrimop'): a force reads a thunk and evaluates-- it without memoizing, so it marks no blackhole either.newtype PureEval a = PureEval (CallDepth -> CData (Either PureError a))instance Functor PureEval where  fmap f (PureEval run) = PureEval (fmap (fmap f) . run)instance Applicative PureEval where  pure a = PureEval (\_ -> pure (Right a))  PureEval runF <*> PureEval runA = PureEval $ \depth ->    runF depth >>= either (pure . Left) (\f -> fmap f <$> runA depth)instance Monad PureEval where  PureEval run >>= k = PureEval $ \depth ->    run depth >>= either (pure . Left) (\a -> runPure (k a) depth)instance MonadCData PureEval where  liftCData act = PureEval (\_ -> Right <$> act)-- | A pure evaluation run at a call depth.runPure :: PureEval a -> CallDepth -> CData (Either PureError a)runPure (PureEval run) = run-- | Fail a pure evaluation.pureFailure :: PureError -> PureEval apureFailure failure = PureEval (\_ -> pure (Left failure))-- | Run a pure evaluation, flattening the internal error into 'Text': a-- failure's message, as t'Nix.Eval.IO.EvalIO' reports it.  The run is a-- value ('runCDataPure'): the evaluation writes only cells it allocates,-- the with-scope entries it resolves ("Nix.Eval.CData" says what two-- runs resolving one entry can show) and the builtins' static cells it-- binds.runPureEval :: PureEval a -> Either Text arunPureEval action =  case runCDataPure (runPure action (topLevelCallDepth defaultMaxCallDepth)) of    Right a -> Right a    Left (PThrow t) -> Left t    Left (PError t) -> Left t    Left (PAbort t) -> Left tinstance MonadEval PureEval where  throwEvalError = pureFailure . PError  throwCatchableError = pureFailure . PThrow  abortEvaluation = pureFailure . PAbort  -- Catch only a throw/assert (tryEval sees the error); eval errors,  -- aborts, and infinite recursion propagate, matching EvalIO and C++ Nix.  catchEvalError (PureEval action) = PureEval (fmap caught . action)    where      caught (Left (PThrow t)) = Right (Left t)      caught (Left other) = Left other      caught (Right a) = Right (Right a)  -- Pure evaluation has no external state, so there is nothing to  -- clean up; the failure passes through unchanged.  onEvalError (PureEval action) _ = PureEval action  -- 'runPureEval' reports a failure's message alone, so a line of context  -- has nowhere to go.  The line is still made, since a failure making it  -- replaces the action's.  -- A pure failure carries no trace: the line is made, as upstream makes  -- it, and the action's failure escapes whatever making it does.  addEvaluatedTrace _ (PureEval line) (PureEval action) =    PureEval $ \depth ->      action depth >>= \case        Left failure -> Left failure <$ line depth        Right a -> pure (Right a)  -- The reader scopes the frame: the action runs one deeper, and whatever  -- follows it sees the depth it started from, failure or not.  withCallFrame (PureEval action) =    PureEval $ \depth -> either (pure . Left . PError) action (enterCallFrame depth)  doesPathExist _ _ = pure False  listDirectory _ = throwEvalError "builtins.readDir: not available in pure evaluation"  importFile _ = throwEvalError "import: not available in pure evaluation"  getEnvVar _ = pure ""  getCurrentTime = pure 0  writeToStore _ _ _ = throwEvalError "toFile: not available in pure evaluation"  scopedImportFile _ _ = throwEvalError "scopedImport: not available in pure evaluation"  readFileBytes _ = throwEvalError "readFile: not available in pure evaluation"  getFileType _ = throwEvalError "readFileType: not available in pure evaluation"  -- No filesystem, so no symlinks: a path resolves to itself.  resolveSymlinks = pure  runProcess _ _ _ = throwEvalError "runProcess: not available in pure evaluation"  createScratchDir _ = throwEvalError "createScratchDir: not available in pure evaluation"  removeScratchDir _ = pure ()  copyPathToStore _ _ _ _ = throwEvalError "builtins.path: not available in pure evaluation"  narHashOfPath _ = throwEvalError "builtins.fetchGit: not available in pure evaluation"  isExecutableFile _ = throwEvalError "builtins.path: not available in pure evaluation"  setExecutableFile _ = throwEvalError "builtins.fetchGit: not available in pure evaluation"  lookupFetchCache _ = pure Nothing  adoptStorePath _ = pure False  pinnedFetchHeld _ = pure False  writeFetchCache _ _ = pure ()  readSymlinkTarget _ = throwEvalError "builtins.path: not available in pure evaluation"  addSourceNar _ _ = throwEvalError "builtins.path: not available in pure evaluation"  addFixedOutputFile _ _ _ = throwEvalError "builtins.fetchurl: not available in pure evaluation"  fetchUrlBytes _ = throwEvalError "builtins.fetchurl: not available in pure evaluation"  fetchArchiveToStore _ _ _ = throwEvalError "builtins.fetchTarball: not available in pure evaluation"  traceMessage _ = pure ()  lookupDrvHash _ = pure Nothing  cacheDrvHash _ _ = pure ()  recordDrvAterm _ _ = pure ()  -- Pure eval cannot read the store, so a dependent derivation's input hash  -- is never recoverable here; the caller turns this into a loud error rather  -- than a guessed hash.  readStoreDerivation _ = pure Nothing  -- Pure eval keeps no session drv closure, so an all-outputs reference's  -- output names are never recoverable here either.  lookupSessionDrv _ = pure Nothing  lookupSessionReferences _ = pure Nothing  realiseContext _ = pure (Right ())  -- Pure eval cannot read files: a path coerces to itself (no store copy);  -- the real copy-to-store happens only under 'EvalIO'.  storeSourcePath = pure  evalPolicy = pure unrestrictedPolicy  checkUri _ = pure ()  -- No filesystem, so no drive to read on: a path names itself.  qualifyPath = pure  resolvePathLiteral = pure . canonPathValue  -- Reads the cell and never writes it: no memoization, and no  -- blackhole, so PureEval may force the same thunk again.  forceThunk evalFn (Thunk ptr) =    cthunkState ptr >>= \case      ThunkComputed -> readComputed ptr      -- Non-catchable: must escape tryEval like in C++ Nix      ThunkBlackhole -> abortEvaluation "infinite recursion encountered"      _ {- PENDING -} -> readPending ptr >>= evalFn