on master help sign in
Ensure inNixShell is run from within an obelisk project
commitacc03c3684fde7421b542dafd2938932ad3d7ed9 authorLuigy Leon <luigy@outlook.com> dateFri 4 May 2018, 12:34 (8 years ago) parent8865f04
2 files changed+24−18
lib/command/src/Obelisk/Command.hs+2−2 lib/command/src/Obelisk/Command/Project.hs+22−16
lib/command/src/Obelisk/Command.hs+2−2
@@ -138,9 +138,9 @@ data ObInternal
138138 = ObInternal_Daemon
139139
140140inNixShell :: Closure (Process ()) -> IO ()
141−inNixShell m = do
141+inNixShell m = withProjectRoot "." $ \root -> do
142142 progName <- getProgName
143− _ <- forkIO $ inImpureProjectShell "ghc" $ unwords [progName, "internal", "daemon"]
143+ _ <- forkIO $ projectShell root False "ghc" $ unwords [progName, "internal", "daemon"]
144144 backend <- initializeBackend "127.0.0.1" "0" obRemoteTable
145145 backendNode <- newLocalNode backend
146146 startMaster backend (daemonMaster backend m) `finally`
lib/command/src/Obelisk/Command/Project.hs+22−16
@@ -5,8 +5,10 @@ module Obelisk.Command.Project
55 , initProject
66 , findProjectObeliskCommand
77 , findProjectRoot
8+ , withProjectRoot
89 , inProjectShell
910 , inImpureProjectShell
11+ , projectShell
1012 ) where
1113
1214import Control.Monad
@@ -110,6 +112,11 @@ findProjectRoot target = do
110112 (result, _) <- runStateT (walkToProjectRoot target targetStat myUid) []
111113 return result
112114
115+withProjectRoot :: FilePath -> (FilePath -> IO ()) -> IO ()
116+withProjectRoot target f = findProjectRoot target >>= \case
117+ Nothing -> fail "Must be used inside of an Obelisk project"
118+ Just root -> f root
119+
113120-- | Walk from the current directory to the containing project's root directory,
114121-- if there is one, accumulating potentially insecure directories that were
115122-- traversed in the process. Return the project root directory, if found.
@@ -153,24 +160,23 @@ isWritableOnlyBy s uid = fileOwner s == uid && fileMode s .&. 0o22 == 0
153160
154161-- | Run a command in the given shell for the current project
155162inProjectShell :: String -> String -> IO ()
156−inProjectShell = inProjectShell' ["--pure"]
157−
158−inProjectShell' :: [String] -> String -> String -> IO ()
159−inProjectShell' args shellName command = do
160− findProjectRoot "." >>= \case
161− Nothing -> putStrLn "Must be used inside of an Obelisk project"
162− Just root -> do
163− (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setCwd (Just root) $ proc "nix-shell" $
164− args <>
165− [ "-A"
166− , "shells." <> shellName
167− , "--run", command
168− ]
169− _ <- waitForProcess ph
170− return ()
163+inProjectShell shellName command = withProjectRoot "." $ \root ->
164+ projectShell root True shellName command
171165
172166inImpureProjectShell :: String -> String -> IO ()
173−inImpureProjectShell = inProjectShell' []
167+inImpureProjectShell shellName command = withProjectRoot "." $ \root ->
168+ projectShell root False shellName command
169+
170+projectShell :: FilePath -> Bool -> String -> String -> IO ()
171+projectShell root isPure shellName command = do
172+ (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setCwd (Just root) $ proc "nix-shell" $
173+ [ "--pure" | isPure ] <>
174+ [ "-A"
175+ , "shells." <> shellName
176+ , "--run", command
177+ ]
178+ _ <- waitForProcess ph
179+ return ()
174180
175181setCwd :: Maybe FilePath -> CreateProcess -> CreateProcess
176182setCwd fp cp = cp { cwd = fp }