add builtins.toFile

This commit is contained in:
hsloan 2018-05-10 20:32:47 -04:00
parent 13e66bfc8e
commit f7ff858ef9
3 changed files with 17 additions and 0 deletions

View file

@ -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

View file

@ -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)

View file

@ -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