From 62207a07e920e1d96c972ce2388d185c009cc589 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 22 Jun 2024 18:34:36 +0200 Subject: [PATCH] Refine the sorting of partitions --- lib/modules/partition.nix | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/modules/partition.nix b/lib/modules/partition.nix index 3859716e..1e8531ef 100644 --- a/lib/modules/partition.nix +++ b/lib/modules/partition.nix @@ -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