-
Notifications
You must be signed in to change notification settings - Fork 1
/
sops.nix
76 lines (67 loc) · 1.8 KB
/
sops.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
{ lib
, callPackage
, runCommandNoCCLocal
, ssh-to-age
, ...
}:
with lib;
let
adminKey = ''3237CA7A1744B4DCE96B409FB4C3BF012D9B26BE'';
inherit (callPackage ./machines.nix { }) machines;
sshToKey = name: path:
if builtins.pathExists path
then
runCommandNoCCLocal "sops-key-${name}.pub" { } ''
${ssh-to-age}/bin/ssh-to-age < ${path} > $out
''
else null;
machineKey = machine:
let
keyFile = sshToKey "machine-${machine.name}" /${machine.path}/gathered/ssh_host_ed25519_key.pub;
in
if keyFile != null
then removeSuffix "\n" (readFile keyFile)
else null;
machine_rules =
let
# Walk a machine and its parent groups and give a list of all related paths
paths = machine:
let
walk = e:
[ e.relPath ] ++ (optionals (e.parent != null) (walk e.parent));
in
walk machine;
# Expand all machines into all related path and assign the machines keys to those paths
pathKeys = foldAttrs
concat [ ]
(map # Build list of { <path> = <key> }
(machine: listToAttrs (map
(path:
let
key = machineKey machine;
in
nameValuePair path (optional (key != null) key))
(paths machine)))
machines);
in
mapAttrsToList
(path: keys: {
"path_regex" = "^${escapeRegex path}/(${escapeRegex "secrets.yaml"}|secrets/.+)$";
"key_groups" = [{
"age" = keys;
"pgp" = [ adminKey ];
}];
})
pathKeys;
in
{
config = {
"creation_rules" = machine_rules ++ [{
"relPath" = "^${escapeRegex "modules/secrets.yaml"}$";
"key_groups" = [{
"age" = remove null (map machineKey machines);
"pgp" = [ adminKey ];
}];
}];
};
}