2018-05-09 01:40:56 +02:00
|
|
|
|
{-# LANGUAGE CPP #-}
|
2018-04-16 08:01:22 +02:00
|
|
|
|
{-# LANGUAGE AllowAmbiguousTypes #-}
|
2018-04-07 21:02:50 +02:00
|
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
|
{-# LANGUAGE FunctionalDependencies #-}
|
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
|
{-# LANGUAGE MultiWayIf #-}
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-05-09 01:40:56 +02:00
|
|
|
|
{-# LANGUAGE PackageImports #-}
|
2018-04-16 08:01:22 +02:00
|
|
|
|
{-# LANGUAGE PartialTypeSignatures #-}
|
2018-05-10 22:51:14 +02:00
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2018-04-07 21:02:50 +02:00
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2018-04-08 00:34:54 +02:00
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2018-04-17 06:47:45 +02:00
|
|
|
|
{-# LANGUAGE TupleSections #-}
|
2018-04-07 21:02:50 +02:00
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
|
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -Wno-missing-signatures #-}
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
|
|
|
|
|
2019-03-16 01:20:10 +01:00
|
|
|
|
module Nix.Builtins (MonadBuiltins, withNixContext, builtins) where
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
import Control.Monad
|
2018-04-07 23:33:15 +02:00
|
|
|
|
import Control.Monad.Catch
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Control.Monad.ListM (sortByM)
|
2018-05-03 06:32:00 +02:00
|
|
|
|
import Control.Monad.Reader (asks)
|
2018-05-09 01:40:56 +02:00
|
|
|
|
|
|
|
|
|
-- Using package imports here because there is a bug in cabal2nix that forces
|
|
|
|
|
-- us to put the hashing package in the unconditional dependency list.
|
|
|
|
|
-- See https://github.com/NixOS/cabal2nix/issues/348 for more info
|
|
|
|
|
#if MIN_VERSION_hashing(0, 1, 0)
|
2019-03-15 22:33:03 +01:00
|
|
|
|
import "hashing" Crypto.Hash
|
2018-05-09 01:40:56 +02:00
|
|
|
|
import qualified "hashing" Crypto.Hash.MD5 as MD5
|
|
|
|
|
import qualified "hashing" Crypto.Hash.SHA1 as SHA1
|
|
|
|
|
import qualified "hashing" Crypto.Hash.SHA256 as SHA256
|
|
|
|
|
import qualified "hashing" Crypto.Hash.SHA512 as SHA512
|
|
|
|
|
#else
|
|
|
|
|
import qualified "cryptohash-md5" Crypto.Hash.MD5 as MD5
|
|
|
|
|
import qualified "cryptohash-sha1" Crypto.Hash.SHA1 as SHA1
|
|
|
|
|
import qualified "cryptohash-sha256" Crypto.Hash.SHA256 as SHA256
|
|
|
|
|
import qualified "cryptohash-sha512" Crypto.Hash.SHA512 as SHA512
|
|
|
|
|
#endif
|
2018-05-10 22:51:14 +02:00
|
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import qualified Data.Aeson as A
|
|
|
|
|
import Data.Align (alignWith)
|
|
|
|
|
import Data.Array
|
2018-09-09 17:01:09 +02:00
|
|
|
|
import Data.Bits
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Data.ByteString (ByteString)
|
2018-04-08 09:26:48 +02:00
|
|
|
|
import qualified Data.ByteString as B
|
2018-12-05 14:43:31 +01:00
|
|
|
|
import Data.ByteString.Base16 as Base16
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Data.Char (isDigit)
|
2018-04-21 07:36:40 +02:00
|
|
|
|
import Data.Fix
|
2018-04-17 06:39:41 +02:00
|
|
|
|
import Data.Foldable (foldrM)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import qualified Data.HashMap.Lazy as M
|
|
|
|
|
import Data.List
|
|
|
|
|
import Data.Maybe
|
2018-12-05 20:59:42 +01:00
|
|
|
|
import Data.Scientific
|
2018-05-10 22:51:14 +02:00
|
|
|
|
import Data.String.Interpolate.IsString
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Data.Text (Text)
|
|
|
|
|
import qualified Data.Text as Text
|
|
|
|
|
import Data.Text.Encoding
|
|
|
|
|
import qualified Data.Text.Lazy as LazyText
|
|
|
|
|
import qualified Data.Text.Lazy.Builder as Builder
|
|
|
|
|
import Data.These (fromThese)
|
2018-05-03 06:32:00 +02:00
|
|
|
|
import qualified Data.Time.Clock.POSIX as Time
|
2018-09-09 17:01:09 +02:00
|
|
|
|
import Data.Traversable (for, mapM)
|
2018-12-05 20:59:42 +01:00
|
|
|
|
import qualified Data.Vector as V
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Nix.Atoms
|
2018-04-15 07:58:50 +02:00
|
|
|
|
import Nix.Convert
|
2018-04-11 06:01:48 +02:00
|
|
|
|
import Nix.Effects
|
2018-04-21 19:10:22 +02:00
|
|
|
|
import qualified Nix.Eval as Eval
|
2018-04-09 09:52:10 +02:00
|
|
|
|
import Nix.Exec
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Nix.Expr.Types
|
2018-04-17 06:39:41 +02:00
|
|
|
|
import Nix.Expr.Types.Annotated
|
2018-04-24 11:14:27 +02:00
|
|
|
|
import Nix.Frames
|
2018-12-09 19:57:58 +01:00
|
|
|
|
import Nix.Json
|
2018-04-08 02:24:22 +02:00
|
|
|
|
import Nix.Normal
|
2018-05-03 06:32:00 +02:00
|
|
|
|
import Nix.Options
|
2018-09-10 04:09:11 +02:00
|
|
|
|
import Nix.Parser hiding (nixPath)
|
2018-04-24 11:14:27 +02:00
|
|
|
|
import Nix.Render
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Nix.Scope
|
2018-11-19 13:04:08 +01:00
|
|
|
|
import Nix.String
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Nix.Thunk
|
|
|
|
|
import Nix.Utils
|
|
|
|
|
import Nix.Value
|
|
|
|
|
import Nix.XML
|
2019-03-10 04:35:19 +01:00
|
|
|
|
import System.Nix.Internal.Hash (printHashBytes32)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import System.FilePath
|
2018-11-16 21:16:17 +01:00
|
|
|
|
import System.Posix.Files (isRegularFile, isDirectory, isSymbolicLink)
|
2019-03-09 21:55:31 +01:00
|
|
|
|
import Text.Read
|
2018-04-07 21:02:50 +02:00
|
|
|
|
import Text.Regex.TDFA
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
-- | This constraint synonym establishes all the ways in which we must be able
|
|
|
|
|
-- to relate different Haskell values to the thunk representation that will
|
|
|
|
|
-- be chosen by the caller.
|
|
|
|
|
type MonadBuiltins e t f m =
|
|
|
|
|
( MonadNix e t f m
|
|
|
|
|
, FromValue NixString m t
|
|
|
|
|
, FromValue Path m t
|
|
|
|
|
, FromValue [t] m t
|
|
|
|
|
, FromValue (M.HashMap Text t) m t
|
|
|
|
|
, ToValue NixString m t
|
|
|
|
|
, ToValue Int m t
|
|
|
|
|
, ToValue () m t
|
|
|
|
|
, FromNix [NixString] m t
|
|
|
|
|
, ToNix t m (NValue t f m)
|
|
|
|
|
)
|
|
|
|
|
|
2018-09-09 17:01:09 +02:00
|
|
|
|
-- | Evaluate a nix expression in the default context
|
2019-03-15 22:33:03 +01:00
|
|
|
|
withNixContext :: forall e t f m r. (MonadBuiltins e t f m, Has e Options)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
=> Maybe FilePath -> m r -> m r
|
|
|
|
|
withNixContext mpath action = do
|
|
|
|
|
base <- builtins
|
|
|
|
|
opts :: Options <- asks (view hasLens)
|
2019-03-15 22:33:03 +01:00
|
|
|
|
let i = wrapValue @t @m @(NValue t f m) $ nvList $
|
|
|
|
|
map (wrapValue @t @m @(NValue t f m)
|
2018-09-16 02:04:54 +02:00
|
|
|
|
. nvStr . hackyMakeNixStringWithoutContext . Text.pack) (include opts)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
pushScope (M.singleton "__includes" i) $
|
|
|
|
|
pushScopes base $ case mpath of
|
|
|
|
|
Nothing -> action
|
|
|
|
|
Just path -> do
|
|
|
|
|
traceM $ "Setting __cur_file = " ++ show path
|
2019-03-15 22:33:03 +01:00
|
|
|
|
let ref = wrapValue @t @m @(NValue t f m) $ nvPath path
|
2018-09-09 17:01:09 +02:00
|
|
|
|
pushScope (M.singleton "__cur_file" ref) action
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
builtins :: (MonadBuiltins e t f m, Scoped t m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (Scopes m t)
|
2018-04-21 19:36:24 +02:00
|
|
|
|
builtins = do
|
2018-04-22 23:32:55 +02:00
|
|
|
|
ref <- thunk $ flip nvSet M.empty <$> buildMap
|
2018-04-08 01:33:50 +02:00
|
|
|
|
lst <- ([("builtins", ref)] ++) <$> topLevelBuiltins
|
2018-04-07 21:02:50 +02:00
|
|
|
|
pushScope (M.fromList lst) currentScopes
|
|
|
|
|
where
|
2018-04-28 22:49:34 +02:00
|
|
|
|
buildMap = M.fromList . map mapping <$> builtinsList
|
2018-04-29 01:47:33 +02:00
|
|
|
|
topLevelBuiltins = map mapping <$> fullBuiltinsList
|
2018-04-28 16:08:08 +02:00
|
|
|
|
|
2018-04-29 01:47:33 +02:00
|
|
|
|
fullBuiltinsList = map go <$> builtinsList
|
2018-04-28 16:08:08 +02:00
|
|
|
|
where
|
2018-04-29 01:47:33 +02:00
|
|
|
|
go b@(Builtin TopLevel _) = b
|
|
|
|
|
go (Builtin Normal (name, builtin)) =
|
|
|
|
|
Builtin TopLevel ("__" <> name, builtin)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
data BuiltinType = Normal | TopLevel
|
2019-03-15 21:27:05 +01:00
|
|
|
|
data Builtin t = Builtin
|
2018-04-29 01:47:33 +02:00
|
|
|
|
{ _kind :: BuiltinType
|
2019-03-15 21:27:05 +01:00
|
|
|
|
, mapping :: (Text, t)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
valueThunk :: forall e t f m. MonadBuiltins e t f m => NValue t f m -> t
|
|
|
|
|
valueThunk = wrapValue @_ @m
|
2018-04-09 09:52:10 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
force' :: forall e t f m. MonadBuiltins e t f m => t -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
force' = force ?? pure
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
builtinsList :: forall e t f m. MonadBuiltins e t f m => m [Builtin t]
|
2018-04-07 21:02:50 +02:00
|
|
|
|
builtinsList = sequence [
|
2018-11-19 13:04:08 +01:00
|
|
|
|
do version <- toValue (principledMakeNixStringWithoutContext "2.0")
|
2018-04-16 01:21:47 +02:00
|
|
|
|
pure $ Builtin Normal ("nixVersion", version)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2018-04-28 05:55:03 +02:00
|
|
|
|
, do version <- toValue (5 :: Int)
|
|
|
|
|
pure $ Builtin Normal ("langVersion", version)
|
|
|
|
|
|
2018-04-28 05:52:09 +02:00
|
|
|
|
, add0 Normal "nixPath" nixPath
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add TopLevel "abort" throw_ -- for now
|
2018-05-02 02:33:17 +02:00
|
|
|
|
, add2 Normal "add" add_
|
2018-09-09 17:01:09 +02:00
|
|
|
|
, add2 Normal "addErrorContext" addErrorContext
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add2 Normal "all" all_
|
|
|
|
|
, add2 Normal "any" any_
|
2018-04-18 04:01:32 +02:00
|
|
|
|
, add Normal "attrNames" attrNames
|
|
|
|
|
, add Normal "attrValues" attrValues
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add TopLevel "baseNameOf" baseNameOf
|
2018-09-09 17:01:09 +02:00
|
|
|
|
, add2 Normal "bitAnd" bitAnd
|
|
|
|
|
, add2 Normal "bitOr" bitOr
|
|
|
|
|
, add2 Normal "bitXor" bitXor
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add2 Normal "catAttrs" catAttrs
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add2 Normal "compareVersions" compareVersions_
|
|
|
|
|
, add Normal "concatLists" concatLists
|
2018-11-19 13:04:08 +01:00
|
|
|
|
, add' Normal "concatStringsSep" (arity2 principledIntercalateNixString)
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add0 Normal "currentSystem" currentSystem
|
2018-05-03 06:32:00 +02:00
|
|
|
|
, add0 Normal "currentTime" currentTime_
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add2 Normal "deepSeq" deepSeq
|
2018-05-10 22:51:14 +02:00
|
|
|
|
|
|
|
|
|
, add0 TopLevel "derivation" $(do
|
|
|
|
|
-- This is compiled in so that we only parse and evaluate it once,
|
|
|
|
|
-- at compile-time.
|
|
|
|
|
let Success expr = parseNixText [i|
|
|
|
|
|
/* This is the implementation of the ‘derivation’ builtin function.
|
|
|
|
|
It's actually a wrapper around the ‘derivationStrict’ primop. */
|
|
|
|
|
|
|
|
|
|
drvAttrs @ { outputs ? [ "out" ], ... }:
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
strict = derivationStrict drvAttrs;
|
|
|
|
|
|
|
|
|
|
commonAttrs = drvAttrs // (builtins.listToAttrs outputsList) //
|
|
|
|
|
{ all = map (x: x.value) outputsList;
|
|
|
|
|
inherit drvAttrs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
outputToAttrListElement = outputName:
|
|
|
|
|
{ name = outputName;
|
|
|
|
|
value = commonAttrs // {
|
|
|
|
|
outPath = builtins.getAttr outputName strict;
|
|
|
|
|
drvPath = strict.drvPath;
|
|
|
|
|
type = "derivation";
|
|
|
|
|
inherit outputName;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
outputsList = map outputToAttrListElement outputs;
|
|
|
|
|
|
|
|
|
|
in (builtins.head outputsList).value|]
|
2018-04-22 19:48:55 +02:00
|
|
|
|
[| cata Eval.eval expr |]
|
|
|
|
|
)
|
2018-05-10 22:51:14 +02:00
|
|
|
|
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add TopLevel "derivationStrict" derivationStrict_
|
|
|
|
|
, add TopLevel "dirOf" dirOf
|
2018-04-29 00:01:12 +02:00
|
|
|
|
, add2 Normal "div" div_
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add2 Normal "elem" elem_
|
|
|
|
|
, add2 Normal "elemAt" elemAt_
|
2018-04-29 01:37:01 +02:00
|
|
|
|
, add Normal "exec" exec_
|
2018-04-29 19:06:06 +02:00
|
|
|
|
, add0 Normal "false" (return $ nvConstant $ NBool False)
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "fetchTarball" fetchTarball
|
2018-05-03 06:38:13 +02:00
|
|
|
|
, add Normal "fetchurl" fetchurl
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add2 Normal "filter" filter_
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add3 Normal "foldl'" foldl'_
|
|
|
|
|
, add Normal "fromJSON" fromJSON
|
|
|
|
|
, add Normal "functionArgs" functionArgs
|
|
|
|
|
, add2 Normal "genList" genList
|
2018-04-28 23:56:08 +02:00
|
|
|
|
, add Normal "genericClosure" genericClosure
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add2 Normal "getAttr" getAttr
|
|
|
|
|
, add Normal "getEnv" getEnv_
|
|
|
|
|
, add2 Normal "hasAttr" hasAttr
|
2018-04-29 07:18:46 +02:00
|
|
|
|
, add Normal "hasContext" hasContext
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add' Normal "hashString" hashString
|
|
|
|
|
, add Normal "head" head_
|
|
|
|
|
, add TopLevel "import" import_
|
|
|
|
|
, add2 Normal "intersectAttrs" intersectAttrs
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add Normal "isAttrs" isAttrs
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "isBool" isBool
|
|
|
|
|
, add Normal "isFloat" isFloat
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add Normal "isFunction" isFunction
|
|
|
|
|
, add Normal "isInt" isInt
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "isList" isList
|
|
|
|
|
, add TopLevel "isNull" isNull
|
|
|
|
|
, add Normal "isString" isString
|
|
|
|
|
, add Normal "length" length_
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add2 Normal "lessThan" lessThan
|
|
|
|
|
, add Normal "listToAttrs" listToAttrs
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add2 TopLevel "map" map_
|
2018-09-09 17:01:09 +02:00
|
|
|
|
, add2 TopLevel "mapAttrs" mapAttrs_
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add2 Normal "match" match_
|
2018-04-29 20:43:06 +02:00
|
|
|
|
, add2 Normal "mul" mul_
|
2018-04-29 19:06:06 +02:00
|
|
|
|
, add0 Normal "null" (return $ nvConstant NNull)
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "parseDrvName" parseDrvName
|
2018-04-07 21:02:50 +02:00
|
|
|
|
, add2 Normal "partition" partition_
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "pathExists" pathExists_
|
2018-04-30 00:10:52 +02:00
|
|
|
|
, add TopLevel "placeholder" placeHolder
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "readDir" readDir_
|
|
|
|
|
, add Normal "readFile" readFile_
|
2018-05-20 15:47:53 +02:00
|
|
|
|
, add2 Normal "findFile" findFile_
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add2 TopLevel "removeAttrs" removeAttrs
|
|
|
|
|
, add3 Normal "replaceStrings" replaceStrings
|
|
|
|
|
, add2 TopLevel "scopedImport" scopedImport
|
|
|
|
|
, add2 Normal "seq" seq_
|
|
|
|
|
, add2 Normal "sort" sort_
|
|
|
|
|
, add2 Normal "split" split_
|
|
|
|
|
, add Normal "splitVersion" splitVersion_
|
2018-12-02 21:59:50 +01:00
|
|
|
|
, add0 Normal "storeDir" (return $ nvStr $ principledMakeNixStringWithoutContext "/nix/store")
|
2018-11-19 13:04:08 +01:00
|
|
|
|
, add' Normal "stringLength" (arity1 $ Text.length . principledStringIgnoreContext)
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add' Normal "sub" (arity2 ((-) @Integer))
|
|
|
|
|
, add' Normal "substring" substring
|
|
|
|
|
, add Normal "tail" tail_
|
2018-04-29 19:06:06 +02:00
|
|
|
|
, add0 Normal "true" (return $ nvConstant $ NBool True)
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add TopLevel "throw" throw_
|
2018-12-05 20:59:42 +01:00
|
|
|
|
, add Normal "toJSON" prim_toJSON
|
2018-05-11 02:32:47 +02:00
|
|
|
|
, add2 Normal "toFile" toFile
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "toPath" toPath
|
|
|
|
|
, add TopLevel "toString" toString
|
|
|
|
|
, add Normal "toXML" toXML_
|
2018-04-29 01:37:01 +02:00
|
|
|
|
, add2 TopLevel "trace" trace_
|
2018-04-22 19:48:55 +02:00
|
|
|
|
, add Normal "tryEval" tryEval
|
|
|
|
|
, add Normal "typeOf" typeOf
|
|
|
|
|
, add Normal "unsafeDiscardStringContext" unsafeDiscardStringContext
|
|
|
|
|
, add2 Normal "unsafeGetAttrPos" unsafeGetAttrPos
|
2018-04-29 03:29:25 +02:00
|
|
|
|
, add Normal "valueSize" getRecursiveSize
|
2018-04-07 21:02:50 +02:00
|
|
|
|
]
|
|
|
|
|
where
|
|
|
|
|
wrap t n f = Builtin t (n, f)
|
|
|
|
|
|
|
|
|
|
arity1 f = Prim . pure . f
|
|
|
|
|
arity2 f = ((Prim . pure) .) . f
|
|
|
|
|
|
2018-05-02 02:33:17 +02:00
|
|
|
|
mkThunk n = thunk . withFrame Info
|
|
|
|
|
(ErrorCall $ "While calling builtin " ++ Text.unpack n ++ "\n")
|
2018-04-19 01:12:31 +02:00
|
|
|
|
|
|
|
|
|
add0 t n v = wrap t n <$> mkThunk n v
|
|
|
|
|
add t n v = wrap t n <$> mkThunk n (builtin (Text.unpack n) v)
|
|
|
|
|
add2 t n v = wrap t n <$> mkThunk n (builtin2 (Text.unpack n) v)
|
|
|
|
|
add3 t n v = wrap t n <$> mkThunk n (builtin3 (Text.unpack n) v)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
add' :: ToBuiltin t f m a => BuiltinType -> Text -> a -> m (Builtin t)
|
2018-04-19 01:12:31 +02:00
|
|
|
|
add' t n v = wrap t n <$> mkThunk n (toBuiltin (Text.unpack n) v)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
-- Primops
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
foldNixPath :: forall e t f m r. MonadBuiltins e t f m
|
2018-09-09 17:01:09 +02:00
|
|
|
|
=> (FilePath -> Maybe String -> NixPathEntryType -> r -> m r) -> r -> m r
|
2018-04-14 01:37:11 +02:00
|
|
|
|
foldNixPath f z = do
|
2018-11-17 22:21:03 +01:00
|
|
|
|
mres <- lookupVar "__includes"
|
2018-04-14 01:37:11 +02:00
|
|
|
|
dirs <- case mres of
|
|
|
|
|
Nothing -> return []
|
2018-11-19 13:04:08 +01:00
|
|
|
|
Just v -> fromNix v
|
2018-04-14 01:37:11 +02:00
|
|
|
|
menv <- getEnvVar "NIX_PATH"
|
2018-11-19 13:04:08 +01:00
|
|
|
|
foldrM go z $ map (fromInclude . principledStringIgnoreContext) dirs ++ case menv of
|
2018-04-14 01:37:11 +02:00
|
|
|
|
Nothing -> []
|
2018-09-09 17:01:09 +02:00
|
|
|
|
Just str -> uriAwareSplit (Text.pack str)
|
2018-04-14 01:37:11 +02:00
|
|
|
|
where
|
2018-09-09 17:01:09 +02:00
|
|
|
|
fromInclude x
|
|
|
|
|
| "://" `Text.isInfixOf` x = (x, PathEntryURI)
|
|
|
|
|
| otherwise = (x, PathEntryPath)
|
|
|
|
|
go (x, ty) rest = case Text.splitOn "=" x of
|
|
|
|
|
[p] -> f (Text.unpack p) Nothing ty rest
|
|
|
|
|
[n, p] -> f (Text.unpack p) (Just (Text.unpack n)) ty rest
|
2018-05-02 02:33:17 +02:00
|
|
|
|
_ -> throwError $ ErrorCall $ "Unexpected entry in NIX_PATH: " ++ show x
|
2018-04-14 01:37:11 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
nixPath :: MonadBuiltins e t f m => m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
nixPath = fmap nvList $ flip foldNixPath [] $ \p mn ty rest ->
|
2018-04-17 06:39:41 +02:00
|
|
|
|
pure $ valueThunk
|
2018-04-22 23:32:55 +02:00
|
|
|
|
(flip nvSet mempty $ M.fromList
|
2018-09-09 17:01:09 +02:00
|
|
|
|
[ case ty of
|
|
|
|
|
PathEntryPath -> ("path", valueThunk $ nvPath p)
|
2018-09-16 02:04:54 +02:00
|
|
|
|
PathEntryURI -> ("uri", valueThunk $ nvStr (hackyMakeNixStringWithoutContext (Text.pack p)))
|
2018-04-17 06:39:41 +02:00
|
|
|
|
, ("prefix", valueThunk $
|
2018-09-16 02:04:54 +02:00
|
|
|
|
nvStr (hackyMakeNixStringWithoutContext $ Text.pack (fromMaybe "" mn))) ]) : rest
|
2018-04-17 06:39:41 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
toString :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-18 00:26:10 +01:00
|
|
|
|
toString str = str >>= coerceToString DontCopyToStore CoerceAny >>= toNix
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
hasAttr :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-05-02 02:33:17 +02:00
|
|
|
|
hasAttr x y =
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromValue x >>= fromStringNoContext >>= \key ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @(AttrSet t, AttrSet SourcePos) y >>= \(aset, _) ->
|
2018-05-02 02:33:17 +02:00
|
|
|
|
toNix $ M.member key aset
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
attrsetGet :: MonadBuiltins e t f m => Text -> AttrSet t -> m t
|
2018-04-30 17:17:34 +02:00
|
|
|
|
attrsetGet k s = case M.lookup k s of
|
|
|
|
|
Just v -> pure v
|
|
|
|
|
Nothing ->
|
2018-05-02 02:33:17 +02:00
|
|
|
|
throwError $ ErrorCall $ "Attribute '" ++ Text.unpack k ++ "' required"
|
2018-04-30 17:17:34 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
hasContext :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-29 07:18:46 +02:00
|
|
|
|
hasContext =
|
2018-09-16 02:04:54 +02:00
|
|
|
|
toNix . stringHasContext <=< fromValue
|
2018-04-29 07:18:46 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
getAttr :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-05-10 09:07:13 +02:00
|
|
|
|
getAttr x y =
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromValue x >>= fromStringNoContext >>= \key ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @(AttrSet t, AttrSet SourcePos) y >>= \(aset, _) ->
|
2018-05-10 09:07:13 +02:00
|
|
|
|
attrsetGet key aset >>= force'
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
unsafeGetAttrPos :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
unsafeGetAttrPos x y = x >>= \x' -> y >>= \y' -> case (x', y') of
|
2018-09-09 17:01:09 +02:00
|
|
|
|
(NVStr ns, NVSet _ apos) -> case M.lookup (hackyStringIgnoreContext ns) apos of
|
2018-05-06 14:17:43 +02:00
|
|
|
|
Nothing -> pure $ nvConstant NNull
|
2018-04-16 04:05:44 +02:00
|
|
|
|
Just delta -> toValue delta
|
2018-05-20 15:47:53 +02:00
|
|
|
|
(x, y) -> throwError $ ErrorCall $ "Invalid types for builtins.unsafeGetAttrPos: "
|
2018-04-11 06:11:46 +02:00
|
|
|
|
++ show (x, y)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2018-04-16 06:03:43 +02:00
|
|
|
|
-- This function is a bit special in that it doesn't care about the contents
|
|
|
|
|
-- of the list.
|
2019-03-15 22:33:03 +01:00
|
|
|
|
length_ :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
length_ = toValue . (length :: [t] -> Int) <=< fromValue
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
add_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-29 00:22:28 +02:00
|
|
|
|
add_ x y = x >>= \x' -> y >>= \y' -> case (x', y') of
|
|
|
|
|
(NVConstant (NInt x), NVConstant (NInt y)) ->
|
|
|
|
|
toNix ( x + y :: Integer)
|
|
|
|
|
(NVConstant (NFloat x), NVConstant (NInt y)) -> toNix (x + fromInteger y)
|
|
|
|
|
(NVConstant (NInt x), NVConstant (NFloat y)) -> toNix (fromInteger x + y)
|
|
|
|
|
(NVConstant (NFloat x), NVConstant (NFloat y)) -> toNix (x + y)
|
|
|
|
|
(_, _) ->
|
|
|
|
|
throwError $ Addition x' y'
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
mul_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-29 20:43:06 +02:00
|
|
|
|
mul_ x y = x >>= \x' -> y >>= \y' -> case (x', y') of
|
|
|
|
|
(NVConstant (NInt x), NVConstant (NInt y)) ->
|
|
|
|
|
toNix ( x * y :: Integer)
|
|
|
|
|
(NVConstant (NFloat x), NVConstant (NInt y)) -> toNix (x * fromInteger y)
|
|
|
|
|
(NVConstant (NInt x), NVConstant (NFloat y)) -> toNix (fromInteger x * y)
|
|
|
|
|
(NVConstant (NFloat x), NVConstant (NFloat y)) -> toNix (x * y)
|
|
|
|
|
(_, _) ->
|
|
|
|
|
throwError $ Multiplication x' y'
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
div_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-29 00:01:12 +02:00
|
|
|
|
div_ x y = x >>= \x' -> y >>= \y' -> case (x', y') of
|
2018-06-12 21:49:55 +02:00
|
|
|
|
(NVConstant (NInt x), NVConstant (NInt y)) | y /= 0 ->
|
2018-04-29 00:01:12 +02:00
|
|
|
|
toNix (floor (fromInteger x / fromInteger y :: Double) :: Integer)
|
2018-06-12 21:49:55 +02:00
|
|
|
|
(NVConstant (NFloat x), NVConstant (NInt y)) | y /= 0 ->
|
|
|
|
|
toNix (x / fromInteger y)
|
2018-11-17 22:08:02 +01:00
|
|
|
|
(NVConstant (NInt x), NVConstant (NFloat y)) | y /= 0 ->
|
2018-06-12 21:49:55 +02:00
|
|
|
|
toNix (fromInteger x / y)
|
2018-11-17 22:08:02 +01:00
|
|
|
|
(NVConstant (NFloat x), NVConstant (NFloat y)) | y /= 0 ->
|
2018-06-12 21:49:55 +02:00
|
|
|
|
toNix (x / y)
|
2018-04-29 00:01:12 +02:00
|
|
|
|
(_, _) ->
|
|
|
|
|
throwError $ Division x' y'
|
|
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
|
anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
|
|
|
|
|
anyM _ [] = return False
|
|
|
|
|
anyM p (x:xs) = do
|
|
|
|
|
q <- p x
|
|
|
|
|
if q then return True
|
|
|
|
|
else anyM p xs
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
any_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-18 02:46:27 +02:00
|
|
|
|
any_ fun xs = fun >>= \f ->
|
2018-04-26 05:46:31 +02:00
|
|
|
|
toNix <=< anyM fromValue <=< mapM ((f `callFunc`) . force')
|
2018-04-18 02:46:27 +02:00
|
|
|
|
<=< fromValue $ xs
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
|
|
|
|
|
allM _ [] = return True
|
|
|
|
|
allM p (x:xs) = do
|
|
|
|
|
q <- p x
|
|
|
|
|
if q then allM p xs
|
|
|
|
|
else return False
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
all_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-18 02:46:27 +02:00
|
|
|
|
all_ fun xs = fun >>= \f ->
|
2018-04-26 05:46:31 +02:00
|
|
|
|
toNix <=< allM fromValue <=< mapM ((f `callFunc`) . force')
|
2018-04-18 02:46:27 +02:00
|
|
|
|
<=< fromValue $ xs
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
foldl'_ :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-18 03:30:05 +02:00
|
|
|
|
foldl'_ fun z xs =
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fun >>= \f -> fromValue @[t] xs >>= foldl' (go f) z
|
2018-04-18 03:30:05 +02:00
|
|
|
|
where
|
2018-04-25 10:10:15 +02:00
|
|
|
|
go f b a = f `callFunc` b >>= (`callFunc` force' a)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
head_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
head_ = fromValue >=> \case
|
2018-05-02 02:33:17 +02:00
|
|
|
|
[] -> throwError $ ErrorCall "builtins.head: empty list"
|
2018-04-17 06:39:41 +02:00
|
|
|
|
h:_ -> force' h
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
tail_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
tail_ = fromValue >=> \case
|
2018-05-02 02:33:17 +02:00
|
|
|
|
[] -> throwError $ ErrorCall "builtins.tail: empty list"
|
2018-04-22 23:32:55 +02:00
|
|
|
|
_:t -> return $ nvList t
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
data VersionComponent
|
|
|
|
|
= VersionComponent_Pre -- ^ The string "pre"
|
|
|
|
|
| VersionComponent_String Text -- ^ A string other than "pre"
|
|
|
|
|
| VersionComponent_Number Integer -- ^ A number
|
|
|
|
|
deriving (Show, Read, Eq, Ord)
|
|
|
|
|
|
|
|
|
|
versionComponentToString :: VersionComponent -> Text
|
|
|
|
|
versionComponentToString = \case
|
|
|
|
|
VersionComponent_Pre -> "pre"
|
|
|
|
|
VersionComponent_String s -> s
|
|
|
|
|
VersionComponent_Number n -> Text.pack $ show n
|
|
|
|
|
|
|
|
|
|
-- | Based on https://github.com/NixOS/nix/blob/4ee4fda521137fed6af0446948b3877e0c5db803/src/libexpr/names.cc#L44
|
|
|
|
|
versionComponentSeparators :: String
|
|
|
|
|
versionComponentSeparators = ".-"
|
|
|
|
|
|
|
|
|
|
splitVersion :: Text -> [VersionComponent]
|
|
|
|
|
splitVersion s = case Text.uncons s of
|
|
|
|
|
Nothing -> []
|
|
|
|
|
Just (h, t)
|
|
|
|
|
| h `elem` versionComponentSeparators -> splitVersion t
|
|
|
|
|
| isDigit h ->
|
|
|
|
|
let (digits, rest) = Text.span isDigit s
|
2019-03-16 22:41:25 +01:00
|
|
|
|
in VersionComponent_Number
|
|
|
|
|
(fromMaybe (error $ "splitVersion: couldn't parse " <> show digits)
|
|
|
|
|
$ readMaybe
|
|
|
|
|
$ Text.unpack digits)
|
|
|
|
|
: splitVersion rest
|
2018-04-07 21:02:50 +02:00
|
|
|
|
| otherwise ->
|
|
|
|
|
let (chars, rest) = Text.span (\c -> not $ isDigit c || c `elem` versionComponentSeparators) s
|
|
|
|
|
thisComponent = case chars of
|
|
|
|
|
"pre" -> VersionComponent_Pre
|
|
|
|
|
x -> VersionComponent_String x
|
|
|
|
|
in thisComponent : splitVersion rest
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
splitVersion_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
splitVersion_ = fromValue >=> fromStringNoContext >=> \s ->
|
2019-03-16 22:41:25 +01:00
|
|
|
|
return $ nvList $ flip map (splitVersion s) $
|
|
|
|
|
valueThunk . nvStr
|
|
|
|
|
. principledMakeNixStringWithoutContext
|
|
|
|
|
. versionComponentToString
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
compareVersions :: Text -> Text -> Ordering
|
|
|
|
|
compareVersions s1 s2 =
|
|
|
|
|
mconcat $ alignWith f (splitVersion s1) (splitVersion s2)
|
|
|
|
|
where
|
|
|
|
|
z = VersionComponent_String ""
|
|
|
|
|
f = uncurry compare . fromThese z z
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
compareVersions_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
compareVersions_ t1 t2 =
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromValue t1 >>= fromStringNoContext >>= \s1 ->
|
|
|
|
|
fromValue t2 >>= fromStringNoContext >>= \s2 ->
|
2018-11-18 00:39:45 +01:00
|
|
|
|
return $ nvConstant $ NInt $ case compareVersions s1 s2 of
|
|
|
|
|
LT -> -1
|
|
|
|
|
EQ -> 0
|
|
|
|
|
GT -> 1
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
splitDrvName :: Text -> (Text, Text)
|
|
|
|
|
splitDrvName s =
|
|
|
|
|
let sep = "-"
|
|
|
|
|
pieces = Text.splitOn sep s
|
|
|
|
|
isFirstVersionPiece p = case Text.uncons p of
|
|
|
|
|
Just (h, _) | isDigit h -> True
|
|
|
|
|
_ -> False
|
|
|
|
|
-- Like 'break', but always puts the first item into the first result
|
|
|
|
|
-- list
|
|
|
|
|
breakAfterFirstItem :: (a -> Bool) -> [a] -> ([a], [a])
|
|
|
|
|
breakAfterFirstItem f = \case
|
|
|
|
|
h : t ->
|
|
|
|
|
let (a, b) = break f t
|
|
|
|
|
in (h : a, b)
|
|
|
|
|
[] -> ([], [])
|
|
|
|
|
(namePieces, versionPieces) =
|
|
|
|
|
breakAfterFirstItem isFirstVersionPiece pieces
|
|
|
|
|
in (Text.intercalate sep namePieces, Text.intercalate sep versionPieces)
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
parseDrvName :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-21 07:21:53 +01:00
|
|
|
|
parseDrvName = fromValue >=> fromStringNoContext >=> \s -> do
|
2018-04-15 10:43:01 +02:00
|
|
|
|
let (name :: Text, version :: Text) = splitDrvName s
|
|
|
|
|
-- jww (2018-04-15): There should be an easier way to write this.
|
2018-04-16 04:05:44 +02:00
|
|
|
|
(toValue =<<) $ sequence $ M.fromList
|
2019-03-11 21:55:40 +01:00
|
|
|
|
[ ("name" :: Text,
|
2019-03-15 22:33:03 +01:00
|
|
|
|
thunk @t
|
2019-03-11 21:55:40 +01:00
|
|
|
|
(toValue $ principledMakeNixStringWithoutContext name))
|
|
|
|
|
, ("version",
|
2019-03-15 22:33:03 +01:00
|
|
|
|
thunk @t
|
2019-03-11 21:55:40 +01:00
|
|
|
|
(toValue $ principledMakeNixStringWithoutContext version)) ]
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
match_ :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
match_ pat str =
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromValue pat >>= fromStringNoContext >>= \p ->
|
|
|
|
|
fromValue str >>= \ns -> do
|
|
|
|
|
-- NOTE: Currently prim_match in nix/src/libexpr/primops.cc ignores the
|
|
|
|
|
-- context of its second argument. This is probably a bug but we're
|
|
|
|
|
-- going to preserve the behavior here until it is fixed upstream.
|
|
|
|
|
-- Relevant issue: https://github.com/NixOS/nix/issues/2547
|
|
|
|
|
let s = principledStringIgnoreContext ns
|
|
|
|
|
|
2018-04-17 06:39:41 +02:00
|
|
|
|
let re = makeRegex (encodeUtf8 p) :: Regex
|
2018-11-19 15:01:34 +01:00
|
|
|
|
let mkMatch t = if Text.null t
|
|
|
|
|
then toValue () -- Shorthand for Null
|
|
|
|
|
else toValue $ principledMakeNixStringWithoutContext t
|
2018-04-17 06:39:41 +02:00
|
|
|
|
case matchOnceText re (encodeUtf8 s) of
|
|
|
|
|
Just ("", sarr, "") -> do
|
|
|
|
|
let s = map fst (elems sarr)
|
2018-11-19 15:01:34 +01:00
|
|
|
|
nvList <$> traverse (mkMatch . decodeUtf8)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
(if length s > 1 then tail s else s)
|
2018-04-22 23:32:55 +02:00
|
|
|
|
_ -> pure $ nvConstant NNull
|
2018-04-17 06:39:41 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
split_ :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
split_ pat str =
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromValue pat >>= fromStringNoContext >>= \p ->
|
|
|
|
|
fromValue str >>= \ns -> do
|
|
|
|
|
-- NOTE: Currently prim_split in nix/src/libexpr/primops.cc ignores the
|
|
|
|
|
-- context of its second argument. This is probably a bug but we're
|
|
|
|
|
-- going to preserve the behavior here until it is fixed upstream.
|
|
|
|
|
-- Relevant issue: https://github.com/NixOS/nix/issues/2547
|
|
|
|
|
let s = principledStringIgnoreContext ns
|
2018-04-17 06:39:41 +02:00
|
|
|
|
let re = makeRegex (encodeUtf8 p) :: Regex
|
|
|
|
|
haystack = encodeUtf8 s
|
2018-04-22 23:32:55 +02:00
|
|
|
|
return $ nvList $
|
2018-04-17 06:39:41 +02:00
|
|
|
|
splitMatches 0 (map elems $ matchAllText re haystack) haystack
|
2018-04-08 09:26:48 +02:00
|
|
|
|
|
|
|
|
|
splitMatches
|
2019-03-15 22:33:03 +01:00
|
|
|
|
:: forall e t f m. MonadBuiltins e t f m
|
2018-04-08 09:26:48 +02:00
|
|
|
|
=> Int
|
|
|
|
|
-> [[(ByteString, (Int, Int))]]
|
|
|
|
|
-> ByteString
|
2019-03-15 21:27:05 +01:00
|
|
|
|
-> [t]
|
2018-04-08 09:26:48 +02:00
|
|
|
|
splitMatches _ [] haystack = [thunkStr haystack]
|
|
|
|
|
splitMatches _ ([]:_) _ = error "Error in splitMatches: this should never happen!"
|
|
|
|
|
splitMatches numDropped (((_,(start,len)):captures):mts) haystack =
|
2018-04-11 08:06:47 +02:00
|
|
|
|
thunkStr before : caps : splitMatches (numDropped + relStart + len) mts (B.drop len rest)
|
2018-04-08 09:26:48 +02:00
|
|
|
|
where
|
2018-04-11 08:06:47 +02:00
|
|
|
|
relStart = max 0 start - numDropped
|
|
|
|
|
(before,rest) = B.splitAt relStart haystack
|
2018-04-22 23:32:55 +02:00
|
|
|
|
caps = valueThunk $ nvList (map f captures)
|
|
|
|
|
f (a,(s,_)) = if s < 0 then valueThunk (nvConstant NNull) else thunkStr a
|
2018-04-08 09:26:48 +02:00
|
|
|
|
|
2018-09-16 02:04:54 +02:00
|
|
|
|
thunkStr s = valueThunk (nvStr (hackyMakeNixStringWithoutContext (decodeUtf8 s)))
|
2018-04-08 09:26:48 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
substring :: MonadBuiltins e t f m => Int -> Int -> NixString -> Prim m NixString
|
2018-04-07 23:33:15 +02:00
|
|
|
|
substring start len str = Prim $
|
2018-04-07 21:02:50 +02:00
|
|
|
|
if start < 0 --NOTE: negative values of 'len' are OK
|
2018-05-02 02:33:17 +02:00
|
|
|
|
then throwError $ ErrorCall $ "builtins.substring: negative start position: " ++ show start
|
2018-11-19 13:04:08 +01:00
|
|
|
|
else pure $ principledModifyNixContents (Text.take len . Text.drop start) str
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
attrNames :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
attrNames = fromValue @(AttrSet t)
|
|
|
|
|
>=> toNix . map principledMakeNixStringWithoutContext . sort . M.keys
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
attrValues :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
attrValues = fromValue @(AttrSet t) >=>
|
|
|
|
|
toValue . fmap snd . sortOn (fst @Text @t) . M.toList
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
map_ :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-18 02:46:27 +02:00
|
|
|
|
map_ fun xs = fun >>= \f ->
|
2019-03-15 22:33:03 +01:00
|
|
|
|
toNix <=< traverse (thunk @t . withFrame Debug
|
|
|
|
|
(ErrorCall "While applying f in map:\n")
|
|
|
|
|
. (f `callFunc`) . force')
|
2019-03-15 21:27:05 +01:00
|
|
|
|
<=< fromValue @[t] $ xs
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
mapAttrs_ :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
mapAttrs_ fun xs = fun >>= \f ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @(AttrSet t) xs >>= \aset -> do
|
2018-09-09 17:01:09 +02:00
|
|
|
|
let pairs = M.toList aset
|
|
|
|
|
values <- for pairs $ \(key, value) ->
|
2019-03-15 22:33:03 +01:00
|
|
|
|
thunk @t $
|
2018-09-09 17:01:09 +02:00
|
|
|
|
withFrame Debug (ErrorCall "While applying f in mapAttrs:\n") $
|
2019-03-15 21:27:05 +01:00
|
|
|
|
callFunc ?? force' value
|
|
|
|
|
=<< callFunc f (pure (nvStr (principledMakeNixStringWithoutContext key)))
|
2018-09-09 17:01:09 +02:00
|
|
|
|
toNix . M.fromList . zip (map fst pairs) $ values
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
filter_ :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-18 02:46:27 +02:00
|
|
|
|
filter_ fun xs = fun >>= \f ->
|
2018-04-26 05:46:31 +02:00
|
|
|
|
toNix <=< filterM (fromValue <=< callFunc f . force')
|
2019-03-15 21:27:05 +01:00
|
|
|
|
<=< fromValue @[t] $ xs
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
catAttrs :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
catAttrs attrName xs =
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromValue attrName >>= fromStringNoContext >>= \n ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @[t] xs >>= \l ->
|
2018-04-22 23:32:55 +02:00
|
|
|
|
fmap (nvList . catMaybes) $
|
2018-04-17 06:39:41 +02:00
|
|
|
|
forM l $ fmap (M.lookup n) . fromValue
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
baseNameOf :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-12-05 12:26:59 +01:00
|
|
|
|
baseNameOf x = do
|
2018-12-03 11:00:50 +01:00
|
|
|
|
ns <- coerceToString DontCopyToStore CoerceStringy =<< x
|
|
|
|
|
pure $ nvStr (principledModifyNixContents (Text.pack . takeFileName . Text.unpack) ns)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
bitAnd :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
bitAnd x y =
|
|
|
|
|
fromValue @Integer x >>= \a ->
|
|
|
|
|
fromValue @Integer y >>= \b -> toNix (a .&. b)
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
bitOr :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
bitOr x y =
|
|
|
|
|
fromValue @Integer x >>= \a ->
|
|
|
|
|
fromValue @Integer y >>= \b -> toNix (a .|. b)
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
bitXor :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
bitXor x y =
|
|
|
|
|
fromValue @Integer x >>= \a ->
|
|
|
|
|
fromValue @Integer y >>= \b -> toNix (a `xor` b)
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
dirOf :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
dirOf x = x >>= \case
|
2018-11-17 22:23:41 +01:00
|
|
|
|
NVStr ns -> pure $ nvStr (principledModifyNixContents (Text.pack . takeDirectory . Text.unpack) ns)
|
2018-04-22 23:32:55 +02:00
|
|
|
|
NVPath path -> pure $ nvPath $ takeDirectory path
|
2018-05-02 02:33:17 +02:00
|
|
|
|
v -> throwError $ ErrorCall $ "dirOf: expected string or path, got " ++ show v
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2018-04-28 22:49:22 +02:00
|
|
|
|
-- jww (2018-04-28): This should only be a string argument, and not coerced?
|
2019-03-15 22:33:03 +01:00
|
|
|
|
unsafeDiscardStringContext :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
unsafeDiscardStringContext mnv = do
|
|
|
|
|
ns <- fromValue mnv
|
|
|
|
|
toNix $ principledMakeNixStringWithoutContext $ principledStringIgnoreContext ns
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-17 21:33:54 +01:00
|
|
|
|
seq_ :: MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
seq_ a b = a >> b
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-17 21:33:54 +01:00
|
|
|
|
deepSeq :: MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
deepSeq a b = do
|
|
|
|
|
-- We evaluate 'a' only for its effects, so data cycles are ignored.
|
2018-09-09 17:01:09 +02:00
|
|
|
|
normalForm_ =<< a
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
|
|
-- Then we evaluate the other argument to deepseq, thus this function
|
|
|
|
|
-- should always produce a result (unlike applying 'deepseq' on infinitely
|
|
|
|
|
-- recursive data structures in Haskell).
|
2018-04-17 06:39:41 +02:00
|
|
|
|
b
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
elem_ :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
elem_ x xs = x >>= \x' ->
|
2019-03-17 07:35:16 +01:00
|
|
|
|
toValue <=< anyM (valueEqM x' <=< force') <=< fromValue @[t] $ xs
|
2018-04-17 03:57:28 +02:00
|
|
|
|
|
|
|
|
|
elemAt :: [a] -> Int -> Maybe a
|
|
|
|
|
elemAt ls i = case drop i ls of
|
|
|
|
|
[] -> Nothing
|
|
|
|
|
a:_ -> Just a
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-17 21:33:54 +01:00
|
|
|
|
elemAt_ :: MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-26 05:46:31 +02:00
|
|
|
|
elemAt_ xs n = fromValue n >>= \n' -> fromValue xs >>= \xs' ->
|
2018-04-17 03:57:28 +02:00
|
|
|
|
case elemAt xs' n' of
|
2018-04-17 06:39:41 +02:00
|
|
|
|
Just a -> force' a
|
2018-05-02 02:33:17 +02:00
|
|
|
|
Nothing -> throwError $ ErrorCall $ "builtins.elem: Index " ++ show n'
|
2018-04-17 03:57:28 +02:00
|
|
|
|
++ " too large for list of length " ++ show (length xs')
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-17 21:33:54 +01:00
|
|
|
|
genList :: forall e t f m. MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-26 05:46:31 +02:00
|
|
|
|
genList generator = fromValue @Integer >=> \n ->
|
2018-04-17 06:39:41 +02:00
|
|
|
|
if n >= 0
|
|
|
|
|
then generator >>= \f ->
|
2019-03-11 21:55:40 +01:00
|
|
|
|
toNix =<< forM [0 .. n - 1]
|
2019-03-15 22:33:03 +01:00
|
|
|
|
(\i -> thunk @t $ f `callFunc` toNix i)
|
2018-05-02 02:33:17 +02:00
|
|
|
|
else throwError $ ErrorCall $ "builtins.genList: Expected a non-negative number, got "
|
2018-04-17 06:39:41 +02:00
|
|
|
|
++ show n
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-17 21:33:54 +01:00
|
|
|
|
genericClosure :: forall e t f m. MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
genericClosure = fromValue @(AttrSet t) >=> \s ->
|
2018-04-28 23:56:08 +02:00
|
|
|
|
case (M.lookup "startSet" s, M.lookup "operator" s) of
|
|
|
|
|
(Nothing, Nothing) ->
|
2018-05-02 02:33:17 +02:00
|
|
|
|
throwError $ ErrorCall $
|
|
|
|
|
"builtins.genericClosure: "
|
|
|
|
|
++ "Attributes 'startSet' and 'operator' required"
|
2018-04-28 23:56:08 +02:00
|
|
|
|
(Nothing, Just _) ->
|
2018-05-02 02:33:17 +02:00
|
|
|
|
throwError $ ErrorCall $
|
|
|
|
|
"builtins.genericClosure: Attribute 'startSet' required"
|
2018-04-28 23:56:08 +02:00
|
|
|
|
(Just _, Nothing) ->
|
2018-05-02 02:33:17 +02:00
|
|
|
|
throwError $ ErrorCall $
|
|
|
|
|
"builtins.genericClosure: Attribute 'operator' required"
|
2018-04-28 23:56:08 +02:00
|
|
|
|
(Just startSet, Just operator) ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @[t] startSet >>= \ss ->
|
2018-04-28 23:56:08 +02:00
|
|
|
|
force operator $ \op ->
|
2019-03-17 07:35:16 +01:00
|
|
|
|
toValue @[t] =<< snd <$> go op ss []
|
2018-04-28 23:56:08 +02:00
|
|
|
|
where
|
2019-03-17 07:35:16 +01:00
|
|
|
|
go :: NValue t f m -> [t] -> [NValue t f m] -> m ([NValue t f m], [t])
|
2018-04-28 23:56:08 +02:00
|
|
|
|
go _ [] ks = pure (ks, [])
|
2018-04-29 00:18:45 +02:00
|
|
|
|
go op (t:ts) ks =
|
2019-03-15 21:27:05 +01:00
|
|
|
|
force t $ \v -> fromValue @(AttrSet t) t >>= \s ->
|
2018-04-29 00:18:45 +02:00
|
|
|
|
case M.lookup "key" s of
|
|
|
|
|
Nothing ->
|
2018-05-02 02:33:17 +02:00
|
|
|
|
throwError $ ErrorCall $
|
|
|
|
|
"builtins.genericClosure: Attribute 'key' required"
|
2019-03-17 07:35:16 +01:00
|
|
|
|
Just k -> force k $ \k' -> do
|
|
|
|
|
ys <- fromValue @[t] =<< (op `callFunc` pure v)
|
|
|
|
|
case ks of
|
|
|
|
|
[] -> checkComparable k' k'
|
|
|
|
|
j:_ -> checkComparable k' j
|
|
|
|
|
fmap (t:) <$> go op (ts ++ ys) (k':ks)
|
2018-04-28 23:56:08 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
replaceStrings :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-15 10:43:01 +02:00
|
|
|
|
replaceStrings tfrom tto ts =
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromNix tfrom >>= \(nsFrom :: [NixString]) ->
|
|
|
|
|
fromNix tto >>= \(nsTo :: [NixString]) ->
|
|
|
|
|
fromValue ts >>= \(ns :: NixString) -> do
|
|
|
|
|
let from = map principledStringIgnoreContext nsFrom
|
2018-12-05 17:53:59 +01:00
|
|
|
|
when (length nsFrom /= length nsTo) $
|
2018-05-02 02:33:17 +02:00
|
|
|
|
throwError $ ErrorCall $
|
|
|
|
|
"'from' and 'to' arguments to 'replaceStrings'"
|
|
|
|
|
++ " have different lengths"
|
2018-04-15 10:43:01 +02:00
|
|
|
|
let lookupPrefix s = do
|
|
|
|
|
(prefix, replacement) <-
|
2018-12-05 17:53:59 +01:00
|
|
|
|
find ((`Text.isPrefixOf` s) . fst) $ zip from nsTo
|
2018-04-15 10:43:01 +02:00
|
|
|
|
let rest = Text.drop (Text.length prefix) s
|
|
|
|
|
return (prefix, replacement, rest)
|
2018-12-05 17:53:59 +01:00
|
|
|
|
finish b = principledMakeNixString (LazyText.toStrict $ Builder.toLazyText b)
|
|
|
|
|
go orig result ctx = case lookupPrefix orig of
|
2018-04-15 10:43:01 +02:00
|
|
|
|
Nothing -> case Text.uncons orig of
|
2018-12-05 17:53:59 +01:00
|
|
|
|
Nothing -> finish result ctx
|
|
|
|
|
Just (h, t) -> go t (result <> Builder.singleton h) ctx
|
|
|
|
|
Just (prefix, replacementNS, rest) ->
|
|
|
|
|
let replacement = principledStringIgnoreContext replacementNS
|
|
|
|
|
newCtx = principledGetContext replacementNS
|
|
|
|
|
in case prefix of
|
|
|
|
|
"" -> case Text.uncons rest of
|
|
|
|
|
Nothing -> finish (result <> Builder.fromText replacement) (ctx <> newCtx)
|
|
|
|
|
Just (h, t) -> go t (mconcat
|
|
|
|
|
[ result
|
|
|
|
|
, Builder.fromText replacement
|
|
|
|
|
, Builder.singleton h
|
|
|
|
|
]) (ctx <> newCtx)
|
|
|
|
|
_ -> go rest (result <> Builder.fromText replacement) (ctx <> newCtx)
|
|
|
|
|
toNix $ go (principledStringIgnoreContext ns) mempty $ principledGetContext ns
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
removeAttrs :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
removeAttrs set = fromNix >=> \(nsToRemove :: [NixString]) ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @(AttrSet t,
|
2018-11-19 13:04:08 +01:00
|
|
|
|
AttrSet SourcePos) set >>= \(m, p) -> do
|
|
|
|
|
toRemove <- mapM fromStringNoContext nsToRemove
|
2018-04-17 06:39:41 +02:00
|
|
|
|
toNix (go m toRemove, go p toRemove)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
where
|
|
|
|
|
go = foldl' (flip M.delete)
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
intersectAttrs :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
intersectAttrs set1 set2 =
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @(AttrSet t,
|
2018-04-27 02:13:22 +02:00
|
|
|
|
AttrSet SourcePos) set1 >>= \(s1, p1) ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @(AttrSet t,
|
2018-04-27 02:13:22 +02:00
|
|
|
|
AttrSet SourcePos) set2 >>= \(s2, p2) ->
|
2018-04-22 23:32:55 +02:00
|
|
|
|
return $ nvSet (s2 `M.intersection` s1) (p2 `M.intersection` p1)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
functionArgs :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
functionArgs fun = fun >>= \case
|
2019-03-15 21:27:05 +01:00
|
|
|
|
NVClosure p _ -> toValue @(AttrSet t) $
|
2018-04-27 02:13:22 +02:00
|
|
|
|
valueThunk . nvConstant . NBool <$>
|
|
|
|
|
case p of
|
|
|
|
|
Param name -> M.singleton name False
|
|
|
|
|
ParamSet s _ _ -> isJust <$> M.fromList s
|
2018-05-02 02:33:17 +02:00
|
|
|
|
v -> throwError $ ErrorCall $
|
|
|
|
|
"builtins.functionArgs: expected function, got " ++ show v
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
toFile :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-05-11 02:32:47 +02:00
|
|
|
|
toFile name s = do
|
2018-11-19 13:04:08 +01:00
|
|
|
|
name' <- fromStringNoContext =<< fromValue name
|
2018-05-11 02:32:47 +02:00
|
|
|
|
s' <- fromValue s
|
2018-11-21 07:50:24 +01:00
|
|
|
|
-- TODO Using hacky here because we still need to turn the context into
|
|
|
|
|
-- runtime references of the resulting file.
|
|
|
|
|
-- See prim_toFile in nix/src/libexpr/primops.cc
|
|
|
|
|
mres <- toFile_ (Text.unpack name') (Text.unpack $ hackyStringIgnoreContext s')
|
2018-11-19 13:04:08 +01:00
|
|
|
|
let t = Text.pack $ unStorePath mres
|
2018-11-21 07:50:24 +01:00
|
|
|
|
sc = StringContext t DirectPath
|
2018-11-19 13:04:08 +01:00
|
|
|
|
toNix $ principledMakeNixStringWithSingletonContext t sc
|
2018-05-11 02:32:47 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
toPath :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-26 05:46:31 +02:00
|
|
|
|
toPath = fromValue @Path >=> toNix @Path
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
pathExists_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
pathExists_ path = path >>= \case
|
2018-04-16 06:03:43 +02:00
|
|
|
|
NVPath p -> toNix =<< pathExists p
|
2018-09-09 17:01:09 +02:00
|
|
|
|
NVStr ns -> toNix =<< pathExists (Text.unpack (hackyStringIgnoreContext ns))
|
2018-05-02 02:33:17 +02:00
|
|
|
|
v -> throwError $ ErrorCall $
|
|
|
|
|
"builtins.pathExists: expected path, got " ++ show v
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
hasKind :: forall a e t f m. (MonadBuiltins e t f m, FromValue a m (NValue t f m))
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m)
|
2018-04-26 05:23:00 +02:00
|
|
|
|
hasKind = fromValueMay >=> toNix . \case Just (_ :: a) -> True; _ -> False
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isAttrs :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
isAttrs = hasKind @(AttrSet t)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isList :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
isList = hasKind @[t]
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isString :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
isString = hasKind @NixString
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isInt :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-16 08:01:22 +02:00
|
|
|
|
isInt = hasKind @Int
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isFloat :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-16 08:01:22 +02:00
|
|
|
|
isFloat = hasKind @Float
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isBool :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-16 08:01:22 +02:00
|
|
|
|
isBool = hasKind @Bool
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isNull :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-16 08:01:22 +02:00
|
|
|
|
isNull = hasKind @()
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
isFunction :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
isFunction func = func >>= \case
|
2018-04-16 08:01:22 +02:00
|
|
|
|
NVClosure {} -> toValue True
|
2018-04-16 04:05:44 +02:00
|
|
|
|
_ -> toValue False
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
throw_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
throw_ mnv = do
|
|
|
|
|
ns <- coerceToString CopyToStore CoerceStringy =<< mnv
|
|
|
|
|
throwError . ErrorCall . Text.unpack $ principledStringIgnoreContext ns
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
import_ :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
import_ = scopedImport (pure (nvSet M.empty M.empty))
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
scopedImport :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
scopedImport asetArg pathArg =
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @(AttrSet t) asetArg >>= \s ->
|
2018-09-09 17:01:09 +02:00
|
|
|
|
fromValue pathArg >>= \(Path p) -> do
|
2019-03-15 22:33:03 +01:00
|
|
|
|
path <- pathToDefaultNix @t @f @m p
|
2018-11-17 22:21:03 +01:00
|
|
|
|
mres <- lookupVar "__cur_file"
|
2018-09-09 17:01:09 +02:00
|
|
|
|
path' <- case mres of
|
|
|
|
|
Nothing -> do
|
|
|
|
|
traceM "No known current directory"
|
|
|
|
|
return path
|
2019-03-15 21:27:05 +01:00
|
|
|
|
Just p -> fromValue @_ @_ @t p >>= \(Path p') -> do
|
2018-09-09 17:01:09 +02:00
|
|
|
|
traceM $ "Current file being evaluated is: " ++ show p'
|
|
|
|
|
return $ takeDirectory p' </> path
|
2019-03-15 21:27:05 +01:00
|
|
|
|
clearScopes @t $
|
2018-09-09 17:01:09 +02:00
|
|
|
|
withNixContext (Just path') $
|
|
|
|
|
pushScope s $
|
2019-03-15 22:33:03 +01:00
|
|
|
|
importPath @t @f @m path'
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
getEnv_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
getEnv_ = fromValue >=> fromStringNoContext >=> \s -> do
|
2018-04-16 08:01:22 +02:00
|
|
|
|
mres <- getEnvVar (Text.unpack s)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
toNix $ principledMakeNixStringWithoutContext $
|
|
|
|
|
case mres of
|
2018-04-16 08:01:22 +02:00
|
|
|
|
Nothing -> ""
|
|
|
|
|
Just v -> Text.pack v
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
sort_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-18 02:46:27 +02:00
|
|
|
|
sort_ comparator xs = comparator >>= \comp ->
|
|
|
|
|
fromValue xs >>= sortByM (cmp comp) >>= toValue
|
|
|
|
|
where
|
|
|
|
|
cmp f a b = do
|
|
|
|
|
isLessThan <- f `callFunc` force' a >>= (`callFunc` force' b)
|
|
|
|
|
fromValue isLessThan >>= \case
|
|
|
|
|
True -> pure LT
|
|
|
|
|
False -> do
|
|
|
|
|
isGreaterThan <- f `callFunc` force' b >>= (`callFunc` force' a)
|
|
|
|
|
fromValue isGreaterThan <&> \case
|
|
|
|
|
True -> GT
|
|
|
|
|
False -> EQ
|
2018-04-17 06:39:41 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
lessThan :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
lessThan ta tb = ta >>= \va -> tb >>= \vb -> do
|
2018-05-02 02:33:17 +02:00
|
|
|
|
let badType = throwError $ ErrorCall $
|
|
|
|
|
"builtins.lessThan: expected two numbers or two strings, "
|
|
|
|
|
++ "got " ++ show va ++ " and " ++ show vb
|
2018-04-22 23:32:55 +02:00
|
|
|
|
nvConstant . NBool <$> case (va, vb) of
|
2018-04-07 21:02:50 +02:00
|
|
|
|
(NVConstant ca, NVConstant cb) -> case (ca, cb) of
|
|
|
|
|
(NInt a, NInt b) -> pure $ a < b
|
|
|
|
|
(NFloat a, NInt b) -> pure $ a < fromInteger b
|
|
|
|
|
(NInt a, NFloat b) -> pure $ fromInteger a < b
|
|
|
|
|
(NFloat a, NFloat b) -> pure $ a < b
|
|
|
|
|
_ -> badType
|
2019-03-10 19:16:36 +01:00
|
|
|
|
(NVStr a, NVStr b) -> pure $ principledStringIgnoreContext a < principledStringIgnoreContext b
|
2018-04-07 21:02:50 +02:00
|
|
|
|
_ -> badType
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
concatLists :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
concatLists = fromValue @[t]
|
|
|
|
|
>=> mapM (fromValue @[t] >=> pure)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
>=> toValue . concat
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
listToAttrs :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2019-03-15 21:27:05 +01:00
|
|
|
|
listToAttrs = fromValue @[t] >=> \l ->
|
2018-04-22 23:32:55 +02:00
|
|
|
|
fmap (flip nvSet M.empty . M.fromList . reverse) $
|
2019-03-15 21:27:05 +01:00
|
|
|
|
forM l $ fromValue @(AttrSet t) >=> \s -> do
|
2018-11-19 13:04:08 +01:00
|
|
|
|
name <- fromStringNoContext =<< fromValue =<< attrsetGet "name" s
|
2018-05-06 09:35:29 +02:00
|
|
|
|
val <- attrsetGet "value" s
|
2018-11-19 13:04:08 +01:00
|
|
|
|
pure (name, val)
|
|
|
|
|
|
|
|
|
|
-- prim_hashString from nix/src/libexpr/primops.cc
|
|
|
|
|
-- fail if context in the algo arg
|
|
|
|
|
-- propagate context from the s arg
|
2019-03-15 22:33:03 +01:00
|
|
|
|
hashString :: MonadBuiltins e t f m => NixString -> NixString -> Prim m NixString
|
2018-11-19 13:04:08 +01:00
|
|
|
|
hashString nsAlgo ns = Prim $ do
|
|
|
|
|
algo <- fromStringNoContext nsAlgo
|
|
|
|
|
let f g = pure $ principledModifyNixContents g ns
|
2018-05-09 01:40:56 +02:00
|
|
|
|
case algo of
|
2018-11-19 13:04:08 +01:00
|
|
|
|
"md5" -> f $ \s ->
|
2018-05-09 01:40:56 +02:00
|
|
|
|
#if MIN_VERSION_hashing(0, 1, 0)
|
|
|
|
|
Text.pack $ show (hash (encodeUtf8 s) :: MD5.MD5)
|
|
|
|
|
#else
|
|
|
|
|
decodeUtf8 $ Base16.encode $ MD5.hash $ encodeUtf8 s
|
|
|
|
|
#endif
|
2018-11-19 13:04:08 +01:00
|
|
|
|
"sha1" -> f $ \s ->
|
2018-05-09 01:40:56 +02:00
|
|
|
|
#if MIN_VERSION_hashing(0, 1, 0)
|
|
|
|
|
Text.pack $ show (hash (encodeUtf8 s) :: SHA1.SHA1)
|
|
|
|
|
#else
|
|
|
|
|
decodeUtf8 $ Base16.encode $ SHA1.hash $ encodeUtf8 s
|
|
|
|
|
#endif
|
2018-11-19 13:04:08 +01:00
|
|
|
|
"sha256" -> f $ \s ->
|
2018-05-09 01:40:56 +02:00
|
|
|
|
#if MIN_VERSION_hashing(0, 1, 0)
|
|
|
|
|
Text.pack $ show (hash (encodeUtf8 s) :: SHA256.SHA256)
|
|
|
|
|
#else
|
|
|
|
|
decodeUtf8 $ Base16.encode $ SHA256.hash $ encodeUtf8 s
|
|
|
|
|
#endif
|
2018-11-19 13:04:08 +01:00
|
|
|
|
"sha512" -> f $ \s ->
|
2018-05-09 01:40:56 +02:00
|
|
|
|
#if MIN_VERSION_hashing(0, 1, 0)
|
|
|
|
|
Text.pack $ show (hash (encodeUtf8 s) :: SHA512.SHA512)
|
|
|
|
|
#else
|
|
|
|
|
decodeUtf8 $ Base16.encode $ SHA512.hash $ encodeUtf8 s
|
|
|
|
|
#endif
|
2018-05-02 02:33:17 +02:00
|
|
|
|
_ -> throwError $ ErrorCall $ "builtins.hashString: "
|
2018-04-07 21:02:50 +02:00
|
|
|
|
++ "expected \"md5\", \"sha1\", \"sha256\", or \"sha512\", got " ++ show algo
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
placeHolder :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-21 07:54:15 +01:00
|
|
|
|
placeHolder = fromValue >=> fromStringNoContext >=> \t -> do
|
2018-11-19 13:04:08 +01:00
|
|
|
|
h <- runPrim (hashString (principledMakeNixStringWithoutContext "sha256")
|
2018-12-05 14:43:31 +01:00
|
|
|
|
(principledMakeNixStringWithoutContext ("nix-output:" <> t)))
|
2019-03-10 04:35:19 +01:00
|
|
|
|
toNix $ principledMakeNixStringWithoutContext $ Text.cons '/' $ printHashBytes32 $
|
2018-12-05 14:43:31 +01:00
|
|
|
|
-- The result coming out of hashString is base16 encoded
|
|
|
|
|
fst $ Base16.decode $ encodeUtf8 $ principledStringIgnoreContext h
|
2018-04-29 00:35:01 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
absolutePathFromValue :: MonadBuiltins e t f m => NValue t f m -> m FilePath
|
2018-04-07 21:02:50 +02:00
|
|
|
|
absolutePathFromValue = \case
|
2018-05-08 22:47:50 +02:00
|
|
|
|
NVStr ns -> do
|
2018-09-09 17:01:09 +02:00
|
|
|
|
let path = Text.unpack $ hackyStringIgnoreContext ns
|
2018-04-07 21:02:50 +02:00
|
|
|
|
unless (isAbsolute path) $
|
2018-05-02 02:33:17 +02:00
|
|
|
|
throwError $ ErrorCall $ "string " ++ show path ++ " doesn't represent an absolute path"
|
2018-04-07 21:02:50 +02:00
|
|
|
|
pure path
|
2018-04-07 23:52:24 +02:00
|
|
|
|
NVPath path -> pure path
|
2018-05-02 02:33:17 +02:00
|
|
|
|
v -> throwError $ ErrorCall $ "expected a path, got " ++ show v
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
readFile_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
readFile_ path =
|
2018-04-24 11:14:27 +02:00
|
|
|
|
path >>= absolutePathFromValue >>= Nix.Render.readFile >>= toNix
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
findFile_ :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-05-20 15:47:53 +02:00
|
|
|
|
findFile_ aset filePath =
|
|
|
|
|
aset >>= \aset' ->
|
|
|
|
|
filePath >>= \filePath' ->
|
|
|
|
|
case (aset', filePath') of
|
2018-07-28 19:23:23 +02:00
|
|
|
|
(NVList x, NVStr ns) -> do
|
2019-03-15 22:33:03 +01:00
|
|
|
|
mres <- findPath @t @f @m x (Text.unpack (hackyStringIgnoreContext ns))
|
2018-05-20 15:47:53 +02:00
|
|
|
|
pure $ nvPath mres
|
|
|
|
|
(NVList _, y) -> throwError $ ErrorCall $ "expected a string, got " ++ show y
|
2018-07-28 21:32:15 +02:00
|
|
|
|
(x, NVStr _) -> throwError $ ErrorCall $ "expected a list, got " ++ show x
|
2018-05-20 15:47:53 +02:00
|
|
|
|
(x, y) -> throwError $ ErrorCall $ "Invalid types for builtins.findFile: " ++ show (x, y)
|
|
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
|
data FileType
|
2018-04-22 23:32:55 +02:00
|
|
|
|
= FileTypeRegular
|
|
|
|
|
| FileTypeDirectory
|
|
|
|
|
| FileTypeSymlink
|
|
|
|
|
| FileTypeUnknown
|
2018-04-07 21:02:50 +02:00
|
|
|
|
deriving (Show, Read, Eq, Ord)
|
|
|
|
|
|
2019-03-16 01:20:10 +01:00
|
|
|
|
instance Convertible e t f m => ToNix FileType m (NValue t f m) where
|
2018-11-19 13:04:08 +01:00
|
|
|
|
toNix = toNix . principledMakeNixStringWithoutContext . \case
|
2018-04-22 23:32:55 +02:00
|
|
|
|
FileTypeRegular -> "regular" :: Text
|
|
|
|
|
FileTypeDirectory -> "directory"
|
|
|
|
|
FileTypeSymlink -> "symlink"
|
|
|
|
|
FileTypeUnknown -> "unknown"
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
readDir_ :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
readDir_ pathThunk = do
|
2018-04-17 06:39:41 +02:00
|
|
|
|
path <- absolutePathFromValue =<< pathThunk
|
2018-04-07 21:02:50 +02:00
|
|
|
|
items <- listDirectory path
|
|
|
|
|
itemsWithTypes <- forM items $ \item -> do
|
2018-11-16 21:16:17 +01:00
|
|
|
|
s <- getSymbolicLinkStatus $ path </> item
|
2018-04-07 21:02:50 +02:00
|
|
|
|
let t = if
|
2018-04-22 23:32:55 +02:00
|
|
|
|
| isRegularFile s -> FileTypeRegular
|
|
|
|
|
| isDirectory s -> FileTypeDirectory
|
|
|
|
|
| isSymbolicLink s -> FileTypeSymlink
|
|
|
|
|
| otherwise -> FileTypeUnknown
|
2018-04-07 21:02:50 +02:00
|
|
|
|
pure (Text.pack item, t)
|
2018-04-16 07:01:01 +02:00
|
|
|
|
toNix (M.fromList itemsWithTypes)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-17 00:23:40 +01:00
|
|
|
|
fromJSON :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 22:33:03 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
fromJSON = fromValue >=> fromStringNoContext >=> \encoded ->
|
2018-04-07 21:02:50 +02:00
|
|
|
|
case A.eitherDecodeStrict' @A.Value $ encodeUtf8 encoded of
|
2018-05-02 02:33:17 +02:00
|
|
|
|
Left jsonError ->
|
|
|
|
|
throwError $ ErrorCall $ "builtins.fromJSON: " ++ jsonError
|
2018-12-05 20:59:42 +01:00
|
|
|
|
Right v -> jsonToNValue v
|
|
|
|
|
where
|
|
|
|
|
jsonToNValue = \case
|
|
|
|
|
A.Object m -> flip nvSet M.empty
|
|
|
|
|
<$> traverse (thunk . jsonToNValue) m
|
|
|
|
|
A.Array l -> nvList <$>
|
2019-03-15 22:33:03 +01:00
|
|
|
|
traverse (\x -> thunk @t @m @(NValue t f m)
|
2019-03-16 01:20:10 +01:00
|
|
|
|
. whileForcingThunk @t @f (CoercionFromJson @t @f @m x)
|
2019-03-15 22:33:03 +01:00
|
|
|
|
. jsonToNValue $ x)
|
|
|
|
|
(V.toList l)
|
2018-12-05 20:59:42 +01:00
|
|
|
|
A.String s -> pure $ nvStr $ hackyMakeNixStringWithoutContext s
|
|
|
|
|
A.Number n -> pure $ nvConstant $ case floatingOrInteger n of
|
|
|
|
|
Left r -> NFloat r
|
|
|
|
|
Right i -> NInt i
|
|
|
|
|
A.Bool b -> pure $ nvConstant $ NBool b
|
|
|
|
|
A.Null -> pure $ nvConstant NNull
|
|
|
|
|
|
|
|
|
|
prim_toJSON
|
2019-03-15 22:33:03 +01:00
|
|
|
|
:: MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m)
|
|
|
|
|
-> m (NValue t f m)
|
2018-12-13 00:54:17 +01:00
|
|
|
|
prim_toJSON x = x >>= nvalueToJSONNixString >>= pure . nvStr
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
toXML_ :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-23 18:47:56 +01:00
|
|
|
|
toXML_ v = v >>= normalForm >>= pure . nvStr . toXML
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
typeOf :: MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-11-19 13:04:08 +01:00
|
|
|
|
typeOf v = v >>= toNix . principledMakeNixStringWithoutContext . \case
|
2018-04-07 21:02:50 +02:00
|
|
|
|
NVConstant a -> case a of
|
2018-04-16 07:01:01 +02:00
|
|
|
|
NInt _ -> "int"
|
2018-04-07 21:02:50 +02:00
|
|
|
|
NFloat _ -> "float"
|
2018-04-16 07:01:01 +02:00
|
|
|
|
NBool _ -> "bool"
|
|
|
|
|
NNull -> "null"
|
2018-07-28 19:23:23 +02:00
|
|
|
|
NVStr _ -> "string"
|
2018-04-16 07:01:01 +02:00
|
|
|
|
NVList _ -> "list"
|
|
|
|
|
NVSet _ _ -> "set"
|
|
|
|
|
NVClosure {} -> "lambda"
|
|
|
|
|
NVPath _ -> "path"
|
2018-04-07 21:02:50 +02:00
|
|
|
|
NVBuiltin _ _ -> "lambda"
|
2018-04-22 23:32:55 +02:00
|
|
|
|
_ -> error "Pattern synonyms obscure complete patterns"
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
tryEval :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
tryEval e = catch (onSuccess <$> e) (pure . onError)
|
2018-04-07 23:33:15 +02:00
|
|
|
|
where
|
2018-04-22 23:32:55 +02:00
|
|
|
|
onSuccess v = flip nvSet M.empty $ M.fromList
|
|
|
|
|
[ ("success", valueThunk (nvConstant (NBool True)))
|
2018-04-07 23:33:15 +02:00
|
|
|
|
, ("value", valueThunk v)
|
|
|
|
|
]
|
|
|
|
|
|
2019-03-15 21:27:05 +01:00
|
|
|
|
onError :: SomeException -> NValue t f m
|
2018-04-22 23:32:55 +02:00
|
|
|
|
onError _ = flip nvSet M.empty $ M.fromList
|
|
|
|
|
[ ("success", valueThunk (nvConstant (NBool False)))
|
|
|
|
|
, ("value", valueThunk (nvConstant (NBool False)))
|
2018-04-07 23:33:15 +02:00
|
|
|
|
]
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
trace_ :: forall e t f m. MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-28 23:28:16 +02:00
|
|
|
|
trace_ msg action = do
|
2019-03-15 22:33:03 +01:00
|
|
|
|
traceEffect @t @f @m
|
|
|
|
|
. Text.unpack
|
|
|
|
|
. principledStringIgnoreContext
|
|
|
|
|
=<< fromValue msg
|
2018-04-28 23:28:16 +02:00
|
|
|
|
action
|
|
|
|
|
|
2018-09-09 17:01:09 +02:00
|
|
|
|
-- TODO: remember error context
|
2019-03-15 22:33:03 +01:00
|
|
|
|
addErrorContext :: forall e t f m. MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-09-09 17:01:09 +02:00
|
|
|
|
addErrorContext _ action = action
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
exec_ :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-04-29 01:37:01 +02:00
|
|
|
|
exec_ xs = do
|
2019-03-15 21:27:05 +01:00
|
|
|
|
ls <- fromValue @[t] xs
|
2018-11-19 13:04:08 +01:00
|
|
|
|
xs <- traverse (coerceToString DontCopyToStore CoerceStringy <=< force') ls
|
|
|
|
|
-- TODO Still need to do something with the context here
|
|
|
|
|
-- See prim_exec in nix/src/libexpr/primops.cc
|
2018-12-05 14:43:31 +01:00
|
|
|
|
-- Requires the implementation of EvalState::realiseContext
|
2018-11-21 07:55:56 +01:00
|
|
|
|
exec (map (Text.unpack . hackyStringIgnoreContext) xs)
|
2018-04-29 01:37:01 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
fetchurl :: forall e t f m. MonadBuiltins e t f m => m (NValue t f m) -> m (NValue t f m)
|
2018-05-03 06:38:13 +02:00
|
|
|
|
fetchurl v = v >>= \case
|
|
|
|
|
NVSet s _ -> attrsetGet "url" s >>= force ?? (go (M.lookup "sha256" s))
|
|
|
|
|
v@NVStr {} -> go Nothing v
|
|
|
|
|
v -> throwError $ ErrorCall $ "builtins.fetchurl: Expected URI or set, got "
|
|
|
|
|
++ show v
|
|
|
|
|
where
|
2019-03-15 21:27:05 +01:00
|
|
|
|
go :: Maybe t -> NValue t f m -> m (NValue t f m)
|
2018-05-03 06:39:23 +02:00
|
|
|
|
go _msha = \case
|
2019-03-09 21:15:34 +01:00
|
|
|
|
NVStr ns -> noContextAttrs ns >>= getURL >>= \case -- msha
|
2018-11-16 23:06:34 +01:00
|
|
|
|
Left e -> throwError e
|
|
|
|
|
Right p -> toValue p
|
2018-05-03 06:39:23 +02:00
|
|
|
|
v -> throwError $ ErrorCall $
|
2019-03-09 21:15:34 +01:00
|
|
|
|
"builtins.fetchurl: Expected URI or string, got " ++ show v
|
|
|
|
|
|
|
|
|
|
noContextAttrs ns = case principledGetStringNoContext ns of
|
|
|
|
|
Nothing -> throwError $ ErrorCall $
|
|
|
|
|
"builtins.fetchurl: unsupported arguments to url"
|
|
|
|
|
Just t -> pure t
|
2018-05-03 06:38:13 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
partition_ :: forall e t f m. MonadBuiltins e t f m
|
2019-03-15 21:27:05 +01:00
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m) -> m (NValue t f m)
|
2018-04-18 02:46:27 +02:00
|
|
|
|
partition_ fun xs = fun >>= \f ->
|
2019-03-15 21:27:05 +01:00
|
|
|
|
fromValue @[t] xs >>= \l -> do
|
2018-04-26 05:46:31 +02:00
|
|
|
|
let match t = f `callFunc` force' t >>= fmap (, t) . fromValue
|
2018-04-18 02:46:27 +02:00
|
|
|
|
selection <- traverse match l
|
|
|
|
|
let (right, wrong) = partition fst selection
|
2018-04-22 23:32:55 +02:00
|
|
|
|
let makeSide = valueThunk . nvList . map snd
|
2019-03-15 21:27:05 +01:00
|
|
|
|
toValue @(AttrSet t) $
|
2018-04-18 02:46:27 +02:00
|
|
|
|
M.fromList [("right", makeSide right), ("wrong", makeSide wrong)]
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
currentSystem :: MonadBuiltins e t f m => m (NValue t f m)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
currentSystem = do
|
|
|
|
|
os <- getCurrentSystemOS
|
|
|
|
|
arch <- getCurrentSystemArch
|
2019-03-09 05:22:43 +01:00
|
|
|
|
return $ nvStr $ principledMakeNixStringWithoutContext (arch <> "-" <> os)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
currentTime_ :: MonadBuiltins e t f m => m (NValue t f m)
|
2018-05-03 06:32:00 +02:00
|
|
|
|
currentTime_ = do
|
|
|
|
|
opts :: Options <- asks (view hasLens)
|
|
|
|
|
toNix @Integer $ round $ Time.utcTimeToPOSIXSeconds (currentTime opts)
|
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
derivationStrict_ :: MonadBuiltins e t f m
|
|
|
|
|
=> m (NValue t f m) -> m (NValue t f m)
|
2018-04-17 06:39:41 +02:00
|
|
|
|
derivationStrict_ = (>>= derivationStrict)
|
2018-04-08 00:34:54 +02:00
|
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
|
newtype Prim m a = Prim { runPrim :: m a }
|
|
|
|
|
|
|
|
|
|
-- | Types that support conversion to nix in a particular monad
|
2019-03-15 22:33:03 +01:00
|
|
|
|
class ToBuiltin t f m a | a -> m where
|
2019-03-15 21:27:05 +01:00
|
|
|
|
toBuiltin :: String -> a -> m (NValue t f m)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
instance (MonadBuiltins e t f m, ToNix a m (NValue t f m))
|
|
|
|
|
=> ToBuiltin t f m (Prim m a) where
|
2018-04-16 05:43:04 +02:00
|
|
|
|
toBuiltin _ p = toNix =<< runPrim p
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
2019-03-15 22:33:03 +01:00
|
|
|
|
instance ( MonadBuiltins e t f m
|
|
|
|
|
, FromNix a m (NValue t f m)
|
|
|
|
|
, ToBuiltin t f m b)
|
|
|
|
|
=> ToBuiltin t f m (a -> b) where
|
2019-03-16 01:20:10 +01:00
|
|
|
|
toBuiltin name f = return $ nvBuiltin name
|
|
|
|
|
(fromNix >=> fmap wrapValue . toBuiltin name . f)
|