module Nix.Expr.Additions
( stringKey, ($$=), dynamicKey
, simpleParamSet, multiParam
, (!!.)
, StrQ(..), mkStrQ, mkStrQI
) where
import Data.Fix (Fix(..))
import Data.Text (Text)
import Data.String (IsString(..))
import Nix.Expr
import Text.Regex.TDFA.Text ()
import Text.Regex.TDFA ((=~))
stringKey :: Text -> NExpr -> Binding NExpr
stringKey k v = NamedVar [dynamicKey k] v
($$=) :: Text -> NExpr -> Binding NExpr
($$=) = stringKey
infixr 2 $$=
dynamicKey :: Text -> NKeyName NExpr
dynamicKey k = DynamicKey $ Plain $ DoubleQuoted [Plain k]
simpleParamSet :: [Text] -> Params NExpr
simpleParamSet = mkParamset . fmap (, Nothing)
multiParam :: [Text] -> NExpr -> NExpr
multiParam ps expr = foldr mkFunction expr $ map Param ps
(!!.) :: NExpr -> Text -> NExpr
aset !!. k = Fix
$ NSelect aset
[(if isPlainSymbol k then StaticKey else dynamicKey) k] Nothing
where
isPlainSymbol :: Text -> Bool
isPlainSymbol s = s =~ ("^[a-zA-Z_][a-zA-Z0-9_'-]*$" :: Text)
infixl 8 !!.
data StrQ = StrQ !Text | AntiQ !NExpr
instance IsString StrQ where
fromString = StrQ . fromString
mkStrQtmpl :: ([Antiquoted Text NExpr] -> NString NExpr) -> [StrQ] -> NExpr
mkStrQtmpl strtr = Fix . NStr . strtr . map trans
where trans (StrQ t) = Plain t
trans (AntiQ r) = Antiquoted r
mkStrQ, mkStrQI :: [StrQ] -> NExpr
mkStrQ = mkStrQtmpl DoubleQuoted
mkStrQI = mkStrQtmpl Indented