Skip to content

Add Flakes support #57

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

Merged
merged 1 commit into from
Aug 26, 2021
Merged
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
3 changes: 3 additions & 0 deletions deploy_nixos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ difference will be detected on the next "terraform plan".

Either pass a "config" which is a dynamic nixos configuration and a
"config_pwd", or a "nixos_config", a path to a nixos configuration.nix file.
If you have defined your NixOs configuration in a Flake, use "nixos_config"
to specify the name of the attribue and set "flake" to true.

### Secret handling

Expand Down Expand Up @@ -106,6 +108,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 |
| flake | Treat the provided nixos_config as the name of the NixOS configuration to use in the flake located in the current directory. Useful if you customize eval-modules or use a pinned nixpkgs. | `bool` | false | 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
7 changes: 7 additions & 0 deletions deploy_nixos/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ variable "hermetic" {
default = false
}

variable "flake" {
type = bool
description = "Treat the provided nixos_config as the NixOS configuration to use in the flake located in the current directory"
default = false
}

variable "delete_older_than" {
type = string
description = "Can be a list of generation numbers, the special value old to delete all non-current generations, a value such as 30d to delete all generations older than the specified number of days (except for the generation that was active at that point in time), or a value such as +5 to keep the last 5 generations ignoring any newer than current, e.g., if 30 is the current generation +5 will delete generation 25 and all older generations."
Expand Down Expand Up @@ -132,6 +138,7 @@ data "external" "nixos-instantiate" {
var.NIX_PATH == "" ? "-" : var.NIX_PATH,
var.config != "" ? var.config : var.nixos_config,
var.config_pwd == "" ? "." : var.config_pwd,
var.flake,
# end of positional arguments
# start of pass-through arguments
"--argstr", "system", var.target_system,
Expand Down
30 changes: 24 additions & 6 deletions deploy_nixos/nixos-instantiate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ set -euo pipefail
nix_path=$1
config=$2
config_pwd=$3
shift 3
flake=$4
shift 4


command=(nix-instantiate --show-trace --expr '
{ system, configuration, hermetic ? false, ... }:
{ system, configuration, hermetic ? false, flake ? false, ... }:
let
importFromFlake = { nixosConfig }:
let
flake = (import (
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
) {
src = ./.;
}).defaultNix;
in
builtins.getAttr nixosConfig flake.nixosConfigurations;
os =
if hermetic
then import configuration
else import <nixpkgs/nixos> { inherit system configuration; };
if flake
then importFromFlake { nixosConfig = configuration; }
else if hermetic
then import configuration
else import <nixpkgs/nixos> { inherit system configuration; };
in {
inherit (builtins) currentSystem;

Expand Down Expand Up @@ -43,7 +57,11 @@ if [[ -f "$config" ]]; then
config=$($readlink "$config")
command+=(--argstr configuration "$config")
else
command+=(--arg configuration "$config")
if $flake; then
command+=(--argstr configuration "$config" --arg flake true)
else
command+=(--arg configuration "$config")
fi
fi

# add all extra CLI args as extra build arguments
Expand Down