{ pkgs, depotRoot, ... }@args:

let
  terranix = import "${pkgs.terranix}/core/default.nix" {
    inherit pkgs;
    terranix_config = { imports = [ ./config.nix ]; };
    strip_nulls = false;
    extraArgs = args // {
      lib = args.lib // {
        mapToAttrs = pred: onWhat: builtins.listToAttrs (map pred onWhat);
      };
    };
  };

  config = (pkgs.formats.json { }).generate "config.tf.json" terranix.config;

  terraform = pkgs.writeShellScript "terraform" ''
    cd ${depotRoot}/ops/vault/cfg/tf
    rm ./config.tf.json
    ln -s ${config} config.tf.json
    exec ${pkgs.terraform}/bin/terraform "$@"
  '';
in {
  inherit config terraform;
}