Hashsync: Copy Nix closures efficiently via ssh

Hello Nix community,

I’d like to announce a project that I just published, hashsync: GitHub - someplaceguy/hashsync: Run a command over SSH, deduplicating its output or input against files stored on the receiving host, to save bandwidth. · GitHub

It can be used to copy Nix closures between your own machines in a far more efficient way than nix-copy-closure (or nix copy), because it doesn’t copy data that already exists in the destination Nix store, even if it’s in a different store path, a differently-named file or even if the data to be copied is at a different offset in a file you already have.

If you use your own Hydra, this can make package updates much faster and bandwidth-efficient, since most package updates usually have almost the same contents as previous versions (so why bother copying the entire package if you already have most of the data, right?).

hashsync works as a transparent pipe over ssh, so it can also be used with tar, rsync, etc. It has no special knowledge of Nix closures, NARs, etc – it works with any data stream, as it can find blocks to deduplicate at any offset (like rsync’s delta algorithm but across your entire Nix store rather than just one file).

But it does have special support for indexing Nix stores: they can be marked as “immutable roots”, so hashsync can assume that a path inside the Nix store always has the same contents, which makes updating the index far more efficient (since it doesn’t have to traverse paths that have already been traversed).

I’ve been using it every day for weeks and it works great for me. Let me know if you have any questions.

Thanks!

Edit: This project was written with assistance from Claude.

5 Likes

nice! any plans to add this to nixpkgs?

It appears that this is mostly generated by an LLM, which according to the policy must be disclosed in your post, so please update it accordingly

3 Likes

Thanks! I’ll submit a PR today.

Updated.

Thank you.

2 Likes

Just for fun, here’s how well hashsync worked for me today:

For context, I build all packages on my own Hydra server and then deploy a new NixOS configuration by copying the system closure from the Hydra server to the machine being updated, and then I reboot or switch to the new configuration.

However, this week, the release-26.05 branch added some patches to glibc, which caused a mass rebuild of all packages, basically causing all packages in the system closure to get new paths.

A normal nix-copy-closure (or nix copy) of the new system closure to my development VM would have transferred tens of GB (total size: 37.3 GiB, minus compression).

With hashsync and the hashync-copy-closure.sh script in the repo, only ~2.8 GiB were transferred instead:

(…)
hashsync: stdin -- 182.73 KiB --> 82.72 KiB wire (compressed)
hashsync: stdout -- 37.30 GiB (8.00 GiB literal, 28.64 GiB -> 8.95 MiB refs, 673.42 MiB -> 31.54 KiB RLE, 0 false positives, 0 not found) + 64.88 KiB hashlist delta --> 2.75 GiB wire (deduplicated, compressed)
hashsync: total -- sent 23.80 MiB and received 2.83 GiB in 555.0s (5.27 MiB/s), data shrinkage factor: 13.05, saved 34.44 GiB of data transfer

To be clear, it doesn’t always work this well because it depends on what data you’re transferring, but when copying Nix closures, a mass rebuild of a NixOS config is the best-case scenario, i.e. where hashsync performs best.

And even when hashsync can’t deduplicate data, it shouldn’t make things measurably worse than they would have been anyway (unless your network is extremely fast, perhaps).

3 Likes

Nice! Yes, it feels like there’s a lot of opportunity to reduce data usage between similar store paths.

How does hashsync compare to casync which also uses rolling hashes? I see there is some integration between nix and casync in Nix-casync, a more efficient way to store and substitute Nix store paths, though I’ve never tried it.

2 Likes

Good question!

I’m not an expert on casync or Nix-casync, but from what I understand, here’s my biased comparison:

  1. casync requires storing the data in its own data store, so data stored there cannot usually be read by normal system utilities and applications. hashsync doesn’t have a data store, it just reads the files that you already have on your filesystem. hashsync mostly operates at the network level.
  2. casync does have a read-only FUSE implementation, which allows you to mount the data store as a read-only filesystem, but 1) you can’t write to the data store via the FUSE filesystem, and 2) FUSE filesystems can be slower than kernel filesystems in many cases.
    hashsync doesn’t have a data store, so you don’t need FUSE, you can just read or write to your files as you’ve always done, and hashsync just reads them / indexes them when needed.
  3. Nix-casync is a binary cache server which stores uploaded NARs as casync chunks. It then reassembles and sends whole NARs to clients. So Nix-casync saves storage on the server, but it doesn’t save bandwidth. hashsync is the opposite: it saves bandwidth, but it doesn’t save storage. (But you can use zfs dedup together with hashsync if you want to dedup storage).

casync is a lot of stuff: a data store, a tar replacement, a network/HTTP server, a FUSE filesystem, a backup system, a network block device server, …

hashsync is just a single, much simpler tool, that just does one thing well: a transparent pipe that deduplicates network streams… nothing else. As a result, it can be trivially combined with other tools: tar, rsync, nix-store --export, etc. It has the same interface that ssh has (hence why rsync -e 'hashsync ssh' works).

There are also a few technical choices that are different:

  1. casync uses buzhash as its rolling hash, which is faster than hashsync’s poly64+mix64, but it collides far more on structured data (~1 million false hits per GB of binaries with a 4K block size, compared to ~0 false hits when using hashsync’s hash).
    For the hashsync use case this would cause way more queries/answers over the network, hence way more round-trips and latency, hence way less efficient network traffic.
    hashsync compensates the slower rolling hash algorithm by using (type-safe and memory-safe) SIMD acceleration when possible and by distributing work over all the CPU cores to handle the remaining cases. If your computer is modern enough, the rolling hash should not be the bottleneck: the network normally is, especially considering hashsync was designed for slow networks.
    That said, it would be trivial to add a new rolling hash to hashsync if necessary, as it supports pluggable hash algorithms both at the protocol and index/database level.
  2. casync uses sha512/256 or sha256 as its strong hash. hashsync doesn’t support sha512/256 (at least, not yet), but it supports hardware-accelerated sha256.
    However, hashsync’s default cryptographically-strong hash is blake3, which e.g. on my laptop is 3.5x faster (3.1 GB/s) than hardware-accelerated sha256 (900 MB/s). You can measure both algorithms on your own machine with hashsync bench.
    This is especially important for indexing: hashsync can index new files at a few GB/s when using modern hardware.
  3. casync uses variably-sized chunks (64K on average), while hashsync uses a fixed block size (16K by default, but you can change this in the default.toml config file). Larger block sizes use less network traffic and smaller indexes, but deduplicate worse.
    I chose 16K because it hits the sweet spot for me: it deduplicates better than larger sizes but everything is still very fast and efficient for the amount of data I typically work with (500 GB - 1 TB Nix stores and similarly-sized rsync backups).
3 Likes

PR submitted to nixpkgs. It also installs the hashsync-copy-closure script, to make it easier to use :tada:.

4 Likes