From d96fbd63510048bf56d3d600a65f7983096c1bb1 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Wed, 3 Mar 2021 00:51:39 +0100 Subject: migrating config This deploy logic is primarily based on hxchn's deploy lib [1], with some slight modifications to make it work with my setup. Everything seems to work fine for now. However, I am unsure about the usage of niv — the config doesn't seem to gain much from it, apart from (some) additional complexity. [1] https://gitlab.com/hexchen/nixfiles --- lib/deploy.nix | 23 +++++++++++++++++ lib/hosts.nix | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 lib/deploy.nix create mode 100644 lib/hosts.nix (limited to 'lib') diff --git a/lib/deploy.nix b/lib/deploy.nix new file mode 100644 index 0000000..1b8d1f2 --- /dev/null +++ b/lib/deploy.nix @@ -0,0 +1,23 @@ +{ pkgs, hosts, groups }: + +with pkgs.lib; + +( + mapAttrs (name: hosts: pkgs.writeScript "deploy-group-${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 + '') groups +) // ( + mapAttrs + (name: host: host.config.system.build.deployScript) + (filterAttrs (_: host: host.config.hexchen.deploy.enable) hosts) +) + diff --git a/lib/hosts.nix b/lib/hosts.nix new file mode 100644 index 0000000..91b6b40 --- /dev/null +++ b/lib/hosts.nix @@ -0,0 +1,80 @@ +{ + pkgs, + hostsDir ? ../hosts, + commonImports ? [], + pkgsPath ? ../pkgs, + modules ? {}, + overlays ? {}, + profiles ? {}, + users ? {}, + sources ? {} +}: + +with pkgs.lib; + +rec { + hostNames = attrNames ( + filterAttrs ( + name: type: type == "directory" + ) ( + builtins.readDir hostsDir + ) + ); + + hostConfig = hostName: { config, ... }: { + _module.args = { + inherit hosts groups; + }; + imports = [ + (import (hostsDir + "/${hostName}/configuration.nix")) + # urgh, yes, we still need to manually import the deploy module for now + # at least if i want to keep my thing reusable. + ../modules/deploy + ] ++ commonImports; + networking = { + inherit hostName; + }; + nixpkgs.pkgs = + let pkgs = import pkgsPath { + inherit (config.nixpkgs) config system; + }; + in if pkgs ? nixpkgs then pkgs.nixpkgs else pkgs; + }; + + hosts = listToAttrs ( + map ( + hostName: nameValuePair hostName ( + import (pkgs.path + "/nixos/lib/eval-config.nix") { + modules = [ + (hostConfig hostName) + (if sources ? home-manager then sources.home-manager + "/nixos" else {}) + ]; + specialArgs = { inherit modules overlays profiles users sources; }; + } + ) + ) hostNames + ); + + groupNames = unique ( + concatLists ( + mapAttrsToList ( + name: host: host.config.hexchen.deploy.groups + ) hosts + ) + ); + + groups = listToAttrs ( + map ( + groupName: nameValuePair groupName ( + filter ( + host: elem groupName host.config.hexchen.deploy.groups + ) ( + attrValues ( + filterAttrs (_: h: h.config.hexchen.deploy.enable) hosts + ) + ) + ) + ) groupNames + ); +} + -- cgit v1.2.3