1
0
Fork 0
nixos-config/common/test-lib.nix

69 lines
2.8 KiB
Nix
Raw Normal View History

2019-07-25 21:35:59 +00:00
let
2019-07-26 15:31:26 +00:00
pkgs = import <nixpkgs> {};
2019-07-31 09:16:47 +00:00
inherit (import ../common/lib.nix) writeHaskellScript get-niv-path home-manager unstable niv haskellList;
2019-07-25 21:35:59 +00:00
haskellBody = commandline:
''
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 = do
2019-07-28 22:51:10 +00:00
(configDir:hostname:args) <- getArgs
paths <- concat <$> mapM (getNivAssign configDir) ["nixpkgs", "unstable", "home-manager"]
2019-07-25 21:35:59 +00:00
${commandline}
'';
2019-07-26 13:54:30 +00:00
bins = [ get-niv-path pkgs.nix ];
2019-07-25 21:35:59 +00:00
test-system-config = writeHaskellScript {
name = "test-system-config";
2019-07-28 22:51:10 +00:00
inherit bins;
2019-07-25 21:35:59 +00:00
} (haskellBody
''
2019-07-28 22:51:10 +00:00
nix $ ["build", "-f", "<nixpkgs/nixos>", "system"] ++ paths ++ ["-I", [i|nixos-config=#{configDir}/hosts/#{hostname}/configuration.nix|], "-o", [i|result-system-#{hostname}|]] ++ args
2019-07-25 21:35:59 +00:00
'');
2019-07-26 13:54:30 +00:00
test-home-config = writeHaskellScript {
name = "test-home-config";
2019-07-28 22:51:10 +00:00
inherit bins;
2019-07-25 21:35:59 +00:00
} (haskellBody
''
2019-07-28 22:51:10 +00:00
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
2019-07-25 21:35:59 +00:00
'');
repoSrc = "git@hera.m-0.eu:nixos-config";
2019-07-26 17:10:53 +00:00
configPath = "/etc/nixos";
2019-07-31 09:16:47 +00:00
systems = ["apollo" "hera"];
homes = ["apollo" "hera" "hephaistos"];
keys = ["default" "apollo" "hera"];
2019-07-26 13:54:30 +00:00
test-and-bump-config = writeHaskellScript {
name = "test-and-bump-config";
bins = [ test-system-config test-home-config pkgs.git pkgs.coreutils niv pkgs.git-crypt ];
2019-07-25 23:35:19 +00:00
imports = [ "Control.Exception (bracket)" "System.Directory (withCurrentDirectory)" "Control.Monad (when)"];
2019-07-25 21:35:59 +00:00
} ''
2019-07-26 15:31:26 +00:00
checkout :: IO FilePath
checkout = do
2019-07-28 22:51:10 +00:00
dir <- LBSC.unpack <$> (readTrim $ mktemp "-d")
2019-07-26 15:31:26 +00:00
git "clone" "${repoSrc}" dir
return dir
2019-07-25 21:35:59 +00:00
main = do
path <- readTrim pwd
2019-07-28 22:51:10 +00:00
bracket checkout (rm "-rf") $ \dir -> do
withCurrentDirectory dir $ do
2019-07-31 09:16:47 +00:00
mapM_ (\x -> git_crypt "unlock" ([i|${configPath}/.git/git-crypt/keys/#{x}|] :: String)) ${haskellList keys}
2019-07-28 22:51:10 +00:00
ignoreFailure $ niv "update"
2019-07-31 09:16:47 +00:00
mapM_ (test_system_config dir) ${haskellList systems}
mapM_ (test_home_config dir) ${haskellList homes}
2019-07-28 22:51:10 +00:00
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"
2019-07-25 21:35:59 +00:00
'';
in
{
2019-07-26 15:31:26 +00:00
inherit test-system-config test-home-config test-and-bump-config;
2019-07-25 21:35:59 +00:00
}