diff options
| -rw-r--r-- | home/environ/vtmp.nix | 99 |
1 files changed, 87 insertions, 12 deletions
diff --git a/home/environ/vtmp.nix b/home/environ/vtmp.nix index bd02699..fcde421 100644 --- a/home/environ/vtmp.nix +++ b/home/environ/vtmp.nix @@ -1,23 +1,98 @@ { config, lib, + pkgs, ... }: with lib; let cfg = config.local.environ; -in { - config = mkIf cfg.enable { - systemd.user.tmpfiles.rules = [ - "d %t/vtmp 0700" - ]; - home.file = { - "vtmp".source = config.lib.file.mkOutOfStoreSymlink "/run/user/${toString config.local.uid}/vtmp"; - }; + vtmp-sync = pkgs.writeShellScript "vtmp-sync" '' + if [ $# -ne 2 ]; then + echo "usage: $0 <remote hostname> <local hostname>" >&2 + exit 1 + fi + + local="$2" + remote="$1" + + rsync="${lib.getExe pkgs.rsync}" + + declare -a rsync_opts + rsync_opts+=("-glprtxz") + rsync_opts+=("--open-noatime") + rsync_opts+=("--progress") + rsync_opts+=("--preallocate") + rsync_opts+=("--max-size=1G") + rsync_opts+=("--rsh=${lib.getExe pkgs.openssh} -o BatchMode=yes") + rsync_opts+=("--log-file=$remote/.rsync.log") + rsync_opts+=("--filter=- /$local/") + rsync_opts+=("--filter=- /$remote/") - gtk.gtk3.bookmarks = [ - "file://${config.home.homeDirectory}/vtmp" - "file://${config.home.homeDirectory}/tmp" - ]; + cd "$HOME/vtmp" + + # Push to $remote from $local + "$rsync" "''${rsync_opts[@]}" -- ./ "$remote:vtmp/$local/" + + # Pull from $remote to $local + "$rsync" "''${rsync_opts[@]}" -- "$remote:vtmp/" "./$remote/" + ''; +in { + options.local.environ = { + vtmpSyncHost = mkOption { + type = with lib.types; nullOr str; + default = null; + }; }; + + config = mkMerge [ + (mkIf cfg.enable { + systemd.user.tmpfiles.rules = [ + "d %t/vtmp 0700" + ]; + + home.file = { + "vtmp".source = config.lib.file.mkOutOfStoreSymlink "/run/user/${toString config.local.uid}/vtmp"; + }; + + gtk.gtk3.bookmarks = [ + "file://${config.home.homeDirectory}/vtmp" + "file://${config.home.homeDirectory}/tmp" + ]; + }) + (mkIf (cfg.enable && cfg.vtmpSyncHost != null) { + programs.ssh = { + extraOptionOverrides.PermitLocalCommand = "yes"; + + matchBlocks.${cfg.vtmpSyncHost}.extraOptions.LocalCommand = + "systemctl --user import-environment SSH_AUTH_SOCK; " + + "systemctl --user start vtmp-sync.timer"; + }; + + systemd.user = { + targets.vtmp-sync-failure = { + Unit = { + Conflicts = ["vtmp-sync.timer"]; + }; + }; + + services.vtmp-sync = { + Unit = { + OnFailure = ["vtmp-sync-failure.target"]; + }; + + Service = { + ExecStart = "${vtmp-sync} ${cfg.vtmpSyncHost} %l"; + }; + }; + + timers.vtmp-sync = { + Timer = { + OnActiveSec = "15s"; + OnUnitInactiveSec = "1h"; + }; + }; + }; + }) + ]; } |
