{ pkgs, ... }:
let
  opentofu = pkgs.khscodes.opentofu;
in
pkgs.writeShellApplication {
  name = "instance-opentofu";
  runtimeInputs = [
    pkgs.uutils-coreutils-noprefix
    opentofu
  ];
  text = ''
    fqdn="$1"
    config="$2"
    cmd="''${3:-apply}"
    dir="$(mktemp -dt "$fqdn-pre-provisioning.XXXXXX")"
    mkdir -p "$dir"
    cat "''${config}" > "$dir/config.tf.json"

    tofu -chdir="$dir" init > /dev/null
    tofu -chdir="$dir" "$cmd"
  '';
}
