Create partitions using individual commands

This commit is contained in:
Manuel Thalmann 2024-06-22 18:53:09 +02:00
parent 5b9a15b03e
commit 63aca41d06

View file

@ -61,41 +61,48 @@
(_: if ((!isSwap _) && (_.size == null)) then 1 else 0) (_: 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 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)); (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 = '' fdiskCommands = lib.strings.concatLines (
${sizeScript} | sed -e "s/.*[^[:space:]]/size=\0/" 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}"; sizeOption = ''
fallback = '' ${sizeScript} | sed -e "s/.*[^[:space:]]/size=\0/"
if ! { ls "${diskVar}"?(p)${toString partition.index} 2>&1; } > /dev/null '';
then
${config} create = appendScript "${toString index}: \"$(${sizeOption})\" type=${lib.strings.escapeShellArg type}";
fi fallback = ''
''; if ! { ls "${diskVar}"?(p)${toString partition.index} 2>&1; } > /dev/null
in [ then
(if keepExisting then fallback else config) ${create}
]) fi
partitions) '';
); in [
fixup = lib.strings.concatLines ( (if keepExisting then fallback else create)
])
partitions)
);
fixType = lib.strings.concatLines (
builtins.concatMap builtins.concatMap
(partition: lib.optional (partition: lib.optional
partition.keepExisting partition.keepExisting
@ -113,12 +120,8 @@
${if (!config.wipe) then cleanup else ""} ${if (!config.wipe) then cleanup else ""}
function input() { ${fdiskCommands}
${fdiskCommands} ${fixType}
}
input | tee /dev/stderr | sudo sfdisk ${if !config.wipe then "--append" else ""} "${diskVar}";
${fixup}
} }
partition partition