-
-
Notifications
You must be signed in to change notification settings - Fork 126
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
terraform: add var.extra_build_env_vars
, closes #413
#414
Changes from 2 commits
66dda84
85f43c8
14f9c29
6ed995d
773d12d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
#!/usr/bin/env bash | ||
set -efu | ||
|
||
declare file attribute nix_options | ||
eval "$(jq -r '@sh "attribute=\(.attribute) file=\(.file) nix_options=\(.nix_options)"')" | ||
declare file attribute nix_options environment | ||
eval "$(jq -r '@sh "attribute=\(.attribute) file=\(.file) nix_options=\(.nix_options) environment=\(.environment)"')" | ||
options=$(echo "${nix_options}" | jq -r '.options | to_entries | map("--option \(.key) \(.value)") | join(" ")') | ||
vars=$(echo "${environment}" | jq -r "to_entries | map(\"\(.key)='\(.value)'\") | join(\" \")") | ||
if [[ -n ${file-} ]] && [[ -e ${file-} ]]; then | ||
# shellcheck disable=SC2086 | ||
out=$(nix build --no-link --json $options -f "$file" "$attribute") | ||
out=$(eval "env ${vars} nix build --no-link --json --impure $options -f '$file' '$attribute'") | ||
printf '%s' "$out" | jq -c '.[].outputs' | ||
else | ||
# shellcheck disable=SC2086 | ||
out=$(nix build --no-link --json $options "$attribute") | ||
out=$(eval "env ${vars} nix build --no-link --json --impure $options '$attribute'") | ||
printf '%s' "$out" | jq -c '.[].outputs' | ||
fi |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,9 @@ variable "nix_options" { | |
description = "the options of nix" | ||
default = {} | ||
} | ||
|
||
variable "extra_build_env_vars" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in general it would be fine to have environment variables or flags passed to nixos-rebuild. This could be used for injecting binary cache configuration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, i'll check it out. i still got a few question marks around say how that would handle conditionals based on environment variables, but i guess i can just try that. |
||
type = map(string) | ||
description = "Extra environment variables to be passed to the build." | ||
default = {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mhm
--impure
by default is not so nice.Have you considered the following approach?
https://github.com/NixOS/nixos-wiki-infra/blob/main/terraform/nixos-wiki/nixos_vars.tf
Basically one can use terraform to generate a json file and using
git add
to make sure it's recognized by the flake. Maybe we can encode this pattern into the terraform module instead of usingbuiltins.getEnv
?The big upside on this is that, you are no longer forced to always have to use terraform to build or deploy your nixos configuration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just imagine you want your machines to be build by CI. Before you could just do
nixos-rebuild build --flake .mymachine
, now you have to do some sort ofterraform plan
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interface wise I would propose a
nix-file
module that would usegit add --intent-to-add --force -- ./file >/dev/null 2>&1 || true
to make sure that it is added to your git repository (if one exists). It would takecontent
as a parameter:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm. if one could make a file without terraform, why couldn't one similarly make env vars, if the nixos configuration is indeed made to require them?
locally such a
nix-file
module might give some noise in version control, tho one could opt to just never usegit add .
anymore. whereas deploying from CI might circumvent that, it would seem a bit annoying in a development setting. going by file may have use-cases as well, tho it'd be nice to see a route more amenable to the common use-case of git + flakes + development supported as well.on
--impure
, maybe a conditional could be added.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed it's possible to achieve your goal without impure evaluation. Check how to import json into a nixos configuration: https://github.com/nix-community/disko/blob/09a776702b004fdf9c41a024e1299d575ee18a7d/install-cli.nix#L56
disko-install still adds
--impure
. However it's possible to use getFlake also in a pure evaluation: Check out this code in clan: https://git.clan.lol/clan/clan-core/src/commit/bcf2cd1814bbef57c74b0b4eec8f6a7cc0479475/pkgs/clan-cli/clan_cli/machines/machines.py#L229There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KiaraGrouwstra@75e09d1 also looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mic92 thanks for elaborating - i originally hadn't quite figured out the tempfile thing. i'll try and see if I can use your mentioned approach to resolve the flake+git friction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notes to self to reconcile the PRs, before i forget: info may be passed purely without staging to git thru
nix build --expr
, reading a file bybuiltins.fetchTree
to pass the content as file to avoid stack overflows based on content size. the originallib.nixosSystem
can then be wrapped so as to e.g. write the extra info to some file.edit: potential alternative:
nix build path:.