From 4e775b6691b221a2556b243460e3589dd7cea15b Mon Sep 17 00:00:00 2001 From: kat witch Date: Sat, 15 May 2021 00:50:24 +0100 Subject: [PATCH] shell: HISTFILE, CI, runners --- default.nix | 4 +++- lib/deploy.nix | 33 ++++++++++++++++++++++----------- lib/hosts.nix | 4 ++-- runners.nix | 16 ++++++++++++++++ shell.nix | 27 +++++++++++++++++++++++++-- 5 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 runners.nix diff --git a/default.nix b/default.nix index d8340ab7..94699c1e 100644 --- a/default.nix +++ b/default.nix @@ -15,7 +15,9 @@ rec { }) hosts targets; - inherit (pkgs) lib; + inherit (pkgs) lib; + + runners = import ./runners.nix { inherit lib; inherit (deploy) target; }; deploy = import ./lib/deploy.nix { inherit pkgs sources; diff --git a/lib/deploy.nix b/lib/deploy.nix index 17c65fcc..5ec1e93a 100644 --- a/lib/deploy.nix +++ b/lib/deploy.nix @@ -5,24 +5,30 @@ with pkgs.lib; let pkgsModule = { ... }: { config._module.args = { pkgs = mkDefault pkgs; }; }; + configExtension = { ... }: { + options.terraform.baseDir = mkOption { + type = types.path; + }; + }; + tfEval = config: (evalModules { - modules = [ pkgsModule (sources.tf-nix + "/modules") ] ++ toList config; + modules = [ pkgsModule (sources.tf-nix + "/modules") configExtension ] ++ toList config; specialArgs = { inherit hosts; }; }).config; - tf = { targetName, target }: + tf = { targetName ? null, target ? [] }: tfEval ({ config, ... }: { imports = optional (builtins.pathExists ../trusted/tf) (import ../trusted/tf/meta.nix) - ++ map (hostName: ../hosts + "/${hostName}/meta.nix") target ++ [{ + ++ flatten (map (hostName: optional (builtins.pathExists (../hosts + "/${hostName}/meta.nix")) (../hosts + "/${hostName}/meta.nix")) target) ++ [{ config = mkMerge (map (hostName: mapAttrs (_: mkMerge) hosts.${hostName}.config.deploy.tf.out.set) target); }] ++ optional - (builtins.pathExists (../trusted/targets + "/${targetName}")) + (targetName != null && builtins.pathExists (../trusted/targets + "/${targetName}")) (../trusted/targets + "/${targetName}") - ++ optional (builtins.pathExists (../targets + "/${targetName}")) + ++ optional (targetName != null && builtins.pathExists (../targets + "/${targetName}")) (../targets + "/${targetName}") ++ concatMap (hostName: filter builtins.pathExists @@ -35,12 +41,17 @@ let enable = true; }; - runners.lazy = { - file = ../.; - args = [ "--show-trace" ]; - attrPrefix = - let attr = if target != null then "target.${targetName}" else "tf"; - in "deploy.${attr}.runners.run."; + runners = { + lazy = { + file = ../.; + args = [ "--show-trace" ]; + attrPrefix = + let attr = if targetName != null then "target.${targetName}" else "tf"; + in "deploy.${attr}.runners.run."; + }; + run = { + apply.name = if targetName != null then "${targetName}-apply" else "tf-apply"; + }; }; variables.hcloud_token = { diff --git a/lib/hosts.nix b/lib/hosts.nix index eea3113f..bffa3a94 100644 --- a/lib/hosts.nix +++ b/lib/hosts.nix @@ -38,7 +38,7 @@ rec { })) hostNames); - targets = foldAttrs (host: hosts: [ host ] ++ hosts) [ ] (mapAttrsToList + targets = filterAttrs (targetName: _: targetName != "") (foldAttrs (host: hosts: [ host ] ++ hosts) [ ] (mapAttrsToList (hostName: host: { ${host.config.deploy.target} = hostName; }) - hosts); + hosts)); } diff --git a/runners.nix b/runners.nix new file mode 100644 index 00000000..867ef74e --- /dev/null +++ b/runners.nix @@ -0,0 +1,16 @@ +{ lib, target }: + +with lib; + +# targets -> targetName list of hosts + +let +runners = { + run = foldAttrList (mapAttrsToList (targetName: targetx: mapAttrs' (k: run: + nameValuePair run.name run.set + ) targetx.runners.run) target); + lazy.run = foldAttrList (mapAttrsToList (targetName: targetx: mapAttrs' (k: run: + nameValuePair run.name run.set + ) targetx.runners.lazy.run) target); + lazy.nativeBuildInputs = concatLists (mapAttrsToList (targetName: target: target.runners.lazy.nativeBuildInputs) target); +}; in runners diff --git a/shell.nix b/shell.nix index 93774a03..009f2832 100644 --- a/shell.nix +++ b/shell.nix @@ -1,7 +1,30 @@ -with (import { }); +{ }: let + config = import ./default.nix; + tf = config.deploy.tf {}; + inherit (config) pkgs; +in pkgs.mkShell { + nativeBuildInputs = config.runners.lazy.nativeBuildInputs; + HISTFILE = toString (tf.terraform.baseDir + "/.history"); + + CI_ROOT = toString ./.; + CI_CONFIG_ROOT = toString ./ci; + #CI_CONFIG = toString ./example/ci.nix + CI_PLATFORM = "impure"; # use host's nixpkgs for more convenient testing -mkShell { shellHook = '' export HOME_HOSTNAME=$(hostname -s) + export NIX_PATH="$NIX_PATH:nixfiles=${toString ./.}" + + CI_CONFIG_FILES=($CI_CONFIG_ROOT/hosts.nix) + gh-actions-generate() { + for f in "''${CI_CONFIG_FILES[@]}"; do + nix run --arg config $f ci.run.gh-actions-generate + done + } + test-all() { + for f in "''${CI_CONFIG_FILES[@]}"; do + nix run --arg config $f ci.test || break + done + } ''; }