Skip to content

Commit aa5caa1

Browse files
jflymergify[bot]
authored andcommitted
rebase, complete implementation
1 parent bb7d636 commit aa5caa1

File tree

5 files changed

+147
-206
lines changed

5 files changed

+147
-206
lines changed

modules/sops/manifest-for.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ writeTextFile {
1616
ageSshKeyPaths = cfg.age.sshKeyPaths;
1717
useTmpfs = cfg.useTmpfs;
1818
templates = cfg.templates;
19+
placeholderBySecretName = cfg.placeholder;
1920
userMode = false;
2021
logging = {
2122
keyImport = builtins.elem "keyImport" cfg.log;

modules/sops/templates/default.nix

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,54 @@
11
{ config, pkgs, lib, options, ... }:
2-
with lib;
3-
with lib.types;
4-
with builtins;
52
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+
810
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-
'';
3011
in {
3112
options.sops = {
3213
templates = mkOption {
3314
description = "Templates for secret files";
34-
type = attrsOf (submodule ({ config, ... }: {
15+
type = types.attrsOf (types.submodule ({ config, ... }: {
3516
options = {
3617
name = mkOption {
37-
type = singleLineStr;
18+
type = types.singleLineStr;
3819
default = config._module.args.name;
3920
description = ''
4021
Name of the file used in /run/secrets/rendered
4122
'';
4223
};
4324
path = mkOption {
4425
description = "Path where the rendered file will be placed";
45-
type = singleLineStr;
26+
type = types.singleLineStr;
4627
default = "/run/secrets/rendered/${config.name}";
4728
};
4829
content = mkOption {
49-
type = lines;
30+
type = types.lines;
5031
default = "";
5132
description = ''
5233
Content of the file
5334
'';
5435
};
5536
mode = mkOption {
56-
type = singleLineStr;
37+
type = types.singleLineStr;
5738
default = "0400";
5839
description = ''
5940
Permissions mode of the rendered secret file in octal.
6041
'';
6142
};
6243
owner = mkOption {
63-
type = singleLineStr;
44+
type = types.singleLineStr;
6445
default = "root";
6546
description = ''
6647
User of the file.
6748
'';
6849
};
6950
group = mkOption {
70-
type = singleLineStr;
51+
type = types.singleLineStr;
7152
default = users.${config.owner}.group;
7253
defaultText = lib.literalExpression ''config.users.users.''${cfg.owner}.group'';
7354
description = ''
@@ -88,40 +69,21 @@ in {
8869
default = { };
8970
};
9071
placeholder = mkOption {
91-
type = attrsOf (mkOptionType {
72+
type = types.attrsOf (types.mkOptionType {
9273
name = "coercibleToString";
9374
description = "value that can be coerced to string";
94-
check = strings.isConvertibleWithToString;
95-
merge = mergeEqualOption;
75+
check = lib.strings.isConvertibleWithToString;
76+
merge = lib.mergeEqualOption;
9677
});
9778
default = { };
9879
visible = false;
9980
};
10081
};
10182

102-
config = optionalAttrs (options ? sops.secrets)
103-
(mkIf (config.sops.templates != { }) {
83+
config = lib.optionalAttrs (options ? sops.secrets)
84+
(lib.mkIf (config.sops.templates != { }) {
10485
sops.placeholder = mapAttrs
105-
(name: _: mkDefault "<SOPS:${hashString "sha256" name}:PLACEHOLDER>")
86+
(name: _: mkDefault "<SOPS:${builtins.hashString "sha256" name}:PLACEHOLDER>")
10687
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);
12688
});
12789
}

modules/sops/templates/subs.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)