Improve haskell scripts
This commit is contained in:
parent
fc0a0c71e8
commit
2292941680
|
@ -16,10 +16,12 @@ let
|
|||
import Data.String.Interpolate (i)
|
||||
import qualified Data.ByteString as BS
|
||||
import qualified Data.ByteString.Lazy as LBS
|
||||
import qualified Data.ByteString.Lazy.Char8 as LBSC
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Lazy as LT
|
||||
import qualified Data.Text.Encoding as TE
|
||||
import qualified Data.Text.Lazy.Encoding as LTE
|
||||
import System.Environment (getArgs)
|
||||
${builtins.concatStringsSep "\n" (map (x: "import ${x}") imports)}
|
||||
|
||||
-- Load binaries from Nix packages. The dependencies will be captured
|
||||
|
@ -35,19 +37,11 @@ let
|
|||
libraries = [ unstable.haskellPackages.cmdargs unstable.haskellPackages.text ];
|
||||
} ''
|
||||
|
||||
data Path = Path {
|
||||
sources :: String,
|
||||
channel :: String
|
||||
} deriving (Show, Data, Typeable)
|
||||
|
||||
path = Path{ sources = def &= argPos 0, channel = def &= argPos 1}
|
||||
|
||||
trimQuotation = pureProc $ LTE.encodeUtf8 . LT.dropAround ('"' ==) . LTE.decodeUtf8 . trim
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
path <- cmdArgs path
|
||||
let expr = [i|(import #{sources path}).#{channel path}|]
|
||||
[sources, channel] <- getArgs
|
||||
let expr = [i|(import #{sources}).#{channel}|]
|
||||
nix_build ["-Q", "-E", expr, "--no-out-link"] &> devNull
|
||||
nix_instantiate ["--eval", "-E", [i|toString #{expr}|]] |> trimQuotation
|
||||
'';
|
||||
|
|
|
@ -18,7 +18,8 @@ in
|
|||
where tag str = ["-I", [i|#{name}=#{str :: LBS.ByteString}|]]
|
||||
|
||||
main = do
|
||||
args <- getArgs
|
||||
paths <- concat <$> mapM getNivAssign ["home-manager", "nixpkgs", "unstable"]
|
||||
home_manager $ paths ++ ["switch"]
|
||||
home_manager $ paths ++ ["switch"] ++ args
|
||||
'';
|
||||
}
|
||||
|
|
|
@ -3,46 +3,32 @@ let
|
|||
inherit (import ../common/lib.nix) writeHaskellScript get-niv-path home-manager unstable niv;
|
||||
haskellBody = commandline:
|
||||
''
|
||||
|
||||
data Host = Host {
|
||||
configDir :: String,
|
||||
hostname :: String
|
||||
} deriving (Show, Data, Typeable)
|
||||
|
||||
host = Host{
|
||||
configDir = def &= argPos 0,
|
||||
hostname = def &= argPos 1
|
||||
}
|
||||
|
||||
getNivPath dir = readTrim . get_niv_path ([i|#{dir :: String}/nix/sources.nix|] :: String)
|
||||
|
||||
getNivAssign dir name = fmap process . getNivPath dir $ name
|
||||
where process str = ["-I", [i|#{name :: String}=#{str :: LBS.ByteString}|]]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
host <- cmdArgs host
|
||||
paths <- (concat <$>) . mapM (getNivAssign $ configDir host) $ ["nixpkgs", "unstable", "home-manager"]
|
||||
(configDir:hostname:args) <- getArgs
|
||||
paths <- concat <$> mapM (getNivAssign configDir) ["nixpkgs", "unstable", "home-manager"]
|
||||
${commandline}
|
||||
'';
|
||||
bins = [ get-niv-path pkgs.nix ];
|
||||
imports = ["System.Console.CmdArgs.Implicit"];
|
||||
libraries = [ unstable.haskellPackages.cmdargs ];
|
||||
|
||||
test-system-config = writeHaskellScript {
|
||||
name = "test-system-config";
|
||||
inherit bins imports libraries;
|
||||
inherit bins;
|
||||
} (haskellBody
|
||||
''
|
||||
nix $ ["build", "-f", "<nixpkgs/nixos>", "system"] ++ paths ++ ["-I", [i|nixos-config=#{configDir host}/hosts/#{hostname host}/configuration.nix|], "-o", [i|result-system-#{hostname host}|]]
|
||||
nix $ ["build", "-f", "<nixpkgs/nixos>", "system"] ++ paths ++ ["-I", [i|nixos-config=#{configDir}/hosts/#{hostname}/configuration.nix|], "-o", [i|result-system-#{hostname}|]] ++ args
|
||||
'');
|
||||
|
||||
test-home-config = writeHaskellScript {
|
||||
name = "test-home-config";
|
||||
inherit bins imports libraries;
|
||||
inherit bins;
|
||||
} (haskellBody
|
||||
''
|
||||
nix $ ["build", "-f", "<home-manager/home-manager/home-manager.nix>"] ++ paths ++ ["--argstr", "confPath", [i|#{configDir host}/hosts/#{hostname host}/home.nix|], "--argstr", "confAttr", "", "--out-link", [i|result-home-manager-#{hostname host}|], "activationPackage"]
|
||||
nix $ ["build", "-f", "<home-manager/home-manager/home-manager.nix>"] ++ paths ++ ["--argstr", "confPath", [i|#{configDir}/hosts/#{hostname}/home.nix|], "--argstr", "confAttr", "", "--out-link", [i|result-home-manager-#{hostname}|], "activationPackage"] ++ args
|
||||
'');
|
||||
|
||||
repoSrc = "git@hera.m-0.eu:nixos-config";
|
||||
|
@ -54,40 +40,24 @@ let
|
|||
} ''
|
||||
checkout :: IO FilePath
|
||||
checkout = do
|
||||
dir <- (LT.unpack . LTE.decodeUtf8 <$>) . readTrim $ mktemp "-d"
|
||||
dir <- LBSC.unpack <$> (readTrim $ mktemp "-d")
|
||||
git "clone" "${repoSrc}" dir
|
||||
return dir
|
||||
|
||||
cleanup :: String -> IO ()
|
||||
cleanup = (rm "-rf")
|
||||
|
||||
unlock :: IO ()
|
||||
unlock = mapM_ (\x -> git_crypt "unlock" ([i|${configPath}/.git/git-crypt/keys/#{x}|] :: String)) ["default", "apollo", "hera"]
|
||||
|
||||
update :: IO ()
|
||||
update = ignoreFailure $ niv "update"
|
||||
|
||||
testBuild :: FilePath -> IO ()
|
||||
testBuild dir = do
|
||||
mapM_ (test_system_config dir) ["apollo", "hera"]
|
||||
mapM_ (test_home_config dir) ["apollo", "hera", "hephaistos"]
|
||||
|
||||
push :: FilePath -> IO()
|
||||
push dir = do
|
||||
changed <- ((mempty /=) <$>) . readTrim $ git "-C" dir "status" "--porcelain"
|
||||
when changed $ do
|
||||
git "-C" dir "config" "user.email" "maralorn@maralorn.de"
|
||||
git "-C" dir "config" "user.name" "maralorn (nix-auto-updater)"
|
||||
git "-C" dir "commit" "-am" "Update dependencies with niv"
|
||||
git "-C" dir "push"
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
path <- readTrim pwd
|
||||
bracket checkout cleanup $ \dir -> do
|
||||
withCurrentDirectory dir $ unlock >> update
|
||||
testBuild dir
|
||||
push dir
|
||||
bracket checkout (rm "-rf") $ \dir -> do
|
||||
withCurrentDirectory dir $ do
|
||||
mapM_ (\x -> git_crypt "unlock" ([i|${configPath}/.git/git-crypt/keys/#{x}|] :: String)) ["default", "apollo", "hera"]
|
||||
ignoreFailure $ niv "update"
|
||||
mapM_ (test_system_config dir) ["apollo", "hera"]
|
||||
mapM_ (test_home_config dir) ["apollo", "hera", "hephaistos"]
|
||||
changed <- ((mempty /=) <$>) . readTrim $ git "-C" dir "status" "--porcelain"
|
||||
when changed $ do
|
||||
git "-C" dir "config" "user.email" "maralorn@maralorn.de"
|
||||
git "-C" dir "config" "user.name" "maralorn (nix-auto-updater)"
|
||||
git "-C" dir "commit" "-am" "Update dependencies with niv"
|
||||
git "-C" dir "push"
|
||||
'';
|
||||
in
|
||||
{
|
||||
|
|
|
@ -15,7 +15,8 @@ let
|
|||
|
||||
main = do
|
||||
paths <- fmap concat . mapM getNivAssign $ ["nixpkgs", "unstable", "home-manager"]
|
||||
nixos_rebuild (paths ++ ["switch"])
|
||||
args <- getArgs
|
||||
nixos_rebuild (paths ++ ["switch"] ++ args)
|
||||
'';
|
||||
system-maintenance = writeHaskellScript
|
||||
{ name = "system-maintenance"; bins = [ pkgs.nix pkgs.git update-system ];} ''
|
||||
|
|
Loading…
Reference in a new issue