add builtins.toFile
This commit is contained in:
parent
13e66bfc8e
commit
f7ff858ef9
|
@ -194,6 +194,7 @@ builtinsList = sequence [
|
|||
, add' Normal "toJSON"
|
||||
(arity1 $ decodeUtf8 . LBS.toStrict . A.encodingToLazyByteString
|
||||
. toEncodingSorted)
|
||||
, add2 Normal "toFile" toFile
|
||||
, add Normal "toPath" toPath
|
||||
, add TopLevel "toString" toString
|
||||
, add Normal "toXML" toXML_
|
||||
|
@ -660,6 +661,13 @@ functionArgs fun = fun >>= \case
|
|||
v -> throwError $ ErrorCall $
|
||||
"builtins.functionArgs: expected function, got " ++ show v
|
||||
|
||||
toFile :: MonadNix e m => m (NValue m) -> m (NValue m) -> m (NValue m)
|
||||
toFile name s = do
|
||||
name' <- fromValue name
|
||||
s' <- fromValue s
|
||||
mres <- toFile_ (Text.unpack name') (Text.unpack s')
|
||||
toNix $ Text.pack $ unStorePath mres
|
||||
|
||||
toPath :: MonadNix e m => m (NValue m) -> m (NValue m)
|
||||
toPath = fromValue @Path >=> toNix @Path
|
||||
|
||||
|
|
|
@ -13,6 +13,8 @@ class MonadFile m => MonadEffects m where
|
|||
-- | Import a path into the nix store, and return the resulting path
|
||||
addPath :: FilePath -> m StorePath
|
||||
|
||||
toFile_ :: FilePath -> String -> m StorePath
|
||||
|
||||
-- | Determine the absolute path of relative path in the current context
|
||||
makeAbsolutePath :: FilePath -> m FilePath
|
||||
findEnvPath :: String -> m FilePath
|
||||
|
@ -38,3 +40,4 @@ class MonadFile m => MonadEffects m where
|
|||
traceEffect :: String -> m ()
|
||||
|
||||
exec :: [String] -> m (NValue m)
|
||||
|
||||
|
|
|
@ -483,6 +483,12 @@ instance (MonadFix m, MonadCatch m, MonadIO m, Alternative m,
|
|||
_ -> throwError $ ErrorCall $
|
||||
"addPath: failed: nix-store --add " ++ show path
|
||||
|
||||
toFile_ filepath content = do
|
||||
liftIO $ writeFile filepath content
|
||||
storepath <- addPath filepath
|
||||
liftIO $ removeFile filepath
|
||||
return storepath
|
||||
|
||||
makeAbsolutePath origPath = do
|
||||
origPathExpanded <- liftIO $ expandHomePath origPath
|
||||
absPath <- if isAbsolute origPathExpanded then pure origPathExpanded else do
|
||||
|
|
Loading…
Reference in a new issue