Skip to content

Allow to pass arguments to a hermetic configuration if it's a function #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deploy_nixos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ see also:
| extra\_build\_args | List of arguments to pass to the nix builder | `list(string)` | `[]` | no |
| extra\_eval\_args | List of arguments to pass to the nix evaluation | `list(string)` | `[]` | no |
| hermetic | Treat the provided nixos configuration as a hermetic expression and do not evaluate using the ambient system nixpkgs. Useful if you customize eval-modules or use a pinned nixpkgs. | `bool` | false | no |
| arguments | Attribute set passed to hermetic configuration if it is a function. | `map(any)` | `{}` | no |
| keys | A map of filename to content to upload as secrets in /var/keys | `map(string)` | `{}` | no |
| nixos\_config | Path to a NixOS configuration | `string` | `""` | no |
| ssh\_agent | Whether to use an SSH agent. True if not ssh\_private\_key is passed | `bool` | `null` | no |
Expand Down
10 changes: 8 additions & 2 deletions deploy_nixos/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ variable "triggers" {
default = {}
}

variable "arguments" {
type = map(any)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type = map(any)
type = any

According to https://www.terraform.io/language/expressions/type-constraints (and testing), map(any) requires that all values be of the same type. This will (for example) coerce boolean values to strings, if any of the values are strings.

description = "A map of values to pass to the Nix expression. It only works form 'hermetic' configurations. For secrets, use 'keys' instead."
default = {}
}

variable "keys" {
type = map(string)
description = "A map of filename to content to upload as secrets in /var/keys"
Expand Down Expand Up @@ -129,7 +135,8 @@ data "external" "nixos-instantiate" {
# end of positional arguments
# start of pass-through arguments
"--argstr", "system", var.target_system,
"--arg", "hermetic", var.hermetic
"--arg", "hermetic", var.hermetic,
"--argstr", "argumentsJson", jsonencode(var.arguments)
],
var.extra_eval_args,
)
Expand Down Expand Up @@ -197,4 +204,3 @@ output "id" {
description = "random ID that changes on every nixos deployment"
value = null_resource.deploy_nixos.id
}

12 changes: 10 additions & 2 deletions deploy_nixos/nixos-instantiate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ shift 3


command=(nix-instantiate --show-trace --expr '
{ system, configuration, hermetic ? false, ... }:
{ system, configuration, hermetic ? false, argumentsJson, ... }:
let
arguments = builtins.fromJSON argumentsJson;

os =
if hermetic
then import configuration
then
let
config = import configuration;
in
if builtins.isFunction config
then config arguments
else config
else import <nixpkgs/nixos> { inherit system configuration; };
in {
inherit (builtins) currentSystem;
Expand Down