Skip to content

Commit 646cacb

Browse files
authored
Add Flakes support (#57)
1 parent 73519ae commit 646cacb

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

deploy_nixos/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ difference will be detected on the next "terraform plan".
1212

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

1618
### Secret handling
1719

@@ -106,6 +108,7 @@ see also:
106108
| extra\_build\_args | List of arguments to pass to the nix builder | `list(string)` | `[]` | no |
107109
| extra\_eval\_args | List of arguments to pass to the nix evaluation | `list(string)` | `[]` | no |
108110
| 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 |
111+
| 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 |
109112
| keys | A map of filename to content to upload as secrets in /var/keys | `map(string)` | `{}` | no |
110113
| nixos\_config | Path to a NixOS configuration | `string` | `""` | no |
111114
| ssh\_agent | Whether to use an SSH agent. True if not ssh\_private\_key is passed | `bool` | `null` | no |

deploy_nixos/main.tf

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ variable "hermetic" {
9999
default = false
100100
}
101101

102+
variable "flake" {
103+
type = bool
104+
description = "Treat the provided nixos_config as the NixOS configuration to use in the flake located in the current directory"
105+
default = false
106+
}
107+
102108
variable "delete_older_than" {
103109
type = string
104110
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."
@@ -132,6 +138,7 @@ data "external" "nixos-instantiate" {
132138
var.NIX_PATH == "" ? "-" : var.NIX_PATH,
133139
var.config != "" ? var.config : var.nixos_config,
134140
var.config_pwd == "" ? "." : var.config_pwd,
141+
var.flake,
135142
# end of positional arguments
136143
# start of pass-through arguments
137144
"--argstr", "system", var.target_system,

deploy_nixos/nixos-instantiate.sh

+24-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@ set -euo pipefail
55
nix_path=$1
66
config=$2
77
config_pwd=$3
8-
shift 3
8+
flake=$4
9+
shift 4
910

1011

1112
command=(nix-instantiate --show-trace --expr '
12-
{ system, configuration, hermetic ? false, ... }:
13+
{ system, configuration, hermetic ? false, flake ? false, ... }:
1314
let
15+
importFromFlake = { nixosConfig }:
16+
let
17+
flake = (import (
18+
fetchTarball {
19+
url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
20+
sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
21+
) {
22+
src = ./.;
23+
}).defaultNix;
24+
in
25+
builtins.getAttr nixosConfig flake.nixosConfigurations;
1426
os =
15-
if hermetic
16-
then import configuration
17-
else import <nixpkgs/nixos> { inherit system configuration; };
27+
if flake
28+
then importFromFlake { nixosConfig = configuration; }
29+
else if hermetic
30+
then import configuration
31+
else import <nixpkgs/nixos> { inherit system configuration; };
1832
in {
1933
inherit (builtins) currentSystem;
2034
@@ -43,7 +57,11 @@ if [[ -f "$config" ]]; then
4357
config=$($readlink "$config")
4458
command+=(--argstr configuration "$config")
4559
else
46-
command+=(--arg configuration "$config")
60+
if $flake; then
61+
command+=(--argstr configuration "$config" --arg flake true)
62+
else
63+
command+=(--arg configuration "$config")
64+
fi
4765
fi
4866

4967
# add all extra CLI args as extra build arguments

0 commit comments

Comments
 (0)