about summary refs log tree commit diff
path: root/ops/terraform/deploy-nixos/nixos-eval.sh
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-01-29T16·44+0300
committertazjin <tazjin@tvl.su>2023-03-03T10·48+0000
commitdbca46d05205bdaab07d4faf1899a6452f34dd0f (patch)
treecdaf75f1bcf73dc70075d6c570cf3e42856b7019 /ops/terraform/deploy-nixos/nixos-eval.sh
parent0b64577702ca7e412bb2af4af7af9e33efc5c0f5 (diff)
feat(ops/terraform): add module for deploying NixOS system closures r/5867
This module makes it fairly easy to deploy NixOS system closures using
Terraform, while properly separating the evaluation of a
derivation (to determine whether a deploy is needed) from the building
and copying of the closure itself.

This has been on my stack for a while. It was originally developed for
Resoptima, who agreed to open-sourcing it in depot back when we
completed our work with them. Their contribution has been acknowledged
in the README.

Co-Authored-By: Florian Klink <flokli@flokli.de>
Change-Id: Ica4c170658cd25f1fb7072c9a45735fcc4351474
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7950
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'ops/terraform/deploy-nixos/nixos-eval.sh')
-rwxr-xr-xops/terraform/deploy-nixos/nixos-eval.sh16
1 files changed, 16 insertions, 0 deletions
diff --git a/ops/terraform/deploy-nixos/nixos-eval.sh b/ops/terraform/deploy-nixos/nixos-eval.sh
new file mode 100755
index 0000000000..dd15784b1b
--- /dev/null
+++ b/ops/terraform/deploy-nixos/nixos-eval.sh
@@ -0,0 +1,16 @@
+#!/usr/bin/env bash
+#
+# Builds a NixOS system configuration at the given attribute path.
+set -ueo pipefail
+
+# Load input variables from Terraform. jq's @sh format takes care of
+# escaping.
+eval "$(jq -r '@sh "ATTRPATH=\(.attrpath)"')"
+
+# Evaluate the system derivation.
+# TODO: configurable REPO_ROOT
+REPO_ROOT=$(git rev-parse --show-toplevel)
+SYSTEM_DRV=$(nix-instantiate -A "${ATTRPATH}" "${REPO_ROOT}")
+
+# Return system derivation back to Terraform.
+jq -n --arg drv "$SYSTEM_DRV" '{"drv":$drv}'