nix-community / terraform-nixos

A set of Terraform modules that are designed to deploy NixOS [maintainer=@adrian-gierakowski]
Apache License 2.0
333 stars 61 forks source link

Speed up the process of copying drv files to the remote host #62

Open exarkun opened 2 years ago

exarkun commented 2 years ago

Is your feature request related to a problem? Please describe.

When deploying changes with deploy_nixos a large portion of the deploy time is spent copying .drv files to the remote host. These files are very small, there are often very many of them, and round-trip latency ends up accounting for most of the time spent.

Describe the solution you'd like

According to #nixos on Matrix, the nix-copy-closure protocol is "very chatty" (ie, has many round-trips) because it tries not to send any objects that the remote already has and it tries not to send anything that the remote doesn't already have all dependencies for.

When buildOnTarget is set, deploy_nixos runs nix-copy-closure in nixos-deploy.sh specifically to copy derivations only. Instead of using nix-copy-closure in this case, it could perform an export, transfer the export, then perform an import. This would be a much less "chatty" exchange and largely eliminate the effects of round-trip latency. Since drvs are small, the penalty of possibly transferring some which do not need to be transferred is minimal. An entire NixOS system's drvs might amount to 30MB or 40MB.

This option could be guarded by a configuration toggle if there's some desire to put the choice about this trade-off into the end-user's hands.

Describe alternatives you've considered

None

Additional context

None

exarkun commented 2 years ago

The export / import might somewhat resemble this pipeline:

nix-store --export $(nix-store --query --requisites $drvPath) | ssh remote nix-store --import