/
server.nix
146 lines (132 loc) · 4.12 KB
/
server.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
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.hcloud.server;
allAdmins =
if (hasAttr "admins" config.users) then config.users.admins else { };
in {
options.hcloud.server = mkOption {
default = { };
description = ''
servers deployed to the hcloud.
'';
type = with types;
attrsOf (submodule ({ name, ... }: {
options = {
enable = mkEnableOption ''
really deploy server
'';
name = mkOption {
default = "${name}";
type = with types; str;
description = ''
name of the server
'';
};
provisioners = mkOption {
default = [ ];
type = with types; listOf attrs;
description = ''
provision steps.
`file` and `remote-exec` steps
automatically get the connection
part setup, if you configure
the `provisioner.privateKeyFile`
parameter.
'';
example = [
{
file.source = "~/.zshrc";
file.destination = "/root/.zshrc";
}
{ remote-exec.inline = [ "shutdown -r +1" ]; }
];
};
image = mkOption {
default = "ubuntu-18.04";
type = with types; str;
description = ''
image to spawn on the server
'';
};
serverType = mkOption {
default = "cx11";
type = with types; str;
description = ''
Hardware equipment. This options influences costs!
'';
};
backups = mkOption {
default = false;
type = with types; bool;
description = ''
enable backups or not
'';
};
};
}));
};
config = mkIf (cfg != { }) {
hcloud.enable = true;
resource.hcloud_server = let
allUsers =
mapAttrsToList (name: ignore: "\${ hcloud_ssh_key.server_${name}.id }")
allAdmins;
allResources = mapAttrs' (name: configuration: {
name = "${configuration.name}";
value = {
# todo : check if name contains _ which is not allowed by hcloud
name = configuration.name;
image = configuration.image;
server_type = configuration.serverType;
backups = configuration.backups;
ssh_keys = allUsers;
provisioner = builtins.map (provisioner:
if (builtins.hasAttr "file" provisioner) then {
"file" = ({
connection = {
type = "ssh";
user = "root";
private_key =
''''${ file("${config.provisioner.privateKeyFile}") }'';
};
} // provisioner.file);
} else if (builtins.hasAttr "remote-exec" provisioner) then {
"remote-exec" = ({
connection = {
type = "ssh";
user = "root";
private_key =
''''${ file("${config.provisioner.privateKeyFile}") }'';
};
} // provisioner.remote-exec);
} else
provisioner) configuration.provisioners;
};
}) cfg;
in allResources;
output = let
ipv4Address = mapAttrs' (ignore: configuration: {
name = "${configuration.name}_ipv4_address";
value = {
value = "\${ hcloud_server.${configuration.name}.ipv4_address }";
};
}) cfg;
ipv6Address = mapAttrs' (ignore: configuration: {
name = "${configuration.name}_ipv6_address";
value = {
value = "\${ hcloud_server.${configuration.name}.ipv6_address }";
};
}) cfg;
in ipv4Address // ipv6Address;
resource.hcloud_ssh_key = let
allResources = mapAttrs' (name: configuration: {
name = "server_${name}";
value = {
name = "SSH Key ${name}";
public_key = configuration.publicKey;
};
}) allAdmins;
in allResources;
};
}