config/modules/services/wifibackup/default.nix

64 lines
1.8 KiB
Nix
Raw Normal View History

2024-06-06 01:57:00 +03:00
{
lib,
config,
pkgs,
...
2024-07-29 15:39:54 +03:00
}:
{
2024-06-06 01:57:00 +03:00
options.mj.services.wifibackup = with lib.types; {
enable = lib.mkEnableOption "enable wifi code backups to M-Active";
fromPath = lib.mkOption {
type = path;
default = "/etc/NetworkManager/system-connections";
};
toPath = lib.mkOption {
type = path;
example = "/home/motiejus/M-Active/wifi";
};
toUser = lib.mkOption {
type = str;
example = "motiejus";
};
};
2024-07-29 15:39:54 +03:00
config =
with config.mj.services.wifibackup;
2024-06-06 01:57:00 +03:00
lib.mkIf enable {
systemd.timers.wifibackup = {
description = "wifibackup to M-Active";
2024-07-29 15:39:54 +03:00
wantedBy = [ "timers.target" ];
2024-06-06 01:57:00 +03:00
timerConfig.OnCalendar = "*-*-* 22:00:00 UTC";
};
systemd.services.wifibackup = {
description = "backup ${fromPath} to ${toPath}";
serviceConfig = {
Type = "oneshot";
User = "root";
2024-07-29 15:39:54 +03:00
SuccessExitStatus = [
0
1
];
2024-06-06 01:57:00 +03:00
};
2024-07-29 15:39:54 +03:00
script =
let
knownHostsCmd = pkgs.writeShellScript "known-hosts-localhost" ''
echo -n "localhost "
exec ${pkgs.coreutils}/bin/cat /etc/ssh/ssh_host_ed25519_key.pub
'';
in
''
sed -i -E '/^(uuid|interface-name)=/d' ${fromPath}/*.nmconnection
2024-06-06 02:13:12 +03:00
2024-07-29 15:39:54 +03:00
exec ${pkgs.unison}/bin/unison \
-sshcmd ${pkgs.openssh}/bin/ssh \
-sshargs "-i /etc/ssh/ssh_host_ed25519_key -o KnownHostsCommand=${knownHostsCmd} -o UserKnownHostsFile=none -o GlobalKnownHostsFile=/dev/null" \
-batch \
-backuploc local \
-backup "Name *" \
${fromPath} \
ssh://${toUser}@localhost/${toPath}/
'';
2024-06-06 01:57:00 +03:00
};
};
}