@@ -145,18 +145,19 @@ builtinsList = sequence
145145 version <- toValue (5 :: Int )
146146 pure $ Builtin Normal (" langVersion" , version)
147147
148- , add0 Normal " nixPath" nixPath
149148 , add TopLevel " abort" throw_ -- for now
150149 , add2 Normal " add" add_
151150 , add2 Normal " addErrorContext" addErrorContext
152151 , add2 Normal " all" all_
153152 , add2 Normal " any" any_
153+ , add2 Normal " appendContext" appendContext
154154 , add Normal " attrNames" attrNames
155155 , add Normal " attrValues" attrValues
156156 , add TopLevel " baseNameOf" baseNameOf
157157 , add2 Normal " bitAnd" bitAnd
158158 , add2 Normal " bitOr" bitOr
159159 , add2 Normal " bitXor" bitXor
160+ , add0 Normal " builtins" builtinsBuiltin
160161 , add2 Normal " catAttrs" catAttrs
161162 , add2 Normal " compareVersions" compareVersions_
162163 , add Normal " concatLists" concatLists
@@ -205,15 +206,21 @@ builtinsList = sequence
205206 , add2 Normal " elemAt" elemAt_
206207 , add Normal " exec" exec_
207208 , add0 Normal " false" (pure $ nvConstant $ NBool False )
209+ -- , add Normal "fetchGit" fetchGit
210+ -- , add Normal "fetchMercurial" fetchMercurial
208211 , add Normal " fetchTarball" fetchTarball
209212 , add Normal " fetchurl" fetchurl
210213 , add2 Normal " filter" filter_
214+ -- , add Normal "filterSource" filterSource
215+ , add2 Normal " findFile" findFile_
211216 , add3 Normal " foldl'" foldl'_
212217 , add Normal " fromJSON" fromJSON
218+ -- , add Normal "fromTOML" fromTOML
213219 , add Normal " functionArgs" functionArgs
214- , add2 Normal " genList" genList
215220 , add Normal " genericClosure" genericClosure
221+ , add2 Normal " genList" genList
216222 , add2 Normal " getAttr" getAttr
223+ , add Normal " getContext" getContext
217224 , add Normal " getEnv" getEnv_
218225 , add2 Normal " hasAttr" hasAttr
219226 , add Normal " hasContext" hasContext
@@ -236,14 +243,15 @@ builtinsList = sequence
236243 , add2 TopLevel " mapAttrs" mapAttrs_
237244 , add2 Normal " match" match_
238245 , add2 Normal " mul" mul_
246+ , add0 Normal " nixPath" nixPath
239247 , add0 Normal " null" (pure $ nvConstant NNull )
240248 , add Normal " parseDrvName" parseDrvName
241249 , add2 Normal " partition" partition_
250+ -- , add Normal "path" path
242251 , add Normal " pathExists" pathExists_
243252 , add TopLevel " placeholder" placeHolder
244253 , add Normal " readDir" readDir_
245254 , add Normal " readFile" readFile_
246- , add2 Normal " findFile" findFile_
247255 , add2 TopLevel " removeAttrs" removeAttrs
248256 , add3 Normal " replaceStrings" replaceStrings
249257 , add2 TopLevel " scopedImport" scopedImport
@@ -252,26 +260,25 @@ builtinsList = sequence
252260 , add2 Normal " split" split_
253261 , add Normal " splitVersion" splitVersion_
254262 , add0 Normal " storeDir" (pure $ nvStr $ principledMakeNixStringWithoutContext " /nix/store" )
263+ -- , add Normal "storePath" storePath
255264 , add' Normal " stringLength" (arity1 $ Text. length . principledStringIgnoreContext)
256265 , add' Normal " sub" (arity2 ((-) @ Integer ))
257- , add' Normal " substring" ( substring @ e @ t @ f @ m )
266+ , add' Normal " substring" substring
258267 , add Normal " tail" tail_
259- , add0 Normal " true" (pure $ nvConstant $ NBool True )
260268 , add TopLevel " throw" throw_
261- , add Normal " toJSON" prim_toJSON
262269 , add2 Normal " toFile" toFile
270+ , add Normal " toJSON" prim_toJSON
263271 , add Normal " toPath" toPath
264272 , add TopLevel " toString" toString
265273 , add Normal " toXML" toXML_
266274 , add2 TopLevel " trace" trace_
275+ , add0 Normal " true" (pure $ nvConstant $ NBool True )
267276 , add Normal " tryEval" tryEval
268277 , add Normal " typeOf" typeOf
278+ -- , add0 Normal "unsafeDiscardOutputDependency" unsafeDiscardOutputDependency
279+ , add Normal " unsafeDiscardStringContext" unsafeDiscardStringContext
280+ , add2 Normal " unsafeGetAttrPos" unsafeGetAttrPos
269281 , add Normal " valueSize" getRecursiveSize
270- , add Normal " getContext" getContext
271- , add2 Normal " appendContext" appendContext
272-
273- , add2 Normal " unsafeGetAttrPos" unsafeGetAttrPos
274- , add Normal " unsafeDiscardStringContext" unsafeDiscardStringContext
275282 ]
276283 where
277284 wrap :: BuiltinType -> Text -> v -> Builtin v
@@ -652,13 +659,13 @@ splitMatches numDropped (((_, (start, len)) : captures) : mts) haystack =
652659thunkStr s = nvStr (hackyMakeNixStringWithoutContext (decodeUtf8 s))
653660
654661substring :: forall e t f m . MonadNix e t f m => Int -> Int -> NixString -> Prim m NixString
655- substring start len str = Prim $ if start < 0 -- NOTE: negative values of 'len' are OK
656- then
657- throwError
658- $ ErrorCall
659- $ " builtins.substring: negative start position: "
660- ++ show start
661- else pure $ principledModifyNixContents ( Text. take len . Text. drop start) str
662+ substring start len str = Prim $
663+ if start < 0
664+ then throwError $ ErrorCall $ " builtins.substring: negative start position: " ++ show start
665+ else pure $ principledModifyNixContents ( take . Text. drop start) str
666+ where
667+ -- NOTE: negative values of 'len' are OK, and mean "take everything"
668+ take = if len < 0 then id else Text. take len
662669
663670attrNames
664671 :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
@@ -766,6 +773,12 @@ bitXor
766773bitXor x y = fromValue @ Integer x
767774 >>= \ a -> fromValue @ Integer y >>= \ b -> toValue (a `xor` b)
768775
776+ builtinsBuiltin
777+ :: forall e t f m
778+ . MonadNix e t f m
779+ => m (NValue t f m )
780+ builtinsBuiltin = (throwError $ ErrorCall " HNix does not provide builtins.builtins at the moment. Using builtins directly should be preferred" )
781+
769782dirOf :: MonadNix e t f m => NValue t f m -> m (NValue t f m )
770783dirOf x = demand x $ \ case
771784 NVStr ns -> pure $ nvStr
@@ -1040,10 +1053,6 @@ isList
10401053 :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
10411054isList = hasKind @ [NValue t f m ]
10421055
1043- isString
1044- :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
1045- isString = hasKind @ NixString
1046-
10471056isInt
10481057 :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
10491058isInt = hasKind @ Int
@@ -1060,6 +1069,12 @@ isNull
10601069 :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
10611070isNull = hasKind @ ()
10621071
1072+ -- isString cannot use `hasKind` because it coerces derivations to strings.
1073+ isString :: MonadNix e t f m => NValue t f m -> m (NValue t f m )
1074+ isString v = demand v $ \ case
1075+ NVStr {} -> toValue True
1076+ _ -> toValue False
1077+
10631078isFunction :: MonadNix e t f m => NValue t f m -> m (NValue t f m )
10641079isFunction func = demand func $ \ case
10651080 NVClosure {} -> toValue True
0 commit comments