{
  inputs,
  pkgs,
}:
pkgs.writeShellApplication {
  name = "nixos-install";
  runtimeInputs = [
    pkgs.nix
    pkgs.nixos-anywhere
  ];
  # TODO: Use secret source and required secrets to set up the correct env variables
  text = ''
    hostname="$1"
    # Build the configuration to ensure it doesn't fail when trying to install it on the host
    nix build --no-link '${inputs.self}#nixosConfigurations."'"$hostname"'".config.system.build.toplevel'
    # Allow overriding the host to connec tto, this is useful when testing and the DNS entries are stale with older IPs.
    host="''${2:-$1}"
    baseAttr='${inputs.self}#nixosConfigurations."'"$hostname"'".config.khscodes.infrastructure.provisioning'
    config="$(nix build --no-link --print-out-paths "''${baseAttr}.preConfig")"
    username="$(nix eval --raw "''${baseAttr}.preImageUsername")"
    if [[ "$config" == "null" ]]; then
      echo "No preprovisioning needed"
      exit 0
    fi
    nixos-anywhere --flake "${inputs.self}#$hostname" --target-host "$username@$host"
  '';
}
