diff --git a/common/cloudsync.nix b/common/cloudsync.nix new file mode 100644 index 0000000..e95e932 --- /dev/null +++ b/common/cloudsync.nix @@ -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 ); + }); +} diff --git a/configuration.nix b/configuration.nix index d7aeaed..da8e8b6 100644 --- a/configuration.nix +++ b/configuration.nix @@ -54,6 +54,26 @@ enable = 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 = { diff --git a/flake.nix b/flake.nix index 578de27..79d746f 100644 --- a/flake.nix +++ b/flake.nix @@ -46,7 +46,6 @@ ./modules/xserver.nix ./modules/system-packages.nix ./modules/kvm.nix - ./modules/cloudsync.nix { environment.systemPackages = [ agenix.packages.${system}.default ]; } ]; }; diff --git a/load_common.nix b/load_common.nix index 0e61963..2d0e221 100644 --- a/load_common.nix +++ b/load_common.nix @@ -119,6 +119,46 @@ in { 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 = [ @@ -134,5 +174,6 @@ in { ./common/pass.nix ./common/sway.nix ./common/sway-defaults.nix + ./common/cloudsync.nix ]; } diff --git a/modules/cloudsync.nix b/modules/cloudsync.nix deleted file mode 100644 index 24c2d49..0000000 --- a/modules/cloudsync.nix +++ /dev/null @@ -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; - }; - }; -} diff --git a/result b/result new file mode 120000 index 0000000..a647276 --- /dev/null +++ b/result @@ -0,0 +1 @@ +/nix/store/nxcb0bbxk9y6bsqz9q77i7awfq4a8qfr-nixos-system-grimmauld-nixos-24.05.20240323.44d0940 \ No newline at end of file