modularize cloudsync

This commit is contained in:
LordGrimmauld 2024-03-26 10:36:33 +01:00
parent def3826dd5
commit 1b5caddf01
6 changed files with 103 additions and 52 deletions

41
common/cloudsync.nix Normal file
View File

@ -0,0 +1,41 @@
{pkgs, config, lib, ... }: let
cfg = config.grimmShared;
in {
config = with cfg; lib.mkIf (enable && cloudSync.enable) (let
cloud_cmd = ''${pkgs.nextcloud-client}/bin/nextcloudcmd -u ${config.grimmShared.cloudSync.username} -p "$(cat ${config.grimmShared.cloudSync.passwordFile})" -h -n --path'';
sync_server = "https://${config.grimmShared.cloudSync.server}";
in {
environment.systemPackages = with pkgs; [
(writeShellScriptBin "cloudsync-cmd" (cloud_cmd + " $@ " + sync_server))
nextcloud-client
];
systemd = lib.mkMerge (lib.mapAttrsToList (local_user: paths: let
sync_script = lib.strings.concatLines (map ({local, remote}: let
remote_clean = lib.strings.concatStrings (builtins.match "/*(.+)" remote);
in "${cloud_cmd} /${remote_clean} ${local} ${sync_server}" ) paths);
in { # user-specific sync jobs
services."nextcloud-autosync-${local_user}" = {
description = "Auto sync Nextcloud";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig.Type = "simple";
serviceConfig.User = local_user;
serviceConfig.Group= "users";
script= sync_script;
# TimeoutStopSec = "180";
# KillMode = "process";
# KillSignal = "SIGINT";
wantedBy = ["multi-user.target"];
enable=true;
};
timers."nextcloud-autosync-${local_user}" = {
description = "Automatic sync files with Nextcloud when booted up after 5 minutes then rerun every 60 minutes";
timerConfig.OnBootSec = "5min";
timerConfig.OnUnitActiveSec = "60min";
wantedBy = ["multi-user.target" "timers.target"];
enable = true;
};
}) cfg.cloudSync.paths );
});
}

View File

@ -54,6 +54,26 @@
enable = true; enable = true;
populateDefaultConfig = true; populateDefaultConfig = true;
}; };
cloudSync = {
enable = true;
username = "Grimmauld";
server = "cloud.grimmauld.de";
passwordFile = config.age.secrets.nextcloud_pass.path;
paths = {
grimmauld = [
{ remote = "3d"; }
{ remote = "Pictures"; }
{ remote = "Documents"; }
{ remote = "Videos"; }
];
};
};
};
age.secrets.nextcloud_pass = {
file = ./secrets/nextcloud_pass.age;
mode = "777";
}; };
qt = { qt = {

View File

@ -46,7 +46,6 @@
./modules/xserver.nix ./modules/xserver.nix
./modules/system-packages.nix ./modules/system-packages.nix
./modules/kvm.nix ./modules/kvm.nix
./modules/cloudsync.nix
{ environment.systemPackages = [ agenix.packages.${system}.default ]; } { environment.systemPackages = [ agenix.packages.${system}.default ]; }
]; ];
}; };

View File

@ -119,6 +119,46 @@ in {
description = "puts my personal config in place using above methods"; description = "puts my personal config in place using above methods";
}; };
}; };
cloudSync = {
enable = mkEnableOption "cloud_sync";
username = mkOption {
type = types.nonEmptyStr;
description = "username to use for cloud login";
};
server = mkOption {
type = types.nonEmptyStr;
description = "server url to use for cloud sync";
};
passwordFile = mkOption {
type = types.nonEmptyStr;
description = "password file to use for login";
};
paths = mkOption {
type = let
mod = types.submodule ({config, ...} : {
options = {
remote = mkOption {
type = types.nonEmptyStr;
description = "path on the cloud server";
};
local = mkOption {
type = types.nonEmptyStr;
default = "$HOME/" + (lib.strings.concatStrings (builtins.match "/*(.+)" config.remote));
description = "local path to sync";
};
};
});
in types.attrsOf (types.listOf mod);
default = [];
description = "puts my personal config in place using above methods";
};
};
}; };
imports = [ imports = [
@ -134,5 +174,6 @@ in {
./common/pass.nix ./common/pass.nix
./common/sway.nix ./common/sway.nix
./common/sway-defaults.nix ./common/sway-defaults.nix
./common/cloudsync.nix
]; ];
} }

View File

@ -1,51 +0,0 @@
{pkgs, config, ... }: let
# cloud_cmd = "${pkgs.nextcloud-client}/bin/nextcloudcmd -h -n --path";
sync_user = "grimmauld";
sync_user_remote = "Grimmauld";
sync_server = "cloud.grimmauld.de";
sync_script = let
cloud_cmd = ''${pkgs.nextcloud-client}/bin/nextcloudcmd -u ${sync_user_remote} -p "$(cat ${config.age.secrets.nextcloud_pass.path})" -h -n --path'';
in ''
${cloud_cmd} /3d /home/${sync_user}/3d https://${sync_server}
${cloud_cmd} /Pictures /home/${sync_user}/Pictures https://${sync_server}
${cloud_cmd} /Documents /home/${sync_user}/Documents https://${sync_server}
${cloud_cmd} /Videos /home/${sync_user}/Videos https://${sync_server}
'';
in {
age.secrets.nextcloud_pass = {
file = ../secrets/nextcloud_pass.age;
owner = sync_user;
mode = "700";
};
environment.systemPackages = let
cloud_cmd = ''${pkgs.nextcloud-client}/bin/nextcloudcmd -u ${sync_user_remote} -p "$(cat ${config.age.secrets.nextcloud_pass.path})" -h -n --path'';
in with pkgs; [
nextcloud-client
(writeShellScriptBin "cloudsync" sync_script)
];
systemd = {
services.nextcloud-autosync = {
description = "Auto sync Nextcloud";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig.Type = "simple";
serviceConfig.User = sync_user;
serviceConfig.Group= "users";
script= sync_script;
# TimeoutStopSec = "180";
# KillMode = "process";
# KillSignal = "SIGINT";
wantedBy = ["multi-user.target"];
enable=true;
};
timers.nextcloud-autosync = {
description = "Automatic sync files with Nextcloud when booted up after 5 minutes then rerun every 60 minutes";
timerConfig.OnBootSec = "5min";
timerConfig.OnUnitActiveSec = "60min";
wantedBy = ["multi-user.target" "timers.target"];
enable = true;
};
};
}

1
result Symbolic link
View File

@ -0,0 +1 @@
/nix/store/nxcb0bbxk9y6bsqz9q77i7awfq4a8qfr-nixos-system-grimmauld-nixos-24.05.20240323.44d0940