-
Notifications
You must be signed in to change notification settings - Fork 32
/
secrets.nix
65 lines (63 loc) · 1.78 KB
/
secrets.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
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.krops.secrets;
secret-file = types.submodule ({ config, ... }: {
options = {
name = mkOption {
type = types.str;
default = config._module.args.name;
};
path = mkOption {
type = types.str;
default = "/run/keys/${config.name}";
};
mode = mkOption {
type = types.str;
default = "0400";
};
owner = mkOption {
type = types.str;
default = "root";
};
group-name = mkOption {
type = types.str;
default = "root";
};
source-path = mkOption {
type = types.str;
default = toString <secrets> + "/${config.name}";
};
};
});
in {
options.krops.secrets = {
files = mkOption {
type = with types; attrsOf secret-file;
default = {};
};
};
config = lib.mkIf (cfg.files != {}) {
system.activationScripts.setup-secrets = let
files = unique (map (flip removeAttrs ["_module"])
(attrValues cfg.files));
script = ''
echo setting up secrets...
mkdir -p /run/keys -m 0750
chown root:keys /run/keys
${concatMapStringsSep "\n" (file: ''
${pkgs.coreutils}/bin/install \
-D \
--compare \
--verbose \
--mode=${lib.escapeShellArg file.mode} \
--owner=${lib.escapeShellArg file.owner} \
--group=${lib.escapeShellArg file.group-name} \
${lib.escapeShellArg file.source-path} \
${lib.escapeShellArg file.path} \
|| echo "failed to copy ${file.source-path} to ${file.path}"
'') files}
'';
in stringAfter [ "users" "groups" ] "source ${pkgs.writeText "setup-secrets.sh" script}";
};
}