infrastructure as nix code
terranix is a Nix-based infrastructure-as-code tool that combines the providers of Terraform (or OpenTofu) and the lazy, functional configuration of NixOS. terranix works as an alternative to HCL by generating Terraform JSON that can then be applied using the same providers.
let
mkSite = { name, region }: {
resource.digitalocean_droplet.${name} = {
inherit name region;
image = "ubuntu-22-04-x64";
size = "s-1vcpu-1gb";
};
resource.digitalocean_record.${name} = {
domain = "example.com";
type = "A";
inherit name;
value = "\${digitalocean_droplet.${name}.ipv4_address}";
};
};
in {
imports = [
(mkSite { name = "web-1"; region = "fra1"; })
(mkSite { name = "web-2"; region = "nyc1"; })
(mkSite { name = "web-3"; region = "sgp1"; })
];
}locals {
sites = {
web-1 = "fra1"
web-2 = "nyc1"
web-3 = "sgp1"
}
}
resource "digitalocean_droplet" "site" {
for_each = local.sites
name = each.key
region = each.value
image = "ubuntu-22-04-x64"
size = "s-1vcpu-1gb"
}
resource "digitalocean_record" "site" {
for_each = local.sites
domain = "example.com"
type = "A"
name = each.key
value = digitalocean_droplet.site[each.key].ipv4_address
}Features
- Full Nix languageUse let-bindings, imports, conditionals, and module composition with the same language you already use for NixOS.
- NixOS module systemType-checked configuration with sensible defaults, overrides, and reusable modules.
- nixpkgs ecosystemTap into
fetchgit,fetchurl,writers, and pin Terraform / OpenTofu versions and providers reproducibly. - Documentation generatorGenerate JSON or man-page documentation straight from your
config.nixmodule options.