Store device scripts in scripts option

This commit is contained in:
Manuel Thalmann 2024-12-07 17:49:57 +01:00
parent 68e1990c87
commit 7b5564a632

View file

@ -61,9 +61,16 @@ let
default = { };
};
script = mkOption {
type = types.str;
description = "The script for formatting the disk.";
scripts = {
init = mkOption {
type = types.str;
description = "A script for loading the device path into the device variable";
};
partition = mkOption {
type = types.str;
description = "A script for partitioning and formatting the device.";
};
};
};
@ -172,35 +179,37 @@ let
id = "disk-${name}";
deviceVariable = deviceVar;
deviceScript =
if config.path == null then ''
${deviceSelector}
'' else ''
${deviceVarName}=${config.path}
if [ ! -b ${deviceVar} ]; then
function fallback() {
echo "Couldn't find the specified disk \"${deviceVar}\"."
if fish ${./confirm.fish} "Do you want to choose a different \"${name}\" disk?"; then
${deviceSelector}
else
exit 1
fi
}
scripts = {
init =
if config.path == null then ''
${deviceSelector}
'' else ''
${deviceVarName}=${config.path}
if [ ! -b ${deviceVar} ]; then
function fallback() {
echo "Couldn't find the specified disk \"${deviceVar}\"."
if fish ${./confirm.fish} "Do you want to choose a different \"${name}\" disk?"; then
${deviceSelector}
else
exit 1
fi
}
fallback
fi
fallback
fi
'';
partition = lib.mkDefault ''
function partition() {
${if (!config.wipe) then cleanup else ""}
${probeScript}
${fdiskCommands}
${fixType}
}
partition
'';
script = lib.mkDefault ''
function partition() {
${if (!config.wipe) then cleanup else ""}
${probeScript}
${fdiskCommands}
${fixType}
}
partition
'';
};
};
}
);
@ -332,8 +341,8 @@ in
partitions = (builtins.concatMap (_: (builtins.attrValues _.partitions)) disks);
in
{
init = lib.strings.concatLines (builtins.map (_: _.deviceScript) disks);
partition = lib.strings.concatLines (builtins.map (_: _.script) disks);
init = lib.strings.concatLines (builtins.map (_: _.scripts.init) disks);
partition = lib.strings.concatLines (builtins.map (_: _.scripts.partition) disks);
mount = lib.strings.concatLines (
(builtins.concatMap
(
@ -341,26 +350,27 @@ in
probeScript
(builtins.concatStringsSep " " (
[ "sudo" "mount" "--mkdir" ] ++
(lib.optionals (_.format == "ntfs") [ "-t" "ntfs3" ]) ++
[
(builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions))
(partPath _)
(normalizePath "/${rootDir}/${_.mountPoint}")
]
(lib.optionals (_.format == "ntfs") [ "-t" "ntfs3" ]) ++
[
(builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions))
(partPath _)
(normalizePath "/${rootDir}/${_.mountPoint}")
]
))
]
)
(lib.lists.sortOn
(_: normalizePath "/${_.mountPoint}")
(builtins.filter (_: _.mountPoint != null) partitions))) ++
(builtins.map
(
_: ''
${probeScript}
sudo swapon ${partPath _}
''
)
(builtins.filter (_: _.useSwap) partitions)));
(builtins.map
(
_: ''
${probeScript}
sudo swapon ${partPath _}
''
)
(builtins.filter (_: _.useSwap) partitions))
);
};
};
};