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

@ -4,12 +4,32 @@
lib, lib,
... ...
}: }:
let
user = config.users.users.actual.name;
in
{ {
options.myConfig.actualbudget.backups.enable = lib.mkEnableOption ""; options.myConfig.actualbudget.backups.enable = lib.mkEnableOption "";
config = lib.mkIf config.myConfig.actualbudget.backups.enable { config = lib.mkIf config.myConfig.actualbudget.backups.enable {
myConfig.resticBackup.actual = { security.polkit = {
enable = true; enable = true;
extraConfig =
let
service = "actual.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.actual = {
inherit user;
healthchecks.enable = true; healthchecks.enable = true;
extraConfig = { extraConfig = {
@ -23,7 +43,7 @@
(pkgs.writeShellApplication { (pkgs.writeShellApplication {
name = "actual-restore"; name = "actual-restore";
text = '' text = ''
sudo bash -c " sudo --user=${user} bash -c "
systemctl stop actual.service systemctl stop actual.service
restic-actual restore latest --target / restic-actual restore latest --target /
systemctl start actual.service systemctl start actual.service

View file

@ -4,11 +4,32 @@
lib, lib,
... ...
}: }:
let
user = config.users.users.hedgedoc.name;
in
{ {
options.myConfig.hedgedoc.backups.enable = lib.mkEnableOption ""; options.myConfig.hedgedoc.backups.enable = lib.mkEnableOption "";
config = lib.mkIf config.myConfig.hedgedoc.backups.enable { config = lib.mkIf config.myConfig.hedgedoc.backups.enable {
security.polkit = {
enable = true;
extraConfig =
let
service = "hedgedoc.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.hedgedoc = { myConfig.resticBackup.hedgedoc = {
inherit user;
healthchecks.enable = true; healthchecks.enable = true;
extraConfig = { extraConfig = {
@ -25,7 +46,7 @@
(pkgs.writeShellApplication { (pkgs.writeShellApplication {
name = "hedgedoc-restore"; name = "hedgedoc-restore";
text = '' text = ''
sudo bash -c " sudo --user=${user} bash -c "
systemctl stop hedgedoc.service systemctl stop hedgedoc.service
restic-hedgedoc restore latest --target / restic-hedgedoc restore latest --target /
systemctl start hedgedoc.service systemctl start hedgedoc.service

View file

@ -6,6 +6,8 @@
}: }:
let let
cfg = config.myConfig.syncthing; cfg = config.myConfig.syncthing;
user = config.users.users.syncthing.name;
in in
{ {
options.myConfig.syncthing.backups.enable = lib.mkEnableOption ""; 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 = { myConfig.resticBackup.syncthing = {
inherit user;
healthchecks.enable = true; healthchecks.enable = true;
extraConfig = { extraConfig = {
@ -32,7 +52,7 @@ in
(pkgs.writeShellApplication { (pkgs.writeShellApplication {
name = "syncthing-restore"; name = "syncthing-restore";
text = '' text = ''
sudo bash -c " sudo --user=${user} bash -c "
systemctl stop syncthing.service systemctl stop syncthing.service
restic-syncthing restore latest --target / restic-syncthing restore latest --target /
systemctl start syncthing.service systemctl start syncthing.service