Skip to content

Commit bef289e

Browse files
authored
Merge pull request #928 from nicdumz/requires-mount
fix(systemd): require mounts for encryption keys.
2 parents d4971dd + fbedbb8 commit bef289e

2 files changed

Lines changed: 61 additions & 44 deletions

File tree

modules/sops/default.nix

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -430,56 +430,67 @@ in
430430
];
431431
config = lib.mkMerge [
432432
(lib.mkIf (cfg.secrets != { }) {
433-
assertions =
434-
[
435-
{
436-
assertion =
437-
cfg.gnupg.home != null
438-
|| cfg.gnupg.sshKeyPaths != [ ]
439-
|| cfg.age.keyFile != null
440-
|| cfg.age.sshKeyPaths != [ ];
441-
message = "No key source configured for sops. Either set services.openssh.enable or set sops.age.keyFile or sops.gnupg.home";
442-
}
443-
{
444-
assertion = !(cfg.gnupg.home != null && cfg.gnupg.sshKeyPaths != [ ]);
445-
message = "Exactly one of sops.gnupg.home and sops.gnupg.sshKeyPaths must be set";
446-
}
447-
]
448-
++ lib.optionals cfg.validateSopsFiles (
449-
lib.concatLists (
450-
lib.mapAttrsToList (name: secret: [
451-
{
452-
assertion = secret.uid != null && secret.uid != 0 -> secret.owner == null;
453-
message = "In ${secret.name} exactly one of sops.owner and sops.uid must be set";
454-
}
455-
{
456-
assertion = secret.gid != null && secret.gid != 0 -> secret.group == null;
457-
message = "In ${secret.name} exactly one of sops.group and sops.gid must be set";
458-
}
459-
]) cfg.secrets
460-
)
461-
);
433+
assertions = [
434+
{
435+
assertion =
436+
cfg.gnupg.home != null
437+
|| cfg.gnupg.sshKeyPaths != [ ]
438+
|| cfg.age.keyFile != null
439+
|| cfg.age.sshKeyPaths != [ ];
440+
message = "No key source configured for sops. Either set services.openssh.enable or set sops.age.keyFile or sops.gnupg.home";
441+
}
442+
{
443+
assertion = !(cfg.gnupg.home != null && cfg.gnupg.sshKeyPaths != [ ]);
444+
message = "Exactly one of sops.gnupg.home and sops.gnupg.sshKeyPaths must be set";
445+
}
446+
]
447+
++ lib.optionals cfg.validateSopsFiles (
448+
lib.concatLists (
449+
lib.mapAttrsToList (name: secret: [
450+
{
451+
assertion = secret.uid != null && secret.uid != 0 -> secret.owner == null;
452+
message = "In ${secret.name} exactly one of sops.owner and sops.uid must be set";
453+
}
454+
{
455+
assertion = secret.gid != null && secret.gid != 0 -> secret.group == null;
456+
message = "In ${secret.name} exactly one of sops.group and sops.gid must be set";
457+
}
458+
]) cfg.secrets
459+
)
460+
);
462461

463462
sops.environment.SOPS_GPG_EXEC = lib.mkIf (cfg.gnupg.home != null || cfg.gnupg.sshKeyPaths != [ ]) (
464463
lib.mkDefault "${cfg.gnupg.package}/bin/gpg"
465464
);
466465

467466
# When using sysusers we no longer are started as an activation script because those are started in initrd while sysusers is started later.
468-
systemd.services.sops-install-secrets = lib.mkIf (regularSecrets != { } && cfg.useSystemdActivation) {
469-
wantedBy = [ "sysinit.target" ];
470-
after = [ "local-fs.target" "systemd-sysusers.service" "userborn.service" ];
471-
requiredBy = [ "sysinit-reactivation.target" ];
472-
before = [ "sysinit-reactivation.target" ];
473-
environment = cfg.environment;
474-
unitConfig.DefaultDependencies = "no";
475-
path = cfg.age.plugins;
476-
477-
serviceConfig = {
478-
Type = "oneshot";
479-
ExecStart = [ "${cfg.package}/bin/sops-install-secrets ${manifest}" ];
480-
RemainAfterExit = true;
481-
};
482-
};
467+
systemd.services.sops-install-secrets =
468+
lib.mkIf (regularSecrets != { } && cfg.useSystemdActivation)
469+
{
470+
wantedBy = [ "sysinit.target" ];
471+
after = [
472+
"local-fs.target"
473+
"systemd-sysusers.service"
474+
"userborn.service"
475+
];
476+
requiredBy = [ "sysinit-reactivation.target" ];
477+
before = [ "sysinit-reactivation.target" ];
478+
environment = cfg.environment;
479+
unitConfig.DefaultDependencies = "no";
480+
path = cfg.age.plugins;
481+
482+
serviceConfig = {
483+
Type = "oneshot";
484+
ExecStart = [ "${cfg.package}/bin/sops-install-secrets ${manifest}" ];
485+
RemainAfterExit = true;
486+
};
487+
unitConfig.RequiresMountsFor = lib.concatLists [
488+
(lib.lists.optional (cfg.gnupg.home != null) cfg.gnupg.home)
489+
cfg.gnupg.sshKeyPaths
490+
(lib.lists.optional (cfg.age.keyFile != null) cfg.age.keyFile)
491+
cfg.age.sshKeyPaths
492+
];
493+
};
483494

484495
system.activationScripts = {
485496
setupSecrets = lib.mkIf (regularSecrets != { } && !cfg.useSystemdActivation) (

modules/sops/secrets-for-users/default.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ in
4444
ExecStart = [ "${cfg.package}/bin/sops-install-secrets -ignore-passwd ${manifestForUsers}" ];
4545
RemainAfterExit = true;
4646
};
47+
unitConfig.RequiresMountsFor = lib.concatLists [
48+
(lib.lists.optional (cfg.gnupg.home != null) cfg.gnupg.home)
49+
cfg.gnupg.sshKeyPaths
50+
(lib.lists.optional (cfg.age.keyFile != null) cfg.age.keyFile)
51+
cfg.age.sshKeyPaths
52+
];
4753
};
4854

4955
system.activationScripts = lib.mkIf (secretsForUsers != { } && !useSystemdActivation) {

0 commit comments

Comments
 (0)