aboutsummaryrefslogtreecommitdiff
path: root/pkgs/manager/main.hs
blob: 9909419c1783b7ec27556d47a3fa4dfcfa8caa09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
module Main where

import Control.Applicative
import Control.Monad
import Data.Bifunctor
import Data.Foldable
import Data.Function
import Data.Functor
import Data.List qualified as L
import Options.Applicative
import System.FilePath (FilePath)
import System.Process.Typed qualified as P
-- import Turtle



main :: IO ()
main = undefined


run :: CLIArgs -> IO ()
run c = runNix $
    [ case _action c of
        Build _ -> "build"
        Deploy _ -> "run"
    , flakeString (_flake c) (_system c) $ toKoboAttr $ _target . _action c
    ] <> _nixArgs c


flakeArg :: Parser Flake
flakeArg = argument (maybeReader parseFlake) $ fold
    [ metavar "FLAKE"
    , help "Flake location"
    ]

sysOption :: Parser String
sysOption = strOption $ fold
    [ long "system"
    , metavar "SYSTEM"
    , help "e.g. x86_64-linux"
    ]

-- koboConfigFlake :: Flake -> String -> Flake
-- koboConfigFlake (Flake u a) sys = Flake u $ "koboConfigurations" : sys :
--     case a of
--       [] -> [ "default" ]
--       _ -> a


flakeString :: KoboFlake -> NixSystem -> KoboAttr -> String
flakeString (KoboFlake u n) sys attr = fold
    [ q u
    , "#"
    , L.intercalate "." $ q <$> ([ "koboConfigurations", sys, n] <> attr)
    ]
    where
        q s = "\"" <> s <> "\""


type KoboAttr = [String]

type NixSystem = String

data KoboFlake = KoboFlake
    { flakeURI :: !FilePath
    , koboName :: !String
    } deriving stock (Eq, Show)


parseKoboFlake :: String -> Maybe Flake
parseKoboFlake str =
    let (u, a) = second (drop 1) $ break (== '#') str
     in case u of
          [] -> Nothing
          _ -> KoboFlake u $ case a of
                               [] -> "default"
                               _ -> a

toKoboAttr :: Target -> KoboAttr
toKoboAttr = ("build":) . \case
    Firmware -> ["firmware" "deploy"]
    UserSpace -> ["koboroot" "deploy"]
    Info -> ["info"]

type NixArgs = [String]


data Target
    = Firmware
    | UserSpace
    | Info
    deriving (Eq, Show)

data Action
    = Build { _target :: !Target }
    | Deploy { _target :: !Target }
    deriving stock (Eq, Show)


data CLIArgs = CLIArgs
    { _action :: !Action
    , _flake :: !KoboFlake
    , _system :: !String
    , _nixArgs :: ![String]
    } deriving stock (Eq, Show)


runNix :: [String] -> IO ()
runNix = P.runProcess_ . P.proc "nix"