Split up disk script into multiple scripts

This commit is contained in:
Manuel Thalmann 2024-12-07 16:12:04 +01:00
parent 7b3b7fcdbc
commit ed4fa1a33d

View file

@ -298,9 +298,50 @@ in
default = { }; default = { };
}; };
scripts = {
init = mkOption {
type = types.str;
description = "The script for initializing the disk partitioning script.";
};
partition = mkOption {
type = types.str;
description = "The script for partitioning the disks.";
};
mount = mkOption {
type = types.str;
description = "The script for mounting the partitioned disks.";
};
};
script = mkOption { script = mkOption {
type = types.str; type = types.str;
description = "The script for partitioning the system's disks."; description = "The script for partitioning the system's disks.";
default =
let
cfg = config.valhalla.fileSystems.diskSetup;
inherit (cfg) scripts;
disks = (builtins.attrValues cfg.disks);
in
''
#!/bin/bash
set -o errexit
${scripts.init}
${lib.strings.concatLines (lib.optionals ((builtins.length disks) > 0) [
''echo "$(tput setaf 3)==== WARNING ====$(tput sgr0)"''
(''echo "Continuing this script will alter the partitions of ''
+ (lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init disks)))
+ (if (builtins.length disks) > 1 then " and " else "") + (lib.lists.last disks).deviceVariable + ''"'')
''
if ! fish ${./confirm.fish} "Are you sure you want to continue?" "n"; then
exit 1
fi
''
])}
${scripts.partition}
${scripts.mount}
'';
}; };
}; };
}; };
@ -310,7 +351,7 @@ in
config = { config = {
valhalla = { valhalla = {
fileSystems.diskSetup = { fileSystems.diskSetup = {
script = lib.mkDefault ( scripts =
let let
cfg = config.valhalla.fileSystems; cfg = config.valhalla.fileSystems;
inherit (cfg) rootDir; inherit (cfg) rootDir;
@ -318,58 +359,38 @@ in
partPath = part: "/dev/disk/by-label/${part.label}"; partPath = part: "/dev/disk/by-label/${part.label}";
disks = ((builtins.attrValues cfg.diskSetup.disks)); disks = ((builtins.attrValues cfg.diskSetup.disks));
partitions = (builtins.concatMap (_: (builtins.attrValues _.partitions)) disks); partitions = (builtins.concatMap (_: (builtins.attrValues _.partitions)) disks);
in
mountScript = lib.strings.concatLines (builtins.concatMap {
( init = lib.strings.concatLines (builtins.map (_: _.deviceScript) disks);
_: [ partition = lib.strings.concatLines (builtins.map (_: _.script) disks);
probeScript mount = lib.strings.concatLines (
(builtins.concatStringsSep " " ([ (builtins.concatMap
"sudo" (
"mount" _: [
"--mkdir" probeScript
] ++ (lib.optionals (_.format == "ntfs") [ (builtins.concatStringsSep " " (
"-t" [ "sudo" "mount" "--mkdir" ] ++
"ntfs3" (lib.optionals (_.format == "ntfs") [ "-t" "ntfs3" ]) ++
]) ++ [ [
(builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions)) (builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions))
(partPath _) (partPath _)
(normalizePath "/${rootDir}/${_.mountPoint}") (normalizePath "/${rootDir}/${_.mountPoint}")
])) ]
] ))
) ]
(lib.lists.sortOn )
(_: normalizePath "/${_.mountPoint}") (lib.lists.sortOn
(builtins.filter (_: _.mountPoint != null) partitions))); (_: normalizePath "/${_.mountPoint}")
(builtins.filter (_: _.mountPoint != null) partitions))) ++
swapScript = lib.strings.concatLines (builtins.map (builtins.map
( (
_: '' _: ''
${probeScript} ${probeScript}
sudo swapon ${partPath _} sudo swapon ${partPath _}
'' ''
) )
(builtins.filter (_: _.useSwap) partitions)); (builtins.filter (_: _.useSwap) partitions)));
in };
lib.strings.concatLines ([
"#!/bin/bash"
"set -o errexit"
]
++ (builtins.map (_: _.deviceScript) disks)
++ lib.optionals ((builtins.length disks) > 0) [
''echo "$(tput setaf 3)==== WARNING ====$(tput sgr0)"''
(''echo "Continuing this script will alter the partitions of ''
+ (lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init disks)))
+ (if (builtins.length disks) > 1 then " and " else "") + (lib.lists.last disks).deviceVariable + ''"'')
''
if ! fish ${./confirm.fish} "Are you sure you want to continue?" "n"; then
exit 1
fi
''
] ++ (builtins.map (_: _.script) disks) ++ [
mountScript
swapScript
])
);
}; };
}; };
}; };