2024-06-21 03:09:18 +00:00
|
|
|
{ lib, config, ... }:
|
2024-06-20 16:42:40 +00:00
|
|
|
let
|
2024-06-21 03:09:18 +00:00
|
|
|
inherit (lib) types mkOption;
|
|
|
|
diskVarName = "myDisk";
|
|
|
|
diskVar = ''''${${diskVarName}}'';
|
2024-06-22 16:34:36 +00:00
|
|
|
isSwap = partition: builtins.elem partition.type ["swap" 19];
|
2024-06-21 03:09:18 +00:00
|
|
|
|
2024-06-22 18:00:03 +00:00
|
|
|
fs = {
|
|
|
|
ext4 = "ext4";
|
2024-06-22 18:01:17 +00:00
|
|
|
swap = "swap";
|
2024-06-22 18:00:03 +00:00
|
|
|
ntfs = "ntfs";
|
|
|
|
fat32 = "fat32";
|
|
|
|
};
|
|
|
|
|
2024-06-21 03:09:18 +00:00
|
|
|
diskSelector = ''
|
|
|
|
. ${./../../scripts/Common/Scripts/choose-disk.sh};
|
|
|
|
chooseDisk ${diskVarName} "Which disk do you wish to install the OS on?";
|
|
|
|
'';
|
|
|
|
|
|
|
|
mkDiskType = osDisk: types.submodule (
|
|
|
|
{ config, name, ... }: {
|
|
|
|
options = {
|
|
|
|
wipe = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
description = "A value indicating whether the disk should be wiped.";
|
|
|
|
default = !(lib.lists.any (_: _.keepExisting) (builtins.attrValues config.partitions));
|
|
|
|
};
|
|
|
|
|
|
|
|
deviceName = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "The name of the device.";
|
|
|
|
default = if osDisk then null else name;
|
|
|
|
};
|
|
|
|
|
|
|
|
devicePath = mkOption {
|
|
|
|
type =
|
|
|
|
if osDisk
|
|
|
|
then types.nullOr types.str
|
|
|
|
else types.str;
|
|
|
|
description = "The path to the device.";
|
|
|
|
default =
|
|
|
|
if osDisk && config.deviceName == null
|
|
|
|
then null
|
|
|
|
else "/dev/${config.deviceName}";
|
|
|
|
};
|
|
|
|
|
|
|
|
partitions = mkOption {
|
2024-06-22 18:10:56 +00:00
|
|
|
type = types.attrsOf (types.nullOr partitionType);
|
2024-06-21 03:09:18 +00:00
|
|
|
description = "The partitions of the disk.";
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
script = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The script for formatting the disk.";
|
|
|
|
internal = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config =
|
|
|
|
let
|
2024-06-22 16:34:36 +00:00
|
|
|
partitions = builtins.foldl'
|
|
|
|
(list: predicate: lib.lists.sortOn predicate list)
|
2024-06-22 18:10:56 +00:00
|
|
|
(builtins.filter (_: _ != null) (builtins.attrValues config.partitions))
|
2024-06-22 16:34:36 +00:00
|
|
|
[
|
|
|
|
(_: _.index)
|
|
|
|
(_: _.order)
|
|
|
|
(_: if ((!isSwap _) && (_.size == null)) then 1 else 0)
|
|
|
|
];
|
|
|
|
|
2024-06-22 20:50:58 +00:00
|
|
|
mkType = type:
|
|
|
|
lib.strings.escapeShellArg (
|
|
|
|
if builtins.isInt type
|
|
|
|
then "${lib.trivial.toHexString type}"
|
|
|
|
else type);
|
|
|
|
|
2024-06-22 16:53:09 +00:00
|
|
|
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;
|
|
|
|
|
2024-06-21 03:09:18 +00:00
|
|
|
cleanup = lib.strings.concatLines (builtins.map
|
2024-06-23 13:03:47 +00:00
|
|
|
(partition: "${fdiskCommand "--delete ${diskVar} ${toString partition.index}"} || true")
|
2024-06-21 03:09:18 +00:00
|
|
|
(builtins.filter (_: !_.keepExisting) partitions));
|
2024-06-22 16:53:09 +00:00
|
|
|
|
|
|
|
fdiskCommands = lib.strings.concatLines (
|
|
|
|
lib.optionals config.wipe [
|
|
|
|
cleanup
|
|
|
|
(wipeScript "label: gpt")
|
|
|
|
] ++
|
|
|
|
(builtins.concatMap (
|
|
|
|
partition:
|
|
|
|
let
|
|
|
|
inherit (partition)
|
2024-06-22 18:00:03 +00:00
|
|
|
format
|
2024-06-22 16:53:09 +00:00
|
|
|
index
|
|
|
|
keepExisting
|
2024-06-22 18:00:25 +00:00
|
|
|
label
|
2024-06-22 16:53:09 +00:00
|
|
|
sizeScript
|
|
|
|
type
|
|
|
|
;
|
|
|
|
|
2024-06-22 19:42:43 +00:00
|
|
|
partVarName = "myPartition";
|
|
|
|
partVar = ''''${${partVarName}}'';
|
|
|
|
|
2024-06-22 16:53:09 +00:00
|
|
|
sizeOption = ''
|
|
|
|
${sizeScript} | sed -e "s/.*[^[:space:]]/size=\0/"
|
|
|
|
'';
|
|
|
|
|
2024-06-22 18:00:03 +00:00
|
|
|
formatScripts = {
|
2024-06-22 19:42:43 +00:00
|
|
|
${fs.ext4} = "mkfs.ext4 -F ${partVar}";
|
|
|
|
${fs.swap} = "mkswap ${partVar}";
|
|
|
|
${fs.ntfs} = "mkfs.ntfs -F ${partVar}";
|
|
|
|
${fs.fat32} = "mkfs.fat -F 32 ${partVar}";
|
2024-06-22 18:00:03 +00:00
|
|
|
};
|
|
|
|
|
2024-06-22 18:00:25 +00:00
|
|
|
labelScripts = {
|
2024-06-22 19:42:43 +00:00
|
|
|
${fs.ext4} = label: "e2label ${partVar} ${label}";
|
2024-06-23 13:03:56 +00:00
|
|
|
${fs.swap} = label: "swaplabel ${partVar} --label ${label}";
|
2024-06-22 19:42:43 +00:00
|
|
|
${fs.ntfs} = label: "ntfslabel ${partVar} ${label}";
|
|
|
|
${fs.fat32} = label: "fatlabel ${partVar} ${label}";
|
2024-06-22 18:00:25 +00:00
|
|
|
};
|
|
|
|
|
2024-06-22 18:00:03 +00:00
|
|
|
create = lib.strings.concatLines [
|
2024-06-22 20:50:58 +00:00
|
|
|
(appendScript "${toString index}: \"$(${sizeOption})\" type=${mkType type}")
|
2024-06-22 20:51:11 +00:00
|
|
|
# Wait for partition to be detected
|
|
|
|
''
|
|
|
|
while true;
|
|
|
|
do
|
|
|
|
[ -b ${partVar} ] && break;
|
|
|
|
done;
|
|
|
|
''
|
|
|
|
"sudo ${formatScripts.${format}}"
|
2024-06-22 18:00:03 +00:00
|
|
|
];
|
|
|
|
|
2024-06-22 16:53:09 +00:00
|
|
|
fallback = ''
|
2024-06-22 18:00:03 +00:00
|
|
|
if ! { ls "${diskVar}"?(p)${toString index} 2>&1; } > /dev/null
|
2024-06-22 16:53:09 +00:00
|
|
|
then
|
|
|
|
${create}
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
in [
|
2024-06-22 19:42:43 +00:00
|
|
|
''local diskPath="$(find -L /dev/disk/by-diskseq -samefile ${diskVar})"''
|
|
|
|
''local ${partVarName}="$diskPath-part${toString index}"''
|
2024-06-22 20:51:11 +00:00
|
|
|
(if keepExisting then fallback else create)
|
2024-06-22 19:52:20 +00:00
|
|
|
"sudo ${labelScripts.${format} label}"
|
2024-06-22 16:53:09 +00:00
|
|
|
])
|
|
|
|
partitions)
|
|
|
|
);
|
|
|
|
|
|
|
|
fixType = lib.strings.concatLines (
|
2024-06-21 03:09:18 +00:00
|
|
|
builtins.concatMap
|
|
|
|
(partition: lib.optional
|
2024-06-22 20:50:58 +00:00
|
|
|
(partition.keepExisting && !(builtins.isNull partition.type))
|
|
|
|
''sudo sfdisk --part-type ${diskVar} ${toString partition.index} ${mkType partition.type}'')
|
2024-06-21 03:09:18 +00:00
|
|
|
partitions);
|
|
|
|
in {
|
|
|
|
script = ''
|
|
|
|
function partition() {
|
|
|
|
${
|
|
|
|
if osDisk && config.devicePath == null then ''
|
|
|
|
${diskSelector}
|
|
|
|
''
|
2024-06-26 16:13:07 +00:00
|
|
|
else ''
|
|
|
|
local ${diskVarName}=${config.devicePath}
|
|
|
|
${if osDisk then ''
|
2024-07-06 19:52:03 +00:00
|
|
|
. ${./../../scripts/Common/Scripts/is-truthy.sh}
|
2024-06-26 16:13:07 +00:00
|
|
|
if [ ! -b ${diskVar} ]; then
|
|
|
|
function fallback() {
|
|
|
|
echo "Couldn't find the specified disk \"${diskVar}\"."
|
|
|
|
local answer
|
2024-07-06 19:52:26 +00:00
|
|
|
read -p "Do you want to install the OS on another disk? [y/n] " answer
|
2024-06-26 16:13:07 +00:00
|
|
|
if isTruthy "$answer"; then
|
|
|
|
${diskSelector}
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
fallback;
|
|
|
|
fi
|
|
|
|
'' else ""}
|
|
|
|
''}
|
2024-06-21 03:09:18 +00:00
|
|
|
|
|
|
|
${if (!config.wipe) then cleanup else ""}
|
|
|
|
|
2024-06-22 16:53:09 +00:00
|
|
|
${fdiskCommands}
|
|
|
|
${fixType}
|
2024-06-21 03:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
partition
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
partitionType = types.submodule (
|
2024-06-22 16:34:36 +00:00
|
|
|
{ name, config, ... }: {
|
2024-06-21 03:09:18 +00:00
|
|
|
options = {
|
|
|
|
index = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
description = "The index of the partition.";
|
|
|
|
};
|
|
|
|
|
|
|
|
order = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
description = "The sort order of the partition creation.";
|
2024-06-22 17:47:46 +00:00
|
|
|
default = 0;
|
2024-06-21 03:09:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The label of the partition.";
|
|
|
|
default = name;
|
|
|
|
};
|
|
|
|
|
|
|
|
keepExisting = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
description = "A value indicating whether the partition should be left untouched if it already exists.";
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2024-06-22 17:45:14 +00:00
|
|
|
type = mkOption {
|
|
|
|
type = types.nullOr (types.either types.str types.int);
|
|
|
|
description = "The type of the partition.";
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2024-06-22 18:00:03 +00:00
|
|
|
format = mkOption {
|
|
|
|
type = types.enum (builtins.attrValues fs);
|
|
|
|
description = "The file system format of the partition.";
|
2024-06-22 18:06:01 +00:00
|
|
|
default =
|
|
|
|
if (isSwap config)
|
|
|
|
then fs.swap
|
|
|
|
else throw("Partition format not specified.");
|
2024-06-22 18:00:03 +00:00
|
|
|
};
|
|
|
|
|
2024-06-21 03:09:18 +00:00
|
|
|
size = mkOption {
|
2024-06-22 16:21:25 +00:00
|
|
|
type = types.nullOr types.str;
|
2024-06-21 03:09:18 +00:00
|
|
|
description = "The size of the partition.";
|
2024-06-22 16:21:25 +00:00
|
|
|
default = null;
|
2024-06-21 03:09:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
sizeScript = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "A script for printing the size to the console.";
|
|
|
|
internal = true;
|
|
|
|
};
|
2024-06-22 18:45:15 +00:00
|
|
|
|
|
|
|
useSwap = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
description = "A value indicating whether this partition should be used as swap.";
|
|
|
|
default = isSwap config;
|
|
|
|
};
|
|
|
|
|
|
|
|
mountPoint = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "The mountpoint of the partition.";
|
|
|
|
default = null;
|
|
|
|
};
|
2024-06-22 18:52:46 +00:00
|
|
|
|
|
|
|
mountOptions = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = "The options to apply to the mount.";
|
|
|
|
default = [];
|
|
|
|
};
|
2024-06-21 03:09:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
sizeScript = (
|
2024-06-22 16:34:36 +00:00
|
|
|
if isSwap config
|
2024-06-21 03:09:18 +00:00
|
|
|
then
|
|
|
|
''echo "$(cat /proc/meminfo | awk -F " " '/^MemTotal/ { print $2 }' | awk '{ print int((($1 / 1024 / 1024) * 0.75) + 0.5)}')"G''
|
|
|
|
else
|
2024-06-22 16:21:25 +00:00
|
|
|
"echo ${lib.strings.escapeShellArg (toString config.size)}"
|
2024-06-21 03:09:18 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2024-06-20 16:42:40 +00:00
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
partition = {
|
2024-06-22 18:45:15 +00:00
|
|
|
rootDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The root of the installation directory to mount disks into.";
|
|
|
|
default = "/mnt";
|
|
|
|
};
|
|
|
|
|
2024-06-21 03:09:18 +00:00
|
|
|
os = mkOption {
|
|
|
|
type = mkDiskType true;
|
|
|
|
description = "The partition layout of the OS disk.";
|
|
|
|
};
|
|
|
|
|
|
|
|
disks = mkOption {
|
|
|
|
type = types.attrsOf (mkDiskType false);
|
2024-06-22 16:57:17 +00:00
|
|
|
description = "The additional disks to format.";
|
|
|
|
default = {};
|
2024-06-21 03:09:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
script = mkOption {
|
|
|
|
type = types.lines;
|
2024-06-20 16:42:40 +00:00
|
|
|
description = "The script for partitioning the system's disks.";
|
2024-06-21 03:09:18 +00:00
|
|
|
internal = true;
|
2024-06-20 16:42:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
partition = {
|
2024-06-22 16:58:05 +00:00
|
|
|
script =
|
|
|
|
let
|
2024-06-22 18:45:15 +00:00
|
|
|
inherit (config.partition) os rootDir;
|
|
|
|
inherit (lib.strings) normalizePath;
|
|
|
|
|
|
|
|
partPath = part: "/dev/disk/by-label/${part.label}";
|
|
|
|
disks = ([os] ++ (builtins.attrValues config.partition.disks));
|
|
|
|
partitions = (builtins.concatMap (_: (builtins.attrValues _.partitions)) disks);
|
|
|
|
|
|
|
|
mountScript = lib.strings.concatLines (
|
|
|
|
builtins.map
|
|
|
|
(_: builtins.concatStringsSep " " [
|
2024-06-22 19:44:27 +00:00
|
|
|
"sudo"
|
2024-06-22 18:45:15 +00:00
|
|
|
"mount"
|
2024-06-22 18:46:15 +00:00
|
|
|
"--mkdir"
|
2024-06-22 18:52:46 +00:00
|
|
|
(builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions))
|
2024-06-22 18:45:15 +00:00
|
|
|
(partPath _)
|
|
|
|
(normalizePath "/${rootDir}/${_.mountPoint}")
|
|
|
|
])
|
2024-06-23 14:15:34 +00:00
|
|
|
(lib.lists.sortOn
|
2024-06-23 14:33:11 +00:00
|
|
|
(_: normalizePath "/${_.mountPoint}")
|
2024-06-23 14:15:34 +00:00
|
|
|
(builtins.filter (_: _.mountPoint != null) partitions)));
|
2024-06-22 18:45:15 +00:00
|
|
|
|
|
|
|
swapScript = lib.strings.concatLines (
|
|
|
|
builtins.map
|
2024-06-22 19:44:27 +00:00
|
|
|
(_: "sudo swapon ${partPath _}")
|
2024-06-22 18:45:15 +00:00
|
|
|
(builtins.filter (_: _.useSwap) partitions));
|
|
|
|
in lib.strings.concatLines (
|
|
|
|
(builtins.map (_: _.script) disks) ++ [
|
|
|
|
mountScript
|
|
|
|
swapScript
|
|
|
|
]
|
|
|
|
);
|
2024-06-22 18:45:50 +00:00
|
|
|
|
|
|
|
os = {
|
|
|
|
partitions = {
|
|
|
|
Boot = {
|
|
|
|
index = 1;
|
|
|
|
type = "uefi";
|
|
|
|
size = "+1G";
|
|
|
|
format = fs.fat32;
|
2024-06-22 18:46:57 +00:00
|
|
|
mountPoint = "/boot";
|
2024-06-22 18:45:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Swap = {
|
|
|
|
index = 2;
|
|
|
|
type = "swap";
|
|
|
|
};
|
|
|
|
|
|
|
|
OS = {
|
|
|
|
index = 3;
|
|
|
|
type = "linux";
|
|
|
|
format = fs.ext4;
|
2024-06-22 18:46:57 +00:00
|
|
|
mountPoint = "/";
|
2024-06-22 18:45:50 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-06-20 16:42:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|