From 17b7aed78b4fb76498ee142c4aeb3ca46ef5133b Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Thu, 19 Dec 2024 15:28:44 +0100 Subject: [PATCH] support for --argstr in the nix-build tf module --- terraform/nix-build/main.tf | 4 ++++ terraform/nix-build/nix-build.sh | 11 ++++++++--- terraform/nix-build/variables.tf | 6 ++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/terraform/nix-build/main.tf b/terraform/nix-build/main.tf index 7a3c335f..7cc07fbd 100644 --- a/terraform/nix-build/main.tf +++ b/terraform/nix-build/main.tf @@ -1,4 +1,7 @@ locals { + nix_argstrs = jsonencode({ + argstrs = { for k, v in var.nix_argstrs : k => v } + }) nix_options = jsonencode({ options = { for k, v in var.nix_options : k => v } }) @@ -8,6 +11,7 @@ data "external" "nix-build" { query = { attribute = var.attribute file = var.file + nix_argstrs = local.nix_argstrs nix_options = local.nix_options special_args = jsonencode(var.special_args) } diff --git a/terraform/nix-build/nix-build.sh b/terraform/nix-build/nix-build.sh index d675341f..fdb6b5b2 100755 --- a/terraform/nix-build/nix-build.sh +++ b/terraform/nix-build/nix-build.sh @@ -1,8 +1,13 @@ #!/usr/bin/env bash set -efu -declare file attribute nix_options special_args -eval "$(jq -r '@sh "attribute=\(.attribute) file=\(.file) nix_options=\(.nix_options) special_args=\(.special_args)"')" +declare file attribute nix_argstrs nix_options special_args +eval "$(jq -r '@sh "attribute=\(.attribute) file=\(.file) nix_argstrs=\(.nix_argstrs) nix_options=\(.nix_options) special_args=\(.special_args)"')" +if [ "${nix_argstrs}" != '{"argstrs":{}}' ]; then + argstrs=$(echo "${nix_argstrs}" | jq -r '.argstrs | to_entries | map("--argstr \(.key) \(.value)") | join(" ")') +else + argstrs="" +fi if [ "${nix_options}" != '{"options":{}}' ]; then options=$(echo "${nix_options}" | jq -r '.options | to_entries | map("--option \(.key) \(.value)") | join(" ")') else @@ -12,7 +17,7 @@ if [[ ${special_args-} == "{}" ]]; then # no special arguments, proceed as normal if [[ -n ${file-} ]] && [[ -e ${file-} ]]; then # shellcheck disable=SC2086 - out=$(nix build --no-link --json $options -f "$file" "$attribute") + out=$(nix build --no-link --json $argstrs $options -f "$file" "$attribute") else # shellcheck disable=SC2086 out=$(nix build --no-link --json ${options} "$attribute") diff --git a/terraform/nix-build/variables.tf b/terraform/nix-build/variables.tf index bad23f73..edb6622b 100644 --- a/terraform/nix-build/variables.tf +++ b/terraform/nix-build/variables.tf @@ -9,6 +9,12 @@ variable "file" { default = null } +variable "nix_argstrs" { + type = map(string) + description = "the argstrs of nix" + default = {} +} + variable "nix_options" { type = map(string) description = "the options of nix"