From 2d522f51f5b61041a2468ff3bb4eb38704081dbc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicolas=20=E2=80=9CNiols=E2=80=9D=20Jeannerod?=
 <nicolas.jeannerod@moduscreate.com>
Date: Fri, 8 Nov 2024 17:03:07 +0100
Subject: [PATCH] Support installing host keys in the installer

---
 installer.nix | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/installer.nix b/installer.nix
index 96e54d0..dcf6f20 100644
--- a/installer.nix
+++ b/installer.nix
@@ -4,10 +4,14 @@
   WARNING: Running this installer will format the target disk!
 */
 
-{ nixpkgs, ... }:
+{ nixpkgs,
+  hostKeys ? {}
+}:
 machine:
 
 let
+  inherit (builtins) concatStringsSep attrValues mapAttrs;
+
   installer = { config, pkgs, lib, ... }:
     let
       bootstrap = pkgs.writeShellApplication {
@@ -15,8 +19,22 @@ let
         runtimeInputs = with pkgs; [ nixos-install-tools ];
         text = ''
           ${machine.config.system.build.diskoScript}
-          nixos-install --no-root-password --no-channel-copy --system ${machine.config.system.build.toplevel} \
-            && poweroff
+          nixos-install --no-root-password --no-channel-copy --system ${machine.config.system.build.toplevel}
+          ${
+            concatStringsSep "\n" (
+              attrValues (
+              mapAttrs
+                (kind: keys: ''
+                   cp ${keys.private} /mnt/etc/ssh/ssh_host_${kind}_key
+                   chmod 600 /mnt/etc/ssh/ssh_host_${kind}_key
+                   cp ${keys.public} /mnt/etc/ssh/ssh_host_${kind}_key.pub
+                   chmod 644 /mnt/etc/ssh/ssh_host_${kind}_key.pub
+                 '')
+                hostKeys
+              )
+            )
+          }
+          poweroff
         '';
       };
     in