teraflops- a terraform ops tool which is sure to be a flop
teraflops aims to provide an integrated experience for deployment workflows which involve both terraform and NixOS - similar to that of NixOps. teraflops uses the excellent colmena deployment tool to do most of the heavy lifting, so the following example should look somewhat familiar if you have ever used colmena.
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
teraflops.url = "github:aanderse/teraflops";
};
outputs = { nixpkgs, teraflops, ... }: {
teraflops = {
imports = [ teraflops.modules.hcloud ];
meta = {
nixpkgs = import nixpkgs {
system = "x86_64-linux";
};
};
machine = { pkgs, ... }: {
deployment.targetEnv = "hcloud";
deployment.hcloud = {
server_type = "cx11";
location = "nbg1";
};
environment.systemPackages = [ pkgs.htop ];
};
# if desired you can write terraform code directly inside your teraflops modules
terraform = {
backend.s3 = {
bucket = "mybucket";
key = "path/to/my/key";
region = "us-east-1";
};
};
};
}
}The teraflops tool has a number of high level commands that often resemble the NixOps CLI.
# prepare your terraform state in the current working directory
teraflops init
# applies all terraform state and deploys your NixOS configuration
teraflops deploy --reboot --confirm
# perform some operational commands
teraflops ssh-for-each -- df -h
teraflops scp machine:/root/.ssh/id_ed25519.pub .
# NixOS introspection
teraflops repl
teraflops eval '{ nodes, ... }: builtins.attrNames nodes'Additionally there are two low level subcommands which get out of your way and let you use the tools you're used to: terraform and colmena.
# 'teraflops tf' is a direct passthrough to terraform
teraflops tf init
teraflops tf apply
# 'teraflops nix' is a direct passthrough to colmena
teraflops nix repl
teraflops nix apply --rebootterapflops implements the set-args command from NixOps. Referencing the example from NixOps:
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
teraflops.url = "github:aanderse/teraflops";
};
outputs = { nixpkgs, teraflops, ... }: {
teraflops =
{ maintenance ? false }:
{
machine =
{ config, pkgs, ... }:
{ services.httpd.enable = maintenance;
...
};
};
};
}You can pass deployment arguments using the set-args command. For example, if we want to set the maintenance argument to true in the previous example, you can run:
teraflops set-args --arg maintenance trueIn addition to the regular nix module inputs and those defined by calls to the set-args command the following arguments are available to teraflops modules:
outputs: The fully evaluated terraform output values. Generally these aren't as useful interaflopsas they are interraformbecause theteraflops evalcommand has full access to aresourcesargument which accounts for most use cases interraform.resources: The fully evaluatedterraformresource set, which includesresource,data,module, etc... objects representing the full state of your deployment.tf: A minor helper which is most useful for thetf.reffunction it contains which is used to createterraformreferences, just like in terranix.
NOTE: Both outputs and resources will be null when a teraflops module is evaluated for the purpose of generating terraform code in order to avoid recursion.
teraflops provides support for opentofu via nixpkgs. See examples/opentofu for a working example.
colmenais entirely statelessteraflopscan make full use ofterraformstate
-
terranixbuilds a high levelnixapi on top ofterraformwhich includes full build time validation -
teraflopsexposesterraformdirectly to you throughnix, sacraficing build time validation in favor of run time validation in order to make the development of variousteraflopsbackends (likedigitalocean,hetznercloud,linode,lxd, etc...) extremely quick and easy in the spirit of RFC42 -
terranixfocuses onterraformcode generation and leaves NixOS integration to the user -
teraflopsprovides full and direct integration with NixOS
-
NixOpsbuilds a high levelnixapi on top of various cloud providers which includes full build time validation, though requires extensivepythondevelopment for every backend desired, many of which do not yet exist -
teraflopsleveragesterraformfor all of this work so as long as aterraformbackend exists it is near trivial to create ateraflopsmodule for it -
NixOpsdevelopment is has lagged for a number of years, though apparently there are plans to bring it back! -
teraflopsis a young project and relies on established software likecolmena,terraform, andnixos-infectto provide all major functionality makingteraflopsalready quite a capable tool
A very quick python script I hacked together which isn't great. Don't look at the code yet... really 😅
- colmena - used by
teraflopsto manage deployments - NixOps - inspiration for
teraflops - nixos-infect - used by
teraflopsfor integration with various cloud providers - terranix - inspiration for
teraflops