Massive rework to rebase on hexchen's new nixfiles deploy stuffs.

This commit is contained in:
kat witch 2021-01-24 21:37:41 +00:00 committed by kat
parent 6eac632061
commit f2eb255ba5
62 changed files with 990 additions and 846 deletions

View file

@ -1,74 +1,18 @@
let
pkgs = import <nixpkgs> { };
lib = pkgs.lib;
{ pkgs, hosts, profiles }:
hosts = import ../configuration/hosts;
nixosHosts = lib.filterAttrs (name: host: host ? ssh) hosts;
with pkgs.lib;
allGroups = lib.unique
(lib.flatten (lib.mapAttrsToList (name: host: host.groups) hosts));
hostsInGroup = group:
lib.filterAttrs (k: v: builtins.elem group v.groups) hosts;
hostsInAllGroups = lib.listToAttrs
(map (group: lib.nameValuePair group (lib.attrNames (hostsInGroup group)))
allGroups);
mkDeploy = hostnames:
pkgs.writeScript "deploy-${lib.concatStringsSep "-" hostnames}" ''
#!${pkgs.stdenv.shell}
set -e -o pipefail
export PATH=/run/wrappers/bin/:${
with pkgs;
lib.makeBinPath [
coreutils
openssh
nix
gnutar
findutils
nettools
gzip
git
]
}
MODE=$1
shift || true
ARGS=$@
[ "$MODE" == "" ] && MODE="switch"
${lib.concatMapStrings (hostname:
let
hostAttrs = nixosHosts.${hostname};
nixosSystem = (import <nixpkgs/nixos/lib/eval-config.nix> {
modules = [
"${toString ../configuration}/hosts/${hostname}/configuration.nix"
];
system =
if hostAttrs ? system then hostAttrs.system else "x86_64-linux";
}).config.system.build.toplevel;
in ''
(
echo "deploying ${hostname}..."
nix copy --no-check-sigs --to ssh://${hostAttrs.ssh.host} ${nixosSystem}
ssh $NIX_SSHOPTS ${hostAttrs.ssh.host} "sudo nix-env -p /nix/var/nix/profiles/system -i ${nixosSystem}"
ssh $NIX_SSHOPTS ${hostAttrs.ssh.host} "sudo /nix/var/nix/profiles/system/bin/switch-to-configuration $MODE"
) &
PID_LIST+=" $!"
'') hostnames}
echo "deploys started, waiting for them to finish..."
trap "kill $PID_LIST" SIGINT
wait $PID_LIST
'';
in {
deploy =
(lib.mapAttrs (hostname: hostAttrs: mkDeploy [ hostname ]) nixosHosts)
// (lib.mapAttrs (group: hosts: mkDeploy hosts) hostsInAllGroups) // {
all = mkDeploy (lib.attrNames nixosHosts);
};
}
(mapAttrs (name: hosts:
pkgs.writeScript "deploy-profile-${name}" ''
#!${pkgs.runtimeShell}
export PATH=
${concatMapStrings (host: ''
echo "deploying ${host.config.networking.hostName}..."
${host.config.system.build.deployScript} $1 &
PID_LIST+=" $!"
'') hosts}
# FIXME: remove jobs from PIDLIST once they finish
trap "kill $PID_LIST" SIGINT
wait $PID_LIST
'') profiles)
// (mapAttrs (name: host: host.config.system.build.deployScript) hosts)

34
lib/hosts.nix Normal file
View file

@ -0,0 +1,34 @@
{ pkgs, hostsDir ? ../config/hosts
, commonImports ? [ ../config/profiles/common ../modules ], pkgsPath ? ../pkgs
}:
with pkgs.lib;
rec {
hostNames = attrNames
(filterAttrs (name: type: type == "directory") (builtins.readDir hostsDir));
hostConfig = hostName:
{ config, ... }: {
_module.args = { inherit hosts profiles; };
imports = [
(import (hostsDir + "/${hostName}/configuration.nix"))
../modules/deploy
] ++ commonImports;
networking = { inherit hostName; };
nixpkgs.pkgs = import pkgsPath { inherit (config.nixpkgs) config; };
};
hosts = listToAttrs (map (hostName:
nameValuePair hostName
(import (pkgs.path + "/nixos") { configuration = hostConfig hostName; }))
hostNames);
profileNames = unique (concatLists
(mapAttrsToList (name: host: host.config.meta.deploy.profiles) hosts));
profiles = listToAttrs (map (profileName:
nameValuePair profileName
(filter (host: elem profileName host.config.meta.deploy.profiles)
(attrValues hosts))) profileNames);
}