Refine the sorting of partitions

This commit is contained in:
Manuel Thalmann 2024-06-22 18:34:36 +02:00
parent dc4bcb4d22
commit 5b9a15b03e

View file

@ -3,6 +3,7 @@
inherit (lib) types mkOption;
diskVarName = "myDisk";
diskVar = ''''${${diskVarName}}'';
isSwap = partition: builtins.elem partition.type ["swap" 19];
diskSelector = ''
. ${./../../scripts/Common/Scripts/choose-disk.sh};
@ -51,7 +52,15 @@
config =
let
partitions = lib.lists.sortOn (_: _.order) (builtins.attrValues config.partitions);
partitions = builtins.foldl'
(list: predicate: lib.lists.sortOn predicate list)
(builtins.attrValues config.partitions)
[
(_: _.index)
(_: _.order)
(_: if ((!isSwap _) && (_.size == null)) then 1 else 0)
];
cleanup = lib.strings.concatLines (builtins.map
(partition: ''sudo sfdisk --delete ${diskVar} ${toString partition.index}'')
(builtins.filter (_: !_.keepExisting) partitions));
@ -118,10 +127,7 @@
});
partitionType = types.submodule (
{ name, config, ... }:
let
isSwap = builtins.elem config.type ["swap" 19];
in {
{ name, config, ... }: {
options = {
index = mkOption {
type = types.int;
@ -131,7 +137,7 @@
order = mkOption {
type = types.int;
description = "The sort order of the partition creation.";
default = if (!isSwap) && ((builtins.match ".*[^[:space:]].*" (toString config.size)) == null) then 1 else 0;
default = if (!(isSwap config)) && ((builtins.match ".*[^[:space:]].*" (toString config.size)) == null) then 1 else 0;
};
label = mkOption {
@ -167,7 +173,7 @@
config = {
sizeScript = (
if isSwap
if isSwap config
then
''echo "$(cat /proc/meminfo | awk -F " " '/^MemTotal/ { print $2 }' | awk '{ print int((($1 / 1024 / 1024) * 0.75) + 0.5)}')"G''
else