2023-07-01 22:39:13 +03:00
|
|
|
{ config, options, lib, diskoLib, parent, device, ... }:
|
|
|
|
let
|
|
|
|
sortedPartitions = lib.sort (x: y: x.priority < y.priority) (lib.attrValues config.partitions);
|
|
|
|
in
|
2023-06-07 14:53:13 +03:00
|
|
|
{
|
|
|
|
options = {
|
|
|
|
type = lib.mkOption {
|
|
|
|
type = lib.types.enum [ "gpt" ];
|
|
|
|
internal = true;
|
|
|
|
description = "Partition table";
|
|
|
|
};
|
2023-07-01 20:02:01 +03:00
|
|
|
device = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = device;
|
|
|
|
description = "Device to use for the partition table";
|
|
|
|
};
|
2023-06-07 14:53:13 +03:00
|
|
|
partitions = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }@partition: {
|
|
|
|
options = {
|
|
|
|
type = lib.mkOption {
|
|
|
|
type = lib.types.strMatching "[A-Fa-f0-9]{4}";
|
|
|
|
default = "8300";
|
|
|
|
description = "Filesystem type to use, run sgdisk -L to see what is available";
|
|
|
|
};
|
2023-07-01 20:02:01 +03:00
|
|
|
device = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2023-07-01 22:39:13 +03:00
|
|
|
default = if config._parent.type == "mdadm" then
|
|
|
|
# workaround because mdadm partlabel do not appear in /dev/disk/by-partlabel
|
|
|
|
"/dev/disk/by-id/md-name-any:${config._parent.name}-part${toString partition.config._index}"
|
|
|
|
else
|
|
|
|
"/dev/disk/by-partlabel/${partition.config.label}";
|
2023-07-01 20:02:01 +03:00
|
|
|
description = "Device to use for the partition";
|
|
|
|
};
|
2023-06-07 14:53:13 +03:00
|
|
|
priority = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
2023-06-17 10:07:14 +03:00
|
|
|
default = if (partition.config.size or "" == "100%") then 9001 else 1000;
|
|
|
|
description = "Priority of the partition, smaller values are created first";
|
2023-06-07 14:53:13 +03:00
|
|
|
};
|
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Name of the partition";
|
|
|
|
default = name;
|
|
|
|
};
|
|
|
|
label = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2023-07-01 22:39:13 +03:00
|
|
|
default = "${config._parent.type}-${config._parent.name}-${partition.config.name}";
|
2023-06-07 14:53:13 +03:00
|
|
|
};
|
2023-06-17 10:07:14 +03:00
|
|
|
size = lib.mkOption {
|
|
|
|
type = lib.types.either (lib.types.enum [ "100%" ]) (lib.types.strMatching "[0-9]+[KMGTP]?");
|
|
|
|
default = "0";
|
|
|
|
description = ''
|
|
|
|
Size of the partition, in sgdisk format.
|
|
|
|
sets end automatically with the + prefix
|
|
|
|
can be 100% for the whole remaining disk, will be done last in that case.
|
|
|
|
'';
|
|
|
|
};
|
2023-06-07 14:53:13 +03:00
|
|
|
start = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "0";
|
|
|
|
description = "Start of the partition, in sgdisk format, use 0 for next available range";
|
|
|
|
};
|
|
|
|
end = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2023-06-17 10:07:14 +03:00
|
|
|
default = if partition.config.size == "100%" then "-0" else "+${partition.config.size}";
|
2023-06-07 14:53:13 +03:00
|
|
|
description = ''
|
|
|
|
End of the partition, in sgdisk format.
|
2023-07-30 20:12:24 +03:00
|
|
|
Use + for relative sizes from the partitions start
|
2023-06-07 14:53:13 +03:00
|
|
|
or - for relative sizes from the disks end
|
|
|
|
'';
|
|
|
|
};
|
2023-07-01 20:02:01 +03:00
|
|
|
content = diskoLib.partitionType { parent = config; device = partition.config.device; };
|
2023-07-01 22:39:13 +03:00
|
|
|
_index = lib.mkOption {
|
|
|
|
internal = true;
|
|
|
|
default = diskoLib.indexOf (x: x.name == partition.config.name) sortedPartitions 0;
|
|
|
|
};
|
2023-06-07 14:53:13 +03:00
|
|
|
};
|
|
|
|
}));
|
|
|
|
default = [ ];
|
|
|
|
description = "Attrs of partitions to add to the partition table";
|
|
|
|
};
|
|
|
|
_parent = lib.mkOption {
|
|
|
|
internal = true;
|
|
|
|
default = parent;
|
|
|
|
};
|
|
|
|
_meta = lib.mkOption {
|
|
|
|
internal = true;
|
|
|
|
readOnly = true;
|
|
|
|
type = lib.types.functionTo diskoLib.jsonType;
|
|
|
|
default = dev:
|
2023-07-01 22:39:13 +03:00
|
|
|
lib.foldr lib.recursiveUpdate { } (map
|
|
|
|
(partition:
|
2023-06-07 14:53:13 +03:00
|
|
|
lib.optionalAttrs (partition.content != null) (partition.content._meta dev)
|
|
|
|
)
|
|
|
|
(lib.attrValues config.partitions));
|
|
|
|
description = "Metadata";
|
|
|
|
};
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
inherit config options;
|
2023-07-01 20:02:01 +03:00
|
|
|
default = ''
|
2023-07-01 22:39:13 +03:00
|
|
|
${lib.concatStrings (map (partition: ''
|
2023-06-07 14:53:13 +03:00
|
|
|
sgdisk \
|
2023-07-01 22:39:13 +03:00
|
|
|
--new=${toString partition._index}:${partition.start}:${partition.end} \
|
|
|
|
--change-name=${toString partition._index}:${partition.label} \
|
|
|
|
--typecode=${toString partition._index}:${partition.type} \
|
2023-07-01 20:02:01 +03:00
|
|
|
${config.device}
|
2023-06-07 14:53:13 +03:00
|
|
|
# ensure /dev/disk/by-path/..-partN exists before continuing
|
2023-07-02 01:17:48 +03:00
|
|
|
udevadm trigger --subsystem-match=block
|
|
|
|
udevadm settle
|
2023-07-01 20:02:01 +03:00
|
|
|
${lib.optionalString (partition.content != null) partition.content._create}
|
2023-07-01 22:39:13 +03:00
|
|
|
'') sortedPartitions)}
|
2023-06-17 10:07:14 +03:00
|
|
|
|
2023-06-07 14:53:13 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
inherit config options;
|
2023-07-01 20:02:01 +03:00
|
|
|
default =
|
2023-06-07 14:53:13 +03:00
|
|
|
let
|
2023-07-01 22:39:13 +03:00
|
|
|
partMounts = lib.foldr lib.recursiveUpdate { } (map
|
|
|
|
(partition:
|
2023-07-01 20:02:01 +03:00
|
|
|
lib.optionalAttrs (partition.content != null) partition.content._mount
|
2023-06-07 14:53:13 +03:00
|
|
|
)
|
|
|
|
(lib.attrValues config.partitions));
|
|
|
|
in
|
|
|
|
{
|
|
|
|
dev = partMounts.dev or "";
|
|
|
|
fs = partMounts.fs or { };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
_config = lib.mkOption {
|
|
|
|
internal = true;
|
|
|
|
readOnly = true;
|
2023-07-20 19:04:41 +03:00
|
|
|
default = (map
|
2023-07-01 20:02:01 +03:00
|
|
|
(partition:
|
|
|
|
lib.optional (partition.content != null) partition.content._config
|
2023-06-07 14:53:13 +03:00
|
|
|
)
|
2023-07-20 19:04:41 +03:00
|
|
|
(lib.attrValues config.partitions))
|
|
|
|
++ (lib.optional (lib.any (part: part.type == "EF02") (lib.attrValues config.partitions)) {
|
|
|
|
boot.loader.grub.devices = [ config.device ];
|
|
|
|
});
|
2023-06-07 14:53:13 +03:00
|
|
|
description = "NixOS configuration";
|
|
|
|
};
|
|
|
|
_pkgs = lib.mkOption {
|
|
|
|
internal = true;
|
|
|
|
readOnly = true;
|
|
|
|
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
|
|
|
default = pkgs:
|
|
|
|
[ pkgs.gptfdisk pkgs.systemdMinimal ] ++ lib.flatten (map
|
|
|
|
(partition:
|
|
|
|
lib.optional (partition.content != null) (partition.content._pkgs pkgs)
|
|
|
|
)
|
|
|
|
(lib.attrValues config.partitions));
|
|
|
|
description = "Packages";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|