### Initialize Terranix project with Nix flake template Source: https://terranix.org/flakes This command initializes a new Terranix project using the `terranix-examples` GitHub repository as a Nix flake template. It provides a quick and easy way to get started with an example Terranix setup. ```Bash nix flake init --template github:terranix/terranix-examples ``` -------------------------------- ### Enable Bastion Host in Terranix Configuration Source: https://terranix.org/modules This snippet shows how to activate the bastion host setup defined in the example module. By setting `security.bastion.enable` to `true`, the module's configuration for the bastion host and its associated resources will be applied. ```Nix { security.bastion.enable = true; } ``` -------------------------------- ### Query Terranix module options.json with curl and jq Source: https://terranix.org/flakes This example demonstrates how to fetch and parse the `options.json` file from a Terranix module using `curl` and `jq`. It extracts specific fields like label, type, description, example, default, and declaration path, then formats them into a structured JSON output for easy querying. ```Bash curl https://raw.githubusercontent.com/terranix/terranix-module-github/main/options.json | \ jq 'to_entries | .[] | \ { \ label: .key, \ type: .value.type, \ description: .value.description, \ example: .value.example | tojson, \ default: .value.default | tojson, \ defined: .value.declarations[0].path, \ url: .value.declarations[0].url \ }' | \ jq -s '{ options: . }' ``` -------------------------------- ### Example Terranix Module for AWS Bastion Host Source: https://terranix.org/modules This example demonstrates a complete Terranix module designed to enable and configure an AWS bastion host. It defines module options for enabling the feature and specifying a VPC ID, then uses these options to provision an AWS EC2 instance and a security group, exposing the public IP as an output. ```Nix { config, lib, pkgs, ... }: { options.security.bastion = { enable = mkEnableOption "bastion host infrastructure"; vpcID = mkOption { default = "\${ aws_default_vpc.default.id }"; type = lib.types.str; description = "vpc id to which the bastion host should proxy"; }; }; config = mkIf (config.security.bastion.enable) { resource.aws_instance."bastion" = { ami = "ami-969ab1f6" instance_type = "t2.micro" associate_public_ip_address = true }; resource.aws_security_group."bastion-sg" = { name = "bastion-security-group"; vpc_id = config.security.bastion.vpcId; ingress.protocol = "tcp"; ingress.from_port = 22; ingress.to_port = 22; ingress.cidr_blocks = ["0.0.0.0/0"]; }; output."bastion_public_ip".value = "\${ aws_instance.bastion.public_ip }"; }; } ``` -------------------------------- ### Configure Terranix with lib.terranixConfiguration in flake.nix Source: https://terranix.org/flakes This `flake.nix` example demonstrates how to use `terranix.lib.terranixConfiguration` to render a `config.tf.json` file. It defines a default package for a specific system, inheriting the system and including a local `config.nix` module for configuration. ```Nix { inputs.terranix.url = "github:terranix/terranix"; outputs = { terranix, ... }: let system = "x86_64-linux"; in { defaultPackage.${system} = terranix.lib.terranixConfiguration { inherit system; modules = [ ./config.nix ]; }; }; } ``` -------------------------------- ### Initialize a new Terranix module project Source: https://terranix.org/flakes This command uses `nix flake init` to create a new Terranix module project based on a template from `terranix-examples`. It provides a structured starting point for developers who wish to create their own custom Terranix modules. ```Bash nix flake init --template "github:terranix/terranix-examples#module" ``` -------------------------------- ### Generate Multiple AWS S3 Buckets using Terranix Map and ZipAttrs Source: https://terranix.org/functions This example showcases how to leverage `lib.map` and `lib.zipAttrs` in Terranix to create multiple AWS S3 buckets from a list of distinct configurations. It's particularly useful for automating the creation of similar resources that only differ by a few parameters, such as names and domains, reducing repetitive code. ```Nix { lib, ... }: let s3Buckets = [ { name = "awesome-com"; domain = "awesome.com"; } { name = "awesome-org"; domain = "awesome.org"; } { name = "awesome-live"; domain = "awesome.live"; } ]; createBucket = { name, domain }: { "${name}" = { bucket = name; acl = "public-read"; cors_rule = { allowed_headers = ["*"]; allowed_methods = ["PUT" "POST" "GET"]; allowed_origins = [ "https://${domain}" ]; expose_headers = ["ETag"]; max_age_seconds = 3000; }; }; }; in { resource.aws_s3_bucket = lib.zipAttrs (lib.map createBucket s3Buckets); } ``` -------------------------------- ### Use mkAssert for Module Assertions in Terranix Source: https://terranix.org/modules This example demonstrates the `mkAssert` function, which allows you to enforce conditions within your Terranix modules. If the assertion condition is false (e.g., `cfg.parameter` is 'fail'), the provided error message will be displayed, preventing the module from being applied with invalid parameters. ```Nix config = mkAssert (cfg.parameter != "fail") "parameter is set to fail!" { resource.aws_what_ever."\${cfg.parameter}" = { I = "love nixos"; }; }; ``` -------------------------------- ### Configure Multiple Identical AWS Providers in Terraform Source: https://terranix.org/what-is-terranix This example demonstrates how to define multiple instances of the same Terraform provider, specifically AWS, within a configuration. It shows how to use aliases to differentiate between provider instances, allowing for distinct configurations like different regions for each. ```Terraform HCL provider.aws = [{ region = "us-east-1"; } { alias = "eu"; region = "eu-central-1"; }]; ``` -------------------------------- ### Configure Terranix and Terraform with shell.nix Source: https://terranix.org/getting-started This `shell.nix` file sets up the development environment by importing `nixpkgs` and defining a `terraform` wrapper that injects an API token. It includes `terranix` and the custom `terraform` wrapper in the `buildInputs` for a convenient shell. ```Nix { pkgs ? import { } }: let hcloud_api_token = "`${pkgs.pass}/bin/pass development/hetzner.com/api-token`"; terraform = pkgs.writers.writeBashBin "terraform" '' export TF_VAR_hcloud_api_token=${hcloud_api_token} ${pkgs.terraform}/bin/terraform "$@" ''; in pkgs.mkShell { buildInputs = [ pkgs.terranix terraform ]; } ``` -------------------------------- ### Define Nix Flake Apps for Terranix `apply` and `destroy` Source: https://terranix.org/getting-startet-with-nix-flakes This `flake.nix` demonstrates how to create Nix flake applications (`apply` and `destroy`) to automate Terranix and Terraform operations. It sets up `nixpkgs` and `terranix` inputs, defines a `terraformConfiguration`, and uses `pkgs.writers.writeBash` to create shell scripts for `terraform init`, `apply`, and `destroy` commands, including cleanup of `config.tf.json`. ```Nix { inputs.nixpkgs.url = "github:nixos/nixpkgs"; inputs.terranix.url = "github:terranix/terranix"; outputs = { self, nixpkgs, terranix, ... }: let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; terraform = pkgs.terraform; terraformConfiguration = terranix.lib.terranixConfiguration { inherit system; modules = [ ./config.nix ]; }; in { # nix run ".#apply" apps.${system} = { apply = { type = "app"; program = toString (pkgs.writers.writeBash "apply" '' if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi cp ${terraformConfiguration} config.tf.json \ && ${terraform}/bin/terraform init \ && ${terraform}/bin/terraform apply ''); }; # nix run ".#destroy" destroy = { type = "app"; program = toString (pkgs.writers.writeBash "destroy" '' if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi cp ${terraformConfiguration} config.tf.json \ && ${terraform}/bin/terraform init \ && ${terraform}/bin/terraform destroy ''); }; }; # nix run defaultApp.${system} = self.apps.${system}.apply; }; } ``` -------------------------------- ### Configure Terranix with Nix Flake for `nix build` Source: https://terranix.org/getting-startet-with-nix-flakes This snippet shows a minimal `flake.nix` configuration to build a Terranix configuration. It defines a default package that uses `terranix.lib.terranixConfiguration` to process a `config.nix` module, resulting in a `config.tf.json` file ready for Terraform. ```Nix { inputs.terranix.url = "github:terranix/terranix"; outputs = { terranix, ... }: let system = "x86_64-linux"; in { defaultPackage.${system} = terranix.lib.terranixConfiguration { inherit system; modules = [ ./config.nix ]; }; }; } ``` -------------------------------- ### Generate Terraform Config and Apply Changes Source: https://terranix.org/getting-started This snippet shows how to generate the Terraform configuration in JSON format from `config.nix` using `terranix` and then initialize and apply the configuration using `terraform init && terraform apply` to provision resources. ```Bash terranix config.nix > config.tf.json terraform init && terraform apply ``` -------------------------------- ### Inspect Terranix configuration AST using nix repl Source: https://terranix.org/flakes This `nix repl` session demonstrates how to use `terranix.lib.terranixConfigurationAst` to inspect the Abstract Syntax Tree (AST) of a Terranix configuration. It shows how to define a simple resource and then access its properties directly from the AST for debugging or advanced manipulation. ```Nix nix-repl> terranixAst = (builtins.getFlake "github:terranix/terranix").lib.terranixConfigurationAst nix-repl> terranix = terranixAst { system = "x86_64-linux"; modules = [{ resource.local_file = { content = "yolo"; filename = "./example"; }; }]; } nix-repl> terranix.config.resource.local_file { content = "yolo"; filename = "./example"; } ``` -------------------------------- ### Reference Resource Parameters: Terraform HCL vs. Terranix Source: https://terranix.org/terranix-vs-hcl Demonstrates different ways to reference resource parameters in Terranix compared to Terraform HCL. While HCL is limited to variable outputs, Terranix allows referencing any part of the configuration, showcasing direct, `config` object, and `inherit` approaches for reusing parameters. ```Nix resource.hcloud_server.myserver = { name = "node1"; image = "debian-9"; server_type = "cx11"; }; ``` ```Nix resource.hcloud_server.myotherserver = { name = "node2"; image = "\${ hcloud_server.myserver.image }"; server_type = "\${ hcloud_server.myserver.server_type }"; }; ``` ```Nix { config, ... }: resource.hcloud_server.myotherotherserver = { name = "node3"; image = config.resource.hcloud_server.myserver.image; server_type = config.resource.hcloud_server.myserver.server_type; }; ``` ```Nix { config, ... }: resource.hcloud_server.myotherotherotherserver = { name = "node4"; inherit (config.resource.hlcoud_server) image server_type; }; ``` -------------------------------- ### Define HCloud Server Resources in config.nix Source: https://terranix.org/getting-started This `config.nix` file defines two `hcloud_server` resources, 'nginx' and 'test', specifying their names, images, server types, and backup settings. This configuration will be translated into Terraform JSON. ```Nix { ... }: { resource.hcloud_server.nginx = { name = "terranix.nginx"; image = "debian-10"; server_type = "cx11"; backups = false; }; resource.hcloud_server.test = { name = "terranix.test"; image = "debian-9"; server_type = "cx11"; backups = true; }; } ``` -------------------------------- ### Define Multi-line Strings: HCL Heredoc vs. Terranix Nix Syntax Source: https://terranix.org/terranix-vs-hcl Illustrates how to define multi-line strings in HCL using heredoc syntax and in Terranix using Nix's multi-line string syntax. It highlights the differences in indentation handling and the termination of the string block. ```HCL variable "multiline" { description = <