From 63aca41d0669ec88d59016fb5adb9631f7842c29 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Sat, 22 Jun 2024 18:53:09 +0200
Subject: [PATCH] Create partitions using individual commands

---
 lib/modules/partition.nix | 77 ++++++++++++++++++++-------------------
 1 file changed, 40 insertions(+), 37 deletions(-)

diff --git a/lib/modules/partition.nix b/lib/modules/partition.nix
index 1e8531ef..e565e09b 100644
--- a/lib/modules/partition.nix
+++ b/lib/modules/partition.nix
@@ -61,41 +61,48 @@
               (_: if ((!isSwap _) && (_.size == null)) then 1 else 0)
             ];
 
+          fdiskCommand = arguments: "sudo sfdisk ${arguments}";
+          fdiskScript = script: append: "echo ${script} | ${fdiskCommand "${if append then "--append" else ""} ${diskVar}"}";
+          wipeScript = script: fdiskScript script false;
+          appendScript = script: fdiskScript script true;
+
           cleanup = lib.strings.concatLines (builtins.map
-            (partition: ''sudo sfdisk --delete ${diskVar} ${toString partition.index}'')
+            (partition: fdiskCommand "--delete ${diskVar} ${toString partition.index}")
             (builtins.filter (_: !_.keepExisting) partitions));
-          fdiskCommands =
-            let
-              print = text: "echo ${text}";
-            in lib.strings.concatLines (
-              (lib.optional config.wipe (print "label: gpt")) ++
-              (builtins.concatMap (
-                partition:
-                  let
-                    inherit (partition)
-                      index
-                      keepExisting
-                      sizeScript
-                      type
-                    ;
 
-                    sizeOption = ''
-                      ${sizeScript} | sed -e "s/.*[^[:space:]]/size=\0/"
-                    '';
+          fdiskCommands = lib.strings.concatLines (
+            lib.optionals config.wipe [
+              cleanup
+              (wipeScript "label: gpt")
+            ] ++
+            (builtins.concatMap (
+              partition:
+                let
+                  inherit (partition)
+                    index
+                    keepExisting
+                    sizeScript
+                    type
+                  ;
 
-                    config = print "${toString index}: \"$(${sizeOption})\" type=${lib.strings.escapeShellArg type}";
-                    fallback = ''
-                      if ! { ls "${diskVar}"?(p)${toString partition.index} 2>&1; } > /dev/null
-                      then
-                        ${config}
-                      fi
-                    '';
-                  in [
-                    (if keepExisting then fallback else config)
-                  ])
-                partitions)
-            );
-          fixup = lib.strings.concatLines (
+                  sizeOption = ''
+                    ${sizeScript} | sed -e "s/.*[^[:space:]]/size=\0/"
+                  '';
+
+                  create = appendScript "${toString index}: \"$(${sizeOption})\" type=${lib.strings.escapeShellArg type}";
+                  fallback = ''
+                    if ! { ls "${diskVar}"?(p)${toString partition.index} 2>&1; } > /dev/null
+                    then
+                      ${create}
+                    fi
+                  '';
+                in [
+                  (if keepExisting then fallback else create)
+                ])
+              partitions)
+          );
+
+          fixType = lib.strings.concatLines (
             builtins.concatMap
               (partition: lib.optional
                 partition.keepExisting
@@ -113,12 +120,8 @@
 
               ${if (!config.wipe) then cleanup else ""}
 
-              function input() {
-                ${fdiskCommands}
-              }
-
-              input | tee /dev/stderr | sudo sfdisk ${if !config.wipe then "--append" else ""} "${diskVar}";
-              ${fixup}
+              ${fdiskCommands}
+              ${fixType}
             }
 
             partition