Moving to modules. Structural changes.

This commit is contained in:
kat witch 2021-07-05 22:47:28 +01:00
parent 3903bc1766
commit 060d4c6d1e
No known key found for this signature in database
GPG key ID: 1B477797DCA5EC72
258 changed files with 621 additions and 407 deletions

View file

@ -1,25 +0,0 @@
{ lib }: rec {
hexChars =
[ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f" ];
hexCharToInt = char:
let
pairs = lib.imap0 (lib.flip lib.nameValuePair) hexChars;
idx = builtins.listToAttrs pairs;
in
idx.${lib.toLower char};
hexToInt = str:
lib.foldl (value: chr: value * 16 + hexCharToInt chr) 0
(lib.stringToCharacters str);
hextorgba = hex: alpha:
(
let
r_hex = lib.substring 1 2 hex;
g_hex = lib.substring 3 2 hex;
b_hex = lib.substring 5 2 hex;
r_dec = hexToInt r_hex;
g_dec = hexToInt g_hex;
b_dec = hexToInt b_hex;
in
"rgba(${toString r_dec}, ${toString g_dec}, ${toString b_dec}, ${toString alpha})"
);
}

View file

@ -1,96 +0,0 @@
{ pkgs, hosts, sources, targets }:
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") configExtension ] ++ toList config;
specialArgs = { inherit hosts; };
}).config;
tf = { targetName ? null, target ? [] }:
tfEval ({ config, ... }: {
imports = optional (builtins.pathExists ../trusted/tf) (import ../trusted/tf/meta.nix)
++ 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
(targetName != null && builtins.pathExists (../trusted/targets + "/${targetName}"))
(../trusted/targets + "/${targetName}")
++ optional (targetName != null && builtins.pathExists (../targets + "/${targetName}"))
(../targets + "/${targetName}") ++ concatMap
(hostName:
filter builtins.pathExists
(map (profile: ../profiles + "/${profile}/meta.nix") (attrNames
(filterAttrs (_: id) hosts.${hostName}.config.deploy.profile))))
target;
deps = {
select.allProviders = true;
enable = true;
};
terraform.version = "0.15";
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 = {
type = "string";
value.shellCommand = "bitw get infra/hcloud_token";
};
variables.glauca_key = {
type = "string";
value.shellCommand = "bitw get infra/rfc2136 -f username";
};
variables.glauca_secret = {
type = "string";
value.shellCommand = "bitw get infra/rfc2136 -f password";
};
dns.zones."kittywit.ch." = { provider = "dns"; };
providers.hcloud = { inputs.token = config.variables.hcloud_token.ref; };
providers.dns = {
inputs.update = {
server = "ns1.as207960.net";
key_name = config.variables.glauca_key.ref;
key_secret = config.variables.glauca_secret.ref;
key_algorithm = "hmac-sha512";
};
};
_module.args = {
inherit targetName;
};
});
in
{
inherit tf;
target =
mapAttrs (targetName: target: tf { inherit target targetName; }) targets;
}

View file

@ -1,55 +0,0 @@
{ pkgs
, target
, users
, hostsDir ? ../hosts
, profiles
, pkgsPath ? ../pkgs
, sources ? { }
, system ? builtins.currentSystem
}:
with pkgs.lib;
rec {
baseModules = import (pkgs.path + "/nixos/modules/module-list.nix");
hostNames = attrNames
(filterAttrs (name: type: type == "directory") (builtins.readDir hostsDir));
hostConfig = hostName:
{ config, ... }: {
_module.args = { inherit hosts targets; };
imports = [ ../nixos.nix ../modules/nixos ];
networking = { inherit hostName; };
nixpkgs.pkgs = pkgs;
};
hosts = listToAttrs (map
(hostName:
nameValuePair hostName (evalModules {
modules = baseModules ++ [
(hostConfig hostName)
({ config, ... }: {
config._module.args.pkgs = pkgs;
config.nixpkgs.system = mkDefault system;
config.nixpkgs.initialSystem = system;
})
(if sources ? home-manager then
sources.home-manager + "/nixos"
else
{ })
];
args = {
inherit baseModules modules;
};
specialArgs = {
modulesPath = builtins.toString pkgs.path + "/nixos/modules";
inherit sources target profiles hostName users;
};
}))
hostNames);
targets = filterAttrs (targetName: _: targetName != "") (foldAttrs (host: hosts: [ host ] ++ hosts) [ ] (mapAttrsToList
(hostName: host: { ${host.config.deploy.target} = hostName; })
hosts));
}

View file

@ -1,27 +0,0 @@
{ modulesDir, defaultFile ? "default.nix", importAll ? false }:
with builtins;
let
filterAttrNamesToList = filter: set:
foldl' (a: b: a ++ b) [ ]
(map (e: if (filter e set.${e}) then [ e ] else [ ]) (attrNames set));
nameValuePair = name: value: { inherit name value; };
listToAttrs = foldl' (acc: val: acc // { ${val.name} = val.value; }) { };
directories =
filterAttrNamesToList (_: type: type == "directory") (readDir modulesDir);
files = map (dir: nameValuePair dir (modulesDir + "/${dir}/${defaultFile}"))
directories;
modules = map
({ name, value }:
# if the file contains a function, assume it to be a module and pass the path
# (for dedup and such). if it contains anything else, pass that.
let m = import value;
in
{
inherit name;
value = if (isFunction m) && !importAll then value else m;
})
files;
in
(listToAttrs modules)

View file

@ -1,23 +0,0 @@
athame-testing = {
provider = "hcloud";
type = "server";
inputs = {
name = "athame-testing";
image = "ubuntu-20.04";
server_type = "cpx21";
location = "nbg1";
backups = false;
ssh_keys = [ (hcloud_ssh_key.refAttr "id") ];
};
connection = { host = config.lib.tf.terraformSelf "ipv4_address"; };
provisioners = [
{
remote-exec.command =
"curl https://raw.githubusercontent.com/elitak/nixos-infect/master/nixos-infect | NO_REBOOT=true PROVIDER=hetznercloud NIX_CHANNEL=nixos-20.09 bash 2>&1 | tee /tmp/infect.log";
}
{
remote-exec.command = "reboot";
onFailure = "continue";
}
];
};