1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE ConstraintKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE Rank2Types #-}
8{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE TemplateHaskell #-}
10{-# LANGUAGE TypeApplications #-}
11module Obelisk.Command.Nix
16 , nixCmdConfig_builders
18 , nixBuildConfig_common
19 , nixBuildConfig_outLink
20 , NixInstantiateConfig (..)
21 , nixInstantiateConfig_eval
22 , NixCommonConfig (..)
31import Control.Monad (guard)
34import Data.Bool (bool)
36import Data.List (intercalate)
38import Data.Monoid ((<>))
39import qualified Data.Text as T
40import System.Process (proc)
42import Obelisk.App (MonadObelisk)
47 { _target_path :: Maybe FilePath
48 , _target_attr :: Maybe String
49 , _target_expr :: Maybe String
53instance Default Target where
55 { _target_path = Just "."
56 , _target_attr = Nothing
57 , _target_expr = Nothing
61 = Arg_Str String String
62 | Arg_Expr String String
65strArg :: String -> String -> Arg
68rawArg :: String -> String -> Arg
71boolArg :: String -> Bool -> Arg
72boolArg k = Arg_Expr k . bool "false" "true"
74cliFromArgs :: [Arg] -> [String]
75cliFromArgs = concatMap $ \case
76 Arg_Str k v -> ["--argstr", k, v]
77 Arg_Expr k v -> ["--arg", k, v]
79data NixCommonConfig = NixCommonConfig
80 { _nixCmdConfig_target :: Target
81 , _nixCmdConfig_args :: [Arg]
82 , _nixCmdConfig_builders :: [String]
84makeClassy ''NixCommonConfig
86instance Default NixCommonConfig where
87 def = NixCommonConfig def mempty mempty
89runNixCommonConfig :: NixCommonConfig -> [FilePath]
90runNixCommonConfig cfg = mconcat [maybeToList path, attrArg, exprArg, args, buildersArg]
92 path = _target_path $ _nixCmdConfig_target cfg
93 attr = _target_attr $ _nixCmdConfig_target cfg
94 expr = _target_expr $ _nixCmdConfig_target cfg
95 attrArg = case attr of
98 exprArg = case expr of
101 args = cliFromArgs $ _nixCmdConfig_args cfg
102 buildersArg = case _nixCmdConfig_builders cfg of
104 builders -> ["--builders", intercalate ";" builders]
106-- | Where to put nix-build output
110 | OutLink_IndirectRoot FilePath
112instance Default OutLink where
113 def = OutLink_Default
115data NixBuildConfig = NixBuildConfig
116 { _nixBuildConfig_common :: NixCommonConfig
117 , _nixBuildConfig_outLink :: OutLink
119makeLenses ''NixBuildConfig
121instance HasNixCommonConfig NixBuildConfig where
122 nixCommonConfig = nixBuildConfig_common
124instance Default NixBuildConfig where
125 def = NixBuildConfig def def
127runNixBuildConfig :: NixBuildConfig -> [FilePath]
128runNixBuildConfig cfg = mconcat
129 [ runNixCommonConfig $ cfg ^. nixCommonConfig
130 , case _nixBuildConfig_outLink cfg of
131 OutLink_Default -> []
132 OutLink_None -> ["--no-out-link"]
133 OutLink_IndirectRoot l -> ["--out-link", l]
136data NixInstantiateConfig = NixInstantiateConfig
137 { _nixInstantiateConfig_common :: NixCommonConfig
138 , _nixInstantiateConfig_eval :: Bool
140makeLenses ''NixInstantiateConfig
142instance HasNixCommonConfig NixInstantiateConfig where
143 nixCommonConfig = nixInstantiateConfig_common
145instance Default NixInstantiateConfig where
146 def = NixInstantiateConfig def False
148runNixInstantiateConfig :: NixInstantiateConfig -> [FilePath]
149runNixInstantiateConfig cfg = mconcat
150 [ runNixCommonConfig $ cfg ^. nixCommonConfig
151 , "--eval" <$ guard (_nixInstantiateConfig_eval cfg)
155 = NixCmd_Build NixBuildConfig
156 | NixCmd_Instantiate NixInstantiateConfig
158instance Default NixCmd where
159 def = NixCmd_Build def
161nixCmd :: MonadObelisk m => NixCmd -> m FilePath
162nixCmd cmdCfg = withSpinner' ("Running " <> cmd <> desc) (Just $ const $ "Built " <> desc) $ do
163 output <- readProcessAndLogStderr Debug $ proc (T.unpack cmd) $ options
164 -- Remove final newline that Nix appends
165 Just (outPath, '\n') <- pure $ T.unsnoc output
166 pure $ T.unpack outPath
168 (cmd, options, commonCfg) = case cmdCfg of
171 , runNixBuildConfig cfg'
172 , cfg' ^. nixCommonConfig
174 NixCmd_Instantiate cfg' ->
176 , runNixInstantiateConfig cfg'
177 , cfg' ^. nixCommonConfig
179 path = commonCfg ^. nixCmdConfig_target . target_path
180 desc = T.pack $ mconcat $ catMaybes
181 [ (" on " <>) <$> path
182 , (\a -> " [" <> a <> "]") <$> (commonCfg ^. nixCmdConfig_target . target_attr)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list