From a99ef161f3d04c04d1d55369d1250634d84a90c3 Mon Sep 17 00:00:00 2001 From: Chris Wells Date: Wed, 11 Sep 2024 12:51:28 -0500 Subject: Starting to write the actual cli manager --- flake.nix | 25 ++++++----------- pkgs/manager/main.hs | 79 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 67 insertions(+), 37 deletions(-) diff --git a/flake.nix b/flake.nix index 73e7ce3..8781602 100644 --- a/flake.nix +++ b/flake.nix @@ -18,13 +18,10 @@ inherit system; overlays = builtins.attrValues self.overlays; }; - # myKobo = self.lib.koboConfiguration { - # inherit pkgs; - # modules = [ ./config ]; - # }; in { - koboConfigurations = { + koboConfigurations = rec { + default = clara; clara = self.lib.koboConfiguration { inherit pkgs; modules = [ ./kobo/clara ]; @@ -41,19 +38,15 @@ nickel-menu plato ; - # firmware-test = pkgs.kobo-firmware { - # hardware = "kobo7"; - # date = "Jun2024"; - # version = "4.38.23038"; - # hash = "sha256-ma9Hf0Hc4hDqf1KQwM6TdX3lZo9HPWMrQqU45eFBScs="; - # }; - # installer = myKobo.config.installer.package; }; - # apps = { - # type = "app"; - # program = "${pkgs.lib.getExe myKobo.config.installer.package} ${myKobo.config.kobo.src}"; - # }; + devShells = { + default = pkgs.mkShellNoCC { + buildInputs = [ + pkgs.kobo-utils + ]; + }; + }; }); } diff --git a/pkgs/manager/main.hs b/pkgs/manager/main.hs index 6eac0a7..9909419 100644 --- a/pkgs/manager/main.hs +++ b/pkgs/manager/main.hs @@ -3,8 +3,10 @@ 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 @@ -16,6 +18,14 @@ 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 @@ -27,44 +37,71 @@ sysOption :: Parser String sysOption = strOption $ fold [ long "system" , metavar "SYSTEM" - , help "Like x86_64-linux" + , help "e.g. x86_64-linux" ] -koboConfigFlake :: Flake -> String -> Flake -koboConfigFlake (Flake u a) sys = Flake u $ "koboConfigurations" : sys : a +-- 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] -data Flake = Flake - { uri :: !FilePath - , attrs :: ![String] +type NixSystem = String + +data KoboFlake = KoboFlake + { flakeURI :: !FilePath + , koboName :: !String } deriving stock (Eq, Show) -parseFlake :: String -> Maybe Flake -parseFlake str = + +parseKoboFlake :: String -> Maybe Flake +parseKoboFlake str = let (u, a) = second (drop 1) $ break (== '#') str in case u of [] -> Nothing - _ -> Flake u $ splitter a - where - splitter xs = - let (y, ys) = second (drop 1) $ break (== '.') xs - in case ys of - [] -> [y] - _ -> y : splitter ys + _ -> 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 - | BuildFirmware - | Deploy - | DeployFirmware - | Check + = Build { _target :: !Target } + | Deploy { _target :: !Target } deriving stock (Eq, Show) data CLIArgs = CLIArgs { _action :: !Action - , _flake :: !Flake + , _flake :: !KoboFlake , _system :: !String + , _nixArgs :: ![String] } deriving stock (Eq, Show) -- cgit v1.2.3