Add makeAbsolutePath to MonadNix

This commit is contained in:
Ryan Trinkle 2018-03-31 16:23:14 -04:00
parent 833e307e52
commit d655e74d67
3 changed files with 12 additions and 1 deletions

View file

@ -49,7 +49,7 @@ eval (NSym var) = do
eval (NConstant x) = return $ NVConstant x
eval (NStr str) = evalString str
eval (NLiteralPath p) = return $ NVLiteralPath p
eval (NLiteralPath p) = NVLiteralPath <$> makeAbsolutePath p
eval (NEnvPath p) = return $ NVEnvPath p
eval (NUnary op arg) = arg >>= \case

View file

@ -144,6 +144,9 @@ class (Show (NScopes m), MonadFix m) => MonadNix m where
-- | Import a path into the nix store, and return the resulting path
addPath :: FilePath -> m StorePath
-- | Determine the absolute path of relative path in the current context
makeAbsolutePath :: FilePath -> m FilePath
deferInScope :: MonadNix m
=> NScopes m -> m (NValue m) -> m (NThunk m)
deferInScope scope = buildThunk . clearScopes . pushScopes scope

View file

@ -25,6 +25,7 @@ import Nix.Parser
import Nix.Pretty
import Nix.Scope
import Nix.Utils
import System.Directory
import System.Environment
import System.Exit (ExitCode (ExitSuccess))
import System.FilePath
@ -117,6 +118,13 @@ instance MonadNix (Cyclic IO) where
return $ StorePath $ dropTrailingLinefeed out
_ -> error $ "No such file or directory: " ++ show path
makeAbsolutePath p = if isAbsolute p then pure p else do
cwd <- lookupVar "__cwd" >>= \case
Nothing -> liftIO getCurrentDirectory
Just (NVLiteralPath s) -> return s
Just v -> throwError $ "when resolving relative path, __cwd is in scope, but is not a path; it is: " ++ show (void v)
liftIO $ canonicalizePath $ cwd </> p
data NThunk (Cyclic IO) = NThunkIO (IORef (Deferred (Cyclic IO)))
valueRef value =