Run restic backups as non root users

This commit is contained in:
SebastianStork 2025-05-19 23:33:39 +02:00
parent 4b91351954
commit 87057d4b88
3 changed files with 65 additions and 4 deletions

View file

@ -6,6 +6,8 @@
}:
let
cfg = config.myConfig.syncthing;
user = config.users.users.syncthing.name;
in
{
options.myConfig.syncthing.backups.enable = lib.mkEnableOption "";
@ -18,7 +20,25 @@ in
}
];
security.polkit = {
enable = true;
extraConfig =
let
service = "syncthing.service";
in
''
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.systemd1.manage-units" &&
action.lookup("unit") == "${service}" &&
subject.user == "${user}") {
return polkit.Result.YES;
}
});
'';
};
myConfig.resticBackup.syncthing = {
inherit user;
healthchecks.enable = true;
extraConfig = {
@ -32,7 +52,7 @@ in
(pkgs.writeShellApplication {
name = "syncthing-restore";
text = ''
sudo bash -c "
sudo --user=${user} bash -c "
systemctl stop syncthing.service
restic-syncthing restore latest --target /
systemctl start syncthing.service