|
1 | 1 | { config, pkgs, lib, options, ... }: |
2 | | -with lib; |
3 | | -with lib.types; |
4 | | -with builtins; |
5 | 2 | let |
6 | | - cfg = config.sops; |
7 | | - secretsForUsers = lib.filterAttrs (_: v: v.neededForUsers) cfg.secrets; |
| 3 | + inherit (lib) |
| 4 | + mkOption |
| 5 | + mkDefault |
| 6 | + mapAttrs |
| 7 | + types |
| 8 | + ; |
| 9 | + |
8 | 10 | users = config.users.users; |
9 | | - useSystemdActivation = (options.systemd ? sysusers && config.systemd.sysusers.enable) || |
10 | | - (options.services ? userborn && config.services.userborn.enable); |
11 | | - renderScript = '' |
12 | | - echo Setting up sops templates... |
13 | | - ${concatMapStringsSep "\n" (name: |
14 | | - let |
15 | | - tpl = config.sops.templates.${name}; |
16 | | - substitute = pkgs.writers.writePython3 "substitute" { } |
17 | | - (readFile ./subs.py); |
18 | | - subst-pairs = pkgs.writeText "pairs" (concatMapStringsSep "\n" |
19 | | - (name: |
20 | | - "${toString config.sops.placeholder.${name}} ${ |
21 | | - config.sops.secrets.${name}.path |
22 | | - }") (attrNames config.sops.secrets)); |
23 | | - in '' |
24 | | - mkdir -p "${dirOf tpl.path}" |
25 | | - (umask 077; ${substitute} ${tpl.file} ${subst-pairs} > ${tpl.path}) |
26 | | - chmod "${tpl.mode}" "${tpl.path}" |
27 | | - chown "${tpl.owner}:${tpl.group}" "${tpl.path}" |
28 | | - '') (attrNames config.sops.templates)} |
29 | | - ''; |
30 | 11 | in { |
31 | 12 | options.sops = { |
32 | 13 | templates = mkOption { |
33 | 14 | description = "Templates for secret files"; |
34 | | - type = attrsOf (submodule ({ config, ... }: { |
| 15 | + type = types.attrsOf (types.submodule ({ config, ... }: { |
35 | 16 | options = { |
36 | 17 | name = mkOption { |
37 | | - type = singleLineStr; |
| 18 | + type = types.singleLineStr; |
38 | 19 | default = config._module.args.name; |
39 | 20 | description = '' |
40 | 21 | Name of the file used in /run/secrets/rendered |
41 | 22 | ''; |
42 | 23 | }; |
43 | 24 | path = mkOption { |
44 | 25 | description = "Path where the rendered file will be placed"; |
45 | | - type = singleLineStr; |
| 26 | + type = types.singleLineStr; |
46 | 27 | default = "/run/secrets/rendered/${config.name}"; |
47 | 28 | }; |
48 | 29 | content = mkOption { |
49 | | - type = lines; |
| 30 | + type = types.lines; |
50 | 31 | default = ""; |
51 | 32 | description = '' |
52 | 33 | Content of the file |
53 | 34 | ''; |
54 | 35 | }; |
55 | 36 | mode = mkOption { |
56 | | - type = singleLineStr; |
| 37 | + type = types.singleLineStr; |
57 | 38 | default = "0400"; |
58 | 39 | description = '' |
59 | 40 | Permissions mode of the rendered secret file in octal. |
60 | 41 | ''; |
61 | 42 | }; |
62 | 43 | owner = mkOption { |
63 | | - type = singleLineStr; |
| 44 | + type = types.singleLineStr; |
64 | 45 | default = "root"; |
65 | 46 | description = '' |
66 | 47 | User of the file. |
67 | 48 | ''; |
68 | 49 | }; |
69 | 50 | group = mkOption { |
70 | | - type = singleLineStr; |
| 51 | + type = types.singleLineStr; |
71 | 52 | default = users.${config.owner}.group; |
72 | 53 | defaultText = lib.literalExpression ''config.users.users.''${cfg.owner}.group''; |
73 | 54 | description = '' |
|
88 | 69 | default = { }; |
89 | 70 | }; |
90 | 71 | placeholder = mkOption { |
91 | | - type = attrsOf (mkOptionType { |
| 72 | + type = types.attrsOf (types.mkOptionType { |
92 | 73 | name = "coercibleToString"; |
93 | 74 | description = "value that can be coerced to string"; |
94 | | - check = strings.isConvertibleWithToString; |
95 | | - merge = mergeEqualOption; |
| 75 | + check = lib.strings.isConvertibleWithToString; |
| 76 | + merge = lib.mergeEqualOption; |
96 | 77 | }); |
97 | 78 | default = { }; |
98 | 79 | visible = false; |
99 | 80 | }; |
100 | 81 | }; |
101 | 82 |
|
102 | | - config = optionalAttrs (options ? sops.secrets) |
103 | | - (mkIf (config.sops.templates != { }) { |
| 83 | + config = lib.optionalAttrs (options ? sops.secrets) |
| 84 | + (lib.mkIf (config.sops.templates != { }) { |
104 | 85 | sops.placeholder = mapAttrs |
105 | | - (name: _: mkDefault "<SOPS:${hashString "sha256" name}:PLACEHOLDER>") |
| 86 | + (name: _: mkDefault "<SOPS:${builtins.hashString "sha256" name}:PLACEHOLDER>") |
106 | 87 | config.sops.secrets; |
107 | | - |
108 | | - systemd.services.sops-render-secrets = let |
109 | | - installServices = [ "sops-install-secrets.service" ] ++ optional (secretsForUsers != { }) "sops-install-secrets-for-users.service"; |
110 | | - in lib.mkIf (cfg.templates != { } && useSystemdActivation) { |
111 | | - wantedBy = [ "sysinit.target" ]; |
112 | | - requires = installServices; |
113 | | - after = installServices; |
114 | | - unitConfig.DefaultDependencies = "no"; |
115 | | - |
116 | | - script = renderScript; |
117 | | - serviceConfig = { |
118 | | - Type = "oneshot"; |
119 | | - RemainAfterExit = true; |
120 | | - }; |
121 | | - }; |
122 | | - |
123 | | - system.activationScripts.renderSecrets = mkIf (cfg.templates != { } && !useSystemdActivation) |
124 | | - (stringAfter ([ "setupSecrets" ] ++ optional (secretsForUsers != { }) "setupSecretsForUsers") |
125 | | - renderScript); |
126 | 88 | }); |
127 | 89 | } |
0 commit comments