-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
168 lines (160 loc) · 5.44 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{
inputs = {
# arion's nixpkgs input must be named nixpkgs
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
nixpkgs-guest.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
unfree = {
url = "github:numtide/nixpkgs-unfree";
inputs.nixpkgs.follows = "nixpkgs";
};
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
disko = {
url = "github:nix-community/disko";
inputs.nixpkgs.follows = "nixpkgs-guest";
};
sops-nix = {
url = "github:mic92/sops-nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.nixpkgs-stable.follows = "nixpkgs";
};
srvos = {
url = "github:nix-community/srvos";
inputs.nixpkgs.follows = "nixpkgs-guest";
};
nixos-anywhere = {
url = "github:KiaraGrouwstra/nixos-anywhere/tf-special-args";
inputs.nixpkgs.follows = "nixpkgs-guest";
inputs.disko.follows = "disko";
inputs.flake-parts.follows = "flake-parts";
inputs.treefmt-nix.follows = "treefmt-nix";
};
flake-utils.url = "github:numtide/flake-utils";
flake-parts.url = "github:hercules-ci/flake-parts";
arion = {
url = "github:KiaraGrouwstra/arion/kiara";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-parts.follows = "flake-parts";
};
impermanence.url = "github:nix-community/impermanence";
};
outputs = {
self,
nixpkgs,
...
} @ inputs: let
system = "x86_64-linux";
inherit (nixpkgs) lib;
# nixos configs to deploy by nixos-anywhere
nixosConfigurations = system: let
inherit (inputs.nixpkgs-guest) lib legacyPackages;
inherit (legacyPackages."${system}") pkgs;
util = import ./lib {inherit pkgs lib;};
in
# assumption: server name = config name
{
combined = lib.nixosSystem {
inherit system;
specialArgs = {
inherit
inputs
util
;
};
modules = [
./servers/common/vm.nix
./servers/common
./hcloud
./hcloud/disk-config.nix
{
nixpkgs.hostPlatform = system;
}
];
};
};
in
lib.attrsets.recursiveUpdate {
nixosConfigurations = nixosConfigurations system;
} (inputs.flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages."${system}";
inherit (pkgs) lib;
in {
inherit pkgs lib;
# for `nix fmt`
formatter = (inputs.treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper;
devShells.default = let
# https://github.com/NixOS/nixpkgs/issues/283015
tofuProvider = provider:
provider.override (oldArgs: {
provider-source-address =
lib.replaceStrings
["https://registry.terraform.io/providers"]
["registry.opentofu.org"]
oldArgs.homepage;
});
in
pkgs.mkShell {
pname = "nixos-hcloud";
packages = let
tfPlugins = p: [
p.hcloud
p.ssh
p.tls
p.null
p.external
p.cloudflare
p.hetznerdns
];
in [
inputs.arion.packages.${system}.default
pkgs.direnv
pkgs.rage
pkgs.colmena
(pkgs.opentofu.withPlugins (p: pkgs.lib.lists.map tofuProvider (tfPlugins p)))
inputs.nixos-anywhere.packages.${system}.default
pkgs.jq
];
};
terraform = let
pkgs = nixpkgs.legacyPackages."${system}";
# possible TF blocks: https://opentofu.org/docs/language/syntax/json/#block-type-specific-exceptions
options = lib.genAttrs ["data" "locals" "module" "output" "provider" "resource" "terraform" "variable"] (_k: lib.mkOption {default = {};});
# modules to load
evaluated = lib.evalModules {
modules = [
{inherit options;}
(import ./terraform.nix {inherit lib pkgs inputs;})
];
};
# TF dislikes empty stuff
sanitized = lib.mapAttrs (_k: lib.filterAttrs (_k: v: v != {})) (lib.filterAttrs (_k: v: v != {}) evaluated.config);
in
sanitized;
nixosConfigurations = nixosConfigurations system;
apps =
lib.mapAttrs (name: script: {
type = "app";
program = toString (pkgs.writers.writeBash name script);
}) {
vm = ''
nixos-rebuild build-vm --flake .#combined && ./result/bin/run-combined-vm
'';
convert = "nix eval --json .#terraform.${system} | jq > main.tf.json";
clean = "rm -rf .terraform/ && rm -f terraform.tfstate* && rm -rf terraform.tfstate.d/";
destroy = ''
tofu destroy
for f in "config.tf.json *.tfstate* *.tfvars.json ci.tfrc .terraform terraform.tfstate.d"; do
echo $f
if [[ -e "${toString ./.}/$f" ]]; then
rm -rf "${toString ./.}/$f";
fi;
done
'';
import = ''eval $(tofu show -json | jq -r '.values.root_module.resources | map(select(.mode == "data") | .type as $type | .values[.type[7:]] | map("tofu import " + $type[0:-1] + "." + .name + " " + (.id | tostring) + ";"))[][]')'';
};
# nix run
defaultApp = self.apps.${system}.convert;
}));
}