@@ -6,6 +6,9 @@ module Obelisk.Command.Project
66 , findProjectObeliskCommand
77 , findProjectRoot
88 , initProject
9+ , nixShellRunConfig
10+ , nixShellRunProc
11+ , nixShellWithHoogle
912 , nixShellWithPkgs
1013 , obeliskDirName
1114 , projectShell
@@ -253,50 +256,54 @@ toNixPath :: FilePath -> FilePath
253256toNixPath root | "/" `isInfixOf` root = root
254257 | otherwise = "./" <> root
255258
259+nixShellRunConfig :: MonadObelisk m => FilePath -> Bool -> Maybe String -> m NixShellConfig
260+nixShellRunConfig root isPure command = do
261+ nixpkgsPath <- fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $
262+ proc nixExePath ["eval", "(import .obelisk/impl {}).nixpkgs.path"]
263+ nixRemote <- liftIO $ lookupEnv "NIX_REMOTE"
264+ pure $ def
265+ & nixShellConfig_pure .~ isPure
266+ & nixShellConfig_common . nixCmdConfig_target .~ (def & target_path .~ Nothing)
267+ & nixShellConfig_run .~ (command <&> \c -> mconcat
268+ [ "export NIX_PATH=nixpkgs=", T.unpack nixpkgsPath, "; "
269+ , maybe "" (\v -> "export NIX_REMOTE=" <> v <> "; ") nixRemote
270+ , c
271+ ])
272+
273+nixShellRunProc :: NixShellConfig -> ProcessSpec
274+nixShellRunProc cfg = setDelegateCtlc True $ proc "nix-shell" $ runNixShellConfig cfg
275+
256276nixShellWithPkgs :: MonadObelisk m => FilePath -> Bool -> Bool -> Map Text FilePath -> Maybe String -> m ()
257277nixShellWithPkgs root isPure chdirToRoot packageNamesAndPaths command = do
258278 packageNamesAndAbsPaths <- liftIO $ for packageNamesAndPaths makeAbsolute
259− nixpkgsPath <- fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $
260− proc nixExePath ["eval", "(import .obelisk/impl {}).nixpkgs.path"]
279+ defShellConfig <- nixShellRunConfig root isPure command
261280 let setCwd_ = if chdirToRoot then setCwd (Just root) else id
262− nixRemote <- liftIO $ lookupEnv "NIX_REMOTE"
263− (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setDelegateCtlc True $ setCwd_ $ proc "nix-shell" $
264− runNixShellConfig $ def
265− & nixShellConfig_pure .~ isPure
266− & nixShellConfig_common . nixCmdConfig_target .~ (def
267− & target_path .~ Nothing
268− & target_expr ?~
269− "{root, pkgs}: ((import root {}).passthru.__unstable__.self.extend (self: super: {\
270− \shellPackages = builtins.fromJSON pkgs; })\
271− \).project.shells.ghc"
272− )
273− & nixShellConfig_common . nixCmdConfig_args .~
274− [ rawArg "root" $ toNixPath $ if chdirToRoot then "." else root
275− , strArg "pkgs" (T.unpack $ decodeUtf8 $ BSL.toStrict $ Json.encode packageNamesAndAbsPaths)
276− ]
277− & nixShellConfig_run .~ (command <&> \c -> mconcat
278− [ "export NIX_PATH=nixpkgs=", T.unpack nixpkgsPath, "; "
279− , maybe "" (\v -> "export NIX_REMOTE=" <> v <> "; ") nixRemote
280− , c
281− ])
281+ (_, _, _, ph) <- createProcess_ "nixShellWithPkgs" $ setCwd_ $ nixShellRunProc $ defShellConfig
282+ & nixShellConfig_common . nixCmdConfig_target . target_expr ?~
283+ "{root, pkgs}: ((import root {}).passthru.__unstable__.self.extend (_: _: {\
284+ \shellPackages = builtins.fromJSON pkgs;\
285+ \})).project.shells.ghc"
286+ & nixShellConfig_common . nixCmdConfig_args .~
287+ [ rawArg "root" $ toNixPath $ if chdirToRoot then "." else root
288+ , strArg "pkgs" (T.unpack $ decodeUtf8 $ BSL.toStrict $ Json.encode packageNamesAndAbsPaths)
289+ ]
290+ void $ liftIO $ waitForProcess ph
291+
292+nixShellWithHoogle :: MonadObelisk m => FilePath -> Bool -> String -> Maybe String -> m ()
293+nixShellWithHoogle root isPure shell' command = do
294+ defShellConfig <- nixShellRunConfig root isPure command
295+ (_, _, _, ph) <- createProcess_ "nixShellWithHoogle" $ setCwd (Just root) $ nixShellRunProc $ defShellConfig
296+ & nixShellConfig_common . nixCmdConfig_target . target_expr ?~
297+ "{shell}: ((import ./. {}).passthru.__unstable__.self.extend (_: super: {\
298+ \userSettings = super.userSettings // { withHoogle = true; };\
299+ \})).project.shells.${shell}"
300+ & nixShellConfig_common . nixCmdConfig_args .~ [ strArg "shell" shell' ]
282301 void $ liftIO $ waitForProcess ph
283302
284303projectShell :: MonadObelisk m => FilePath -> Bool -> String -> Maybe String -> m ()
285304projectShell root isPure shellName command = do
286− nixpkgsPath <- fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $ proc nixExePath ["eval", "(import .obelisk/impl {}).nixpkgs.path"]
287− nixRemote <- liftIO $ lookupEnv "NIX_REMOTE"
288− (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setDelegateCtlc True $ setCwd (Just root) $ proc "nix-shell" $
289− [ "default.nix"] <>
290− ["--pure" | isPure] <>
291− [ "-A"
292− , "shells." <> shellName
293− ] <> case command of
294− Nothing -> []
295− Just c ->
296− -- TODO: Escape nixpkgsPath and nixRemote
297− [ "--run"
298− , "export NIX_PATH=nixpkgs=" <> T.unpack nixpkgsPath <> "; " <>
299− maybe "" (\v -> "export NIX_REMOTE=" <> v <> "; ") nixRemote <>
300− c
301− ]
305+ defShellConfig <- nixShellRunConfig root isPure command
306+ (_, _, _, ph) <- createProcess_ "runNixShellAttr" $ setCwd (Just root) $ nixShellRunProc $ defShellConfig
307+ & nixShellConfig_common . nixCmdConfig_target . target_path ?~ "default.nix"
308+ & nixShellConfig_common . nixCmdConfig_target . target_attr ?~ ("shells." <> shellName)
302309 void $ liftIO $ waitForProcess ph