-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
flake.nix
294 lines (257 loc) · 13.8 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
{
description = "Lollypops - Lollypop Operations Deployment Tool";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, ... }@inputs:
with inputs;
{
nixosModules.lollypops = import ./module.nix;
nixosModules.default = self.nixosModules.lollypops;
hmModule = import ./hm-module.nix;
} //
# TODO test/add other plattforms
# (flake-utils.lib.eachDefaultSystem)
(flake-utils.lib.eachSystem (flake-utils.lib.defaultSystems ++ [ "aarch64-darwin" ]))
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages = rec {
default = docs;
docs = pkgs.callPackage ./docs.nix {
filterPrefix = "lollypops";
title = "Lollypops options";
modules = [
(import ./module.nix {
inherit pkgs;
lib = pkgs.lib;
config.networking.hostName = "hostname";
})
{
# Fake this so depending options documentation don't fail
options.environment = pkgs.lib.mkOption { description = "test"; };
}
];
};
};
# Allow custom packages to be run using `nix run`
apps =
let
# Build steps for all secrets of all users
mkSeclistUser = homeUsers: pkgs.lib.lists.flatten (builtins.attrValues (builtins.mapAttrs
(user: userconfig: [
# Deploy secrets for user 'user'
(builtins.attrValues (builtins.mapAttrs
(secretName: secretConfig: [
# Deloy secret 'secretName' of user 'user'
"echo 'Deploying ${secretName} (from user ${user}) to ${pkgs.lib.escapeShellArg secretConfig.path}'"
# Create parent directory if it does not exist
''
{{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} 'umask 076; sudo -u ${user} mkdir -p "$(dirname ${pkgs.lib.escapeShellArg secretConfig.path})"'
''
# Copy file
''
${secretConfig.cmd} | {{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} "umask 077; cat > ${pkgs.lib.escapeShellArg secretConfig.path}"
''
# # Set group and owner
''
{{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} "chown ${secretConfig.owner}:${secretConfig.group-name} ${pkgs.lib.escapeShellArg secretConfig.path}"
''
])
userconfig.lollypops.secrets.files))
])
homeUsers));
in
{
default = { configFlake, ... }:
let
mkTaskFileForHost = hostName: hostConfig: pkgs.writeText "CommonTasks.yml"
(builtins.toJSON {
version = "3";
output = "prefixed";
# Set global shell options:
# set -o pipefail -e
set = [
"e"
"pipefail"
];
vars = with hostConfig.config.lollypops; {
REMOTE_USER = ''{{default "${deployment.ssh.user}" .LP_REMOTE_USER}}'';
REMOTE_HOST = ''{{default "${deployment.ssh.host}" .LP_REMOTE_HOST}}'';
REMOTE_COMMAND = ''{{default "${deployment.ssh.command}" .LP_REMOTE_COMMAND}}'';
REMOTE_SSH_OPTS = ''{{default "${pkgs.lib.concatStrings deployment.ssh.opts}" .LP_REMOTE_SSH_OPTS}}'';
REMOTE_SUDO_COMMAND = ''{{default "${deployment.sudo.command}" .LP_REMOTE_SUDO_COMMAND}}'';
REMOTE_SUDO_OPTS = ''{{default "${pkgs.lib.concatStrings deployment.sudo.opts}" .LP_REMOTE_SUDO_OPTS}}'';
REBUILD_ACTION = ''{{default "switch" .REBUILD_ACTION}}'';
REMOTE_CONFIG_DIR = deployment.config-dir;
LOCAL_FLAKE_SOURCE = configFlake;
HOSTNAME = hostName;
};
tasks =
let
useSudo = hostConfig.config.lollypops.deployment.sudo.enable;
in
with pkgs.lib; {
check-vars.preconditions = [{
sh = ''[ ! -z "{{.HOSTNAME}}" ]'';
msg = "HOSTNAME not set: {{.HOSTNAME}}";
}];
deploy-secrets =
let
mkSeclist = config: lists.flatten (map
(x:
let
path = escapeShellArg x.path;
in
[
"echo 'Deploying ${x.name} to ${path}'"
# Create parent directory if it does not exist
''
{{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} \
'umask 076; ${optionalString useSudo "{{.REMOTE_SUDO_COMMAND}} {{.REMOTE_SUDO_OPTS}} "} mkdir -p "$(dirname ${path})"'
''
# Copy file
''
${x.cmd} | {{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} \
"${optionalString useSudo "{{.REMOTE_SUDO_COMMAND}} {{.REMOTE_SUDO_OPTS}}"} \
install -m 700 /dev/null ${path}; \
${optionalString useSudo "{{.REMOTE_SUDO_COMMAND}} {{.REMOTE_SUDO_OPTS}}"} \
tee ${path} > /dev/null"
''
# Set group and owner
''
{{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} \
"${optionalString useSudo "{{.REMOTE_SUDO_COMMAND}} {{.REMOTE_SUDO_OPTS}}"} \
chown ${x.owner}:${x.group-name} ${path}"
''
])
(builtins.attrValues config.lollypops.secrets.files));
in
{
deps = [ "check-vars" ];
desc = "Deploy secrets to: ${hostName}";
cmds = [
''echo "Deploying secrets to: {{.HOSTNAME}}"''
]
++ mkSeclist hostConfig.config
++ (
# Check for home-manager
if builtins.hasAttr "home-manager" hostConfig.config then
(
# Check for lollypops hmModule
if builtins.hasAttr "lollypops" hostConfig.config.home-manager then
mkSeclistUser hostConfig.config.home-manager.users else [ ]
)
else [ ]
);
};
rebuild = {
dir = self;
desc = "Rebuild configuration of: ${hostName}";
deps = [ "check-vars" ];
cmds = [
(if hostConfig.config.lollypops.deployment.local-evaluation then
''
${optionalString useSudo ''NIX_SSHOPTS="{{.REMOTE_SSH_OPTS}}"''} nixos-rebuild {{.REBUILD_ACTION}} \
--flake '{{.LOCAL_FLAKE_SOURCE}}#{{.HOSTNAME}}' \
--target-host {{.REMOTE_USER}}@{{.REMOTE_HOST}} \
${optionalString useSudo "--use-remote-sudo"}
'' else ''
{{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} \
'${optionalString useSudo "{{.REMOTE_SUDO_COMMAND}} {{.REMOTE_SUDO_OPTS}}"} nixos-rebuild {{.REBUILD_ACTION}} \
--flake "$(readlink -f {{.REMOTE_CONFIG_DIR}}/flake)#{{.HOSTNAME}}"'
'')
];
};
deploy-flake = {
deps = [ "check-vars" ];
desc = "Deploy flake repository to: ${hostName}";
cmds = [
''echo "Deploying flake to: {{.HOSTNAME}}"''
(if hostConfig.config.lollypops.deployment.deploy-method == "archive" then
''
NIX_SSHOPTS="{{.REMOTE_SSH_OPTS}}" nix flake archive \
--to ssh://{{.REMOTE_USER}}@{{.REMOTE_HOST}} \
--option builders-use-substitutes true \
{{.LOCAL_FLAKE_SOURCE}}
''
else
''
NIX_SSHOPTS="{{.REMOTE_SSH_OPTS}}" nix copy \
--to ssh://{{.REMOTE_USER}}@{{.REMOTE_HOST}} \
--substitute-on-destination \
--option builders-use-substitutes true \
{{.LOCAL_FLAKE_SOURCE}}
''
)
''
{{.REMOTE_COMMAND}} {{.REMOTE_SSH_OPTS}} {{.REMOTE_USER}}@{{.REMOTE_HOST}} \
"${optionalString useSudo "{{.REMOTE_SUDO_COMMAND}} {{.REMOTE_SUDO_OPTS}}"} ln -sfn {{.LOCAL_FLAKE_SOURCE}} {{.REMOTE_CONFIG_DIR}}/flake"
''
];
};
} // hostConfig.config.lollypops.extraTasks;
});
# Group hosts by their group name
hostGroups =
let
processHost = currentGroups: host:
let
groupName = host.config.lollypops.deployment.group;
existing = currentGroups."${groupName}" or [ ];
in
# Either add the host to an existing group or create a new group list
currentGroups // { ${groupName} = existing ++ [ host.config.system.name ]; };
in
builtins.foldl' processHost { } (builtins.attrValues configFlake.nixosConfigurations);
# Taskfile passed to go-task
taskfile = pkgs.writeText
"Taskfile.yml"
(builtins.toJSON {
version = "3";
output = "prefixed";
# Don't print executed commands. Can be overridden by -v
silent = true;
# Import the tasks once for each host, setting the HOST
# variable. This allows running them as `host:task` for
# each host individually.
includes = builtins.mapAttrs
(name: value:
{
taskfile = mkTaskFileForHost name value;
})
configFlake.nixosConfigurations;
# Define grouped tasks to run all tasks for one host.
# E.g. to make a complete deployment for host "server01":
# `nix run '.' -- server01
tasks = builtins.mapAttrs
(name: value:
{
desc = "Provision host: ${name}";
cmds = map (task: { task = "${name}:${task}"; }) value.config.lollypops.tasks;
})
configFlake.nixosConfigurations // builtins.mapAttrs
(groupName: hosts:
{
desc = "Provision group: ${groupName}";
deps = map (host: { task = "${host}"; }) hosts;
})
hostGroups // {
# Add special task called "all" which has all hosts as
# dependency to deploy all hosts at once
all.deps = map (x: { task = x; }) (builtins.attrNames configFlake.nixosConfigurations);
};
});
in
flake-utils.lib.mkApp
{
drv = pkgs.writeShellScriptBin "go-task-runner" ''
${pkgs.go-task}/bin/task -t ${taskfile} "$@"
'';
};
};
});
}