nix,terraform: auto generate tf variables based on hosts.nix

This commit is contained in:
Vivian 2021-11-25 00:15:19 +01:00
parent 6740fb7264
commit 5eeedf319c
8 changed files with 165 additions and 93 deletions

View file

@ -48,16 +48,31 @@
};
};
# Generates hosts.auto.tfvars.json for Terraform
genTFVars = let
hostToVar = z@{ hostname, mac, ... }: {
"${hostname}" = { inherit mac; };
};
hostSet = lib.foldr (el: acc: acc // hostToVar el) { } hosts;
json = builtins.toJSON { hosts = hostSet; };
in pkgs.writeScriptBin "gen-tf-vars" ''
echo '${json}' | ${pkgs.jq}/bin/jq > terraform/hosts.auto.tfvars.json;
echo "Generated Terraform Variables";
'';
# Import all nixos host definitions that are actual nix machines
nixHosts = filter ({ nix ? true, ... }: nix) hosts;
pkgs = serokell-nix.lib.pkgsWith nixpkgs.legacyPackages.${system} [ vault-secrets.overlay ];
pkgs = serokell-nix.lib.pkgsWith nixpkgs.legacyPackages.${system}
[ vault-secrets.overlay ];
deployChecks = mapAttrs (_: lib: lib.deployChecks self.deploy) deploy-rs.lib;
deployChecks =
mapAttrs (_: lib: lib.deployChecks self.deploy) deploy-rs.lib;
checks = { };
in {
# Make the config and deploy sets
nixosConfigurations = lib.foldr (el: acc: acc // mkConfig el) { } nixHosts;
nixosConfigurations =
lib.foldr (el: acc: acc // mkConfig el) { } nixHosts;
deploy.nodes = lib.foldr (el: acc: acc // mkDeploy el) { } nixHosts;
apps.x86_64-linux.vault-push-approles = {
@ -70,6 +85,11 @@
"${pkgs.vault-push-approle-envs self}/bin/vault-push-approle-envs";
};
apps.x86_64-linux.tfvars = {
type = "app";
program = "${genTFVars}/bin/gen-tf-vars";
};
# Use by running `nix develop`
devShell.${system} = pkgs.mkShell {
VAULT_ADDR = "http://vault.olympus:8200/";
@ -86,6 +106,7 @@
vault
(vault-push-approles self { })
(vault-push-approle-envs self { })
genTFVars
];
};