From 402c9cac2595f2219efb8bb51becd337bbcb7965 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= <joerg@thalheim.io>
Date: Sun, 29 Dec 2019 21:43:58 +0000
Subject: writeDeploy: add support for build hosts

This allows to evaluate & build the system on the dedicated build host,
from which the build artifacts are uploaded onto the target machine.
---
 README.md              | 32 ++++++++++++++++++++++++++++----
 pkgs/krops/default.nix | 18 +++++++++++++++---
 2 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index 323d08b..88bc7e4 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # krops (krebs ops)
 
-krops is a lightweigt toolkit to deploy NixOS systems, remotely or locally.
+krops is a lightweight toolkit to deploy NixOS systems, remotely or locally.
 
 
 ## Some Features
@@ -56,7 +56,9 @@ and run `$(nix-build --no-out-link krops.nix)` to deploy the target machine.
 Under the hood, this will make the sources available on the target machine
 below `/var/src`, and execute `nixos-rebuild switch -I /var/src`.
 
-## Deployment Target Attribute
+## Deployment Attributes
+
+### `target`
 
 The `target` attribute to `writeDeploy` can either be a string or an attribute
 set, specifying where to make the sources available, as well as where to run
@@ -84,9 +86,31 @@ pkgs.krops.writeDeploy "deploy" {
   };
 }
 ```
-
 For more details about the `target` attribute, please check the `mkTarget`
-function in lib/default.nix.
+function in [lib/default.nix](lib/defaults.nix).
+
+### `backup` (optional, defaults to false)
+
+Backup all paths specified in source before syncing new sources.
+
+### `buildTarget` (optional)
+
+If set the evaluation and build of the system will be executed on this host.
+`buildTarget` takes the same arguments as target.
+Sources will be synced to both `buildTarget` and `target`.
+Built packages will be uploaded from the `buildTarget` to `target` directly
+This requires the building machine to have ssh access to the target.
+To build the system on the same machine, that runs the krops command,
+set up a local ssh service and set the build host to localhost.
+
+### `fast` (optional, defaults to false)
+
+Run `nixos-rebuild switch` immediately without building the system
+in a dedicated `nix build` step.
+
+### `force` (optional, defaults to false)
+
+Create the sentinel file (`/var/src/.populate`) before syncing the new source.
 
 ## Source Types
 
diff --git a/pkgs/krops/default.nix b/pkgs/krops/default.nix
index a3e7745..700066e 100644
--- a/pkgs/krops/default.nix
+++ b/pkgs/krops/default.nix
@@ -47,21 +47,33 @@ in
 
   writeDeploy = name: {
     backup ? false,
+    buildTarget ? null,
     fast ? false,
     force ? false,
     source,
     target
   }: let
+    buildTarget' =
+      if buildTarget == null
+        then target'
+        else lib.mkTarget buildTarget;
     target' = lib.mkTarget target;
   in
     writeDash name ''
       set -efu
+      ${lib.optionalString (buildTarget' != target')
+        (populate { inherit backup force source; target = buildTarget'; })}
       ${populate { inherit backup force source; target = target'; }}
       ${lib.optionalString (! fast) ''
-        ${rebuild ["dry-build"] target'}
-        ${build target'}
+        ${rebuild ["dry-build"] buildTarget'}
+        ${build buildTarget'}
       ''}
-      ${rebuild ["switch"] target'}
+      ${rebuild ([
+        "switch"
+      ] ++ lib.optionals (buildTarget' != target') [
+        "--build-host" "${buildTarget'.user}@${buildTarget'.host}"
+        "--target-host" "${target'.user}@${target'.host}"
+      ]) buildTarget'}
     '';
 
   writeTest = name: {
-- 
cgit v1.2.3