terraform {
  required_providers {
    null = {
      source = "hashicorp/null"
      version = "3.2.1"
    }
    template = {
      source = "hashicorp/template"
      version = "2.2.0"
    }
  }
}

provider "template" {}

variable "ssh_password" {
  description = "Password for SSH connection"
  type        = string
}

resource "null_resource" "deploy_nixos" {
  connection {
    type        = "ssh"
    host        = "jaglan-beta-m01"
    user        = "root"
    password    = var.ssh_password
  }

  provisioner "file" {
    source      = "configuration.nix"
    destination = "/tmp/configuration.nix"
  }

  provisioner "remote-exec" {
    inline = [
      "mv /tmp/configuration.nix /etc/nixos/configuration.nix",
      "nixos-rebuild switch --use-remote-sudo"
    ]
  }

  triggers = {
    configuration_content = file("configuration.nix")
  }
}