Skip to content

Commit 72d63d3

Browse files
committed
add age.secrets.*.{action,service}
represents an action to perform or systemd service to restart when the secret changes
1 parent 08b9c96 commit 72d63d3

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

modules/age.nix

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ let
9090
Group of the decrypted secret.
9191
'';
9292
};
93+
action = mkOption {
94+
type = types.str;
95+
default = "";
96+
description = "A script to run when secret is updated.";
97+
};
98+
service = mkOption {
99+
type = types.str;
100+
default = "";
101+
description = "The systemd service that uses this secret. Will be restarted when the secret changes.";
102+
example = "wireguard-wg0";
103+
};
93104
symlink = mkEnableOption "symlinking secrets to their destination" // { default = true; };
94105
};
95106
});
@@ -201,6 +212,41 @@ in
201212
"agenixChownKeys"
202213
];
203214
};
215+
216+
# services that watch for file changes and exectue the configured action
217+
systemd.services = lib.mkMerge
218+
(lib.mapAttrsToList
219+
(name: {action, service, file, path, mode, owner, group, ...}:
220+
let
221+
fileHash = builtins.hashString "sha256" (builtins.readFile file);
222+
restartTriggers = [ fileHash path mode owner group ];
223+
in
224+
lib.mkMerge [
225+
(lib.mkIf (service != "") {
226+
${service} = { inherit restartTriggers; };
227+
})
228+
(lib.mkIf (action != "") {
229+
"agenix-${name}-action" = {
230+
inherit restartTriggers;
231+
232+
# We execute the action on reload so that it doesn't happen at
233+
# startup. The only disadvantage is that it won't trigger the
234+
# first time the service is created.
235+
reload = action;
236+
reloadIfChanged = true;
237+
238+
serviceConfig = {
239+
Type = "oneshot";
240+
RemainAfterExit = true;
241+
};
242+
243+
script = " "; # it complains if we only set ExecReload
244+
245+
# Give it a reason for starting
246+
wantedBy = [ "multi-user.target" ];
247+
};
248+
249+
})]) cfg.secrets);
204250
};
205251

206252
}

0 commit comments

Comments
 (0)