tvl-depot/users/grfn/terraform/workspace.nix
sterni 0c178a0ef6 chore(3p/sources): Bump channels & overlays
Upstream nixpkgs removed a lot of aliases this time, so we needed to do
the following transformations. It's a real shame that aliases only
really become discoverable easily when they are removed.

* runCommandNoCC -> runCommand
* gmailieer -> lieer
  We also need to work around the fact that home-manager hasn't catched
  on to this rename.
* mysql -> mariadb
* pkgconfig -> pkg-config
  This also affects our Nix fork which needs to be bumped.
* prometheus_client -> prometheus-client
* rxvt_unicode -> rxvt-unicode-unwrapped
* nix-review -> nixpkgs-review
* oauth2_proxy -> oauth2-proxy

Additionally, some Go-related builders decided to drop support for
passing the sha256 hash in directly, so we need to use the generic hash
arguments.

Change-Id: I84aaa225ef18962937f8616a9ff064822f0d5dc3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6792
Autosubmit: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: wpcarro <wpcarro@gmail.com>
2022-09-28 08:02:31 +00:00

107 lines
2.5 KiB
Nix

{ pkgs, depot, ... }:
name: { plugins }: module_tf:
let
inherit (pkgs) lib runCommand writeText writeScript;
inherit (lib) filterAttrsRecursive;
allPlugins = (p: plugins p ++ (with p; [
external
local
tls
p.null
]));
tf = pkgs.terraform.withPlugins allPlugins;
cleanTerraform = filterAttrsRecursive (k: _: ! (builtins.elem k [
"__readTree"
"__readTreeChildren"
]));
plugins_tf = {
terraform.required_providers = (builtins.listToAttrs (map
(p: {
name = lib.last (lib.splitString "/" p.provider-source-address);
value = {
source = p.provider-source-address;
version = p.version;
};
})
(allPlugins pkgs.terraform.plugins)));
};
module_tf' = module_tf // {
inherit (depot.users.grfn.terraform) globals;
plugins = plugins_tf;
};
module = runCommand "module" { } ''
mkdir $out
${lib.concatStrings (lib.mapAttrsToList (k: config_tf:
(let
# TODO: filterAttrsRecursive?
configJson = writeText "${k}.tf.json"
(builtins.toJSON (cleanTerraform config_tf));
in ''
${pkgs.jq}/bin/jq . ${configJson} > $out/${lib.escapeShellArg k}.tf.json
''))
(cleanTerraform module_tf'))}
'';
tfcmd = writeScript "${name}-tfcmd" ''
set -e
dir="''${TF_STATE_ROOT:-$HOME/tfstate}/${name}"
cd "$dir"
rm -f *.json
cp ${module}/*.json .
exec ${tf}/bin/terraform "$(basename "$0")"
'';
init = writeScript "${name}-init" ''
set -e
dir="''${TF_STATE_ROOT:-$HOME/tfstate}/${name}"
[ -d "$dir" ] || mkdir -p "$dir"
cd "$dir"
rm -f *.json
cp ${module}/*.json .
exec ${tf}/bin/terraform init
'';
# TODO: import (-config)
tfcmds = runCommand "${name}-tfcmds" { } ''
mkdir -p $out/bin
ln -s ${init} $out/bin/init
ln -s ${tfcmd} $out/bin/validate
ln -s ${tfcmd} $out/bin/plan
ln -s ${tfcmd} $out/bin/apply
ln -s ${tfcmd} $out/bin/destroy
'';
in
{
inherit name module;
terraform = tf;
cmds = tfcmds;
# run = {
# init = depot.nix.nixRunWrapper "init" tfcmds;
# validate = depot.nix.nixRunWrapper "validate" tfcmds;
# plan = depot.nix.nixRunWrapper "plan" tfcmds;
# apply = depot.nix.nixRunWrapper "apply" tfcmds;
# destroy = depot.nix.nixRunWrapper "destroy" tfcmds;
# };
test = runCommand "${name}-test" { } ''
set -e
export TF_STATE_ROOT=$(pwd)
${tfcmds}/bin/init
${tfcmds}/bin/validate
touch $out
'';
meta.targets = [ "module" "test" ];
}