disko/lib/types/table.nix

138 lines
5.0 KiB
Nix
Raw Normal View History

{ config, options, lib, diskoLib, parent, ... }:
2023-01-28 18:19:13 +03:00
{
options = {
type = lib.mkOption {
type = lib.types.enum [ "table" ];
internal = true;
description = "Partition table";
};
format = lib.mkOption {
type = lib.types.enum [ "gpt" "msdos" ];
default = "gpt";
description = "The kind of partition table";
};
partitions = lib.mkOption {
2023-06-06 14:32:47 +03:00
type = lib.types.listOf (lib.types.submodule ({ ... }: {
2023-04-07 15:29:39 +03:00
options = {
part-type = lib.mkOption {
type = lib.types.enum [ "primary" "logical" "extended" ];
default = "primary";
description = "Partition type";
};
fs-type = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "btrfs" "ext2" "ext3" "ext4" "fat16" "fat32" "hfs" "hfs+" "linux-swap" "ntfs" "reiserfs" "udf" "xfs" ]);
default = null;
description = "Filesystem type to use";
};
name = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = "Name of the partition";
};
start = lib.mkOption {
type = lib.types.str;
default = "0%";
description = "Start of the partition";
};
end = lib.mkOption {
type = lib.types.str;
default = "100%";
description = "End of the partition";
};
flags = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "Partition flags";
};
bootable = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to make the partition bootable";
};
content = diskoLib.partitionType { parent = config; };
2023-04-07 15:29:39 +03:00
};
}));
2023-01-28 18:19:13 +03:00
default = [ ];
description = "List of partitions to add to the partition table";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
2023-01-28 18:19:13 +03:00
_meta = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo diskoLib.jsonType;
default = dev:
2023-05-12 18:20:14 +03:00
lib.foldr lib.recursiveUpdate { } (lib.imap
2023-06-06 14:32:47 +03:00
(_index: partition:
2023-05-12 18:20:14 +03:00
lib.optionalAttrs (partition.content != null) (partition.content._meta dev)
)
config.partitions);
2023-01-28 18:19:13 +03:00
description = "Metadata";
};
_create = diskoLib.mkCreateOption {
inherit config options;
default = { dev }: ''
parted -s ${dev} -- mklabel ${config.format}
2023-04-07 15:29:39 +03:00
${lib.concatStrings (lib.imap (index: partition: ''
${lib.optionalString (config.format == "gpt") ''
parted -s ${dev} -- mkpart ${partition.name} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end}
''}
${lib.optionalString (config.format == "msdos") ''
parted -s ${dev} -- mkpart ${partition.part-type} ${diskoLib.maybeStr partition.fs-type} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end}
''}
# ensure /dev/disk/by-path/..-partN exists before continuing
udevadm trigger --subsystem-match=block; udevadm settle
${lib.optionalString partition.bootable ''
parted -s ${dev} -- set ${toString index} boot on
''}
${lib.concatMapStringsSep "" (flag: ''
parted -s ${dev} -- set ${toString index} ${flag} on
'') partition.flags}
# ensure further operations can detect new partitions
udevadm trigger --subsystem-match=block; udevadm settle
${lib.optionalString (partition.content != null) (partition.content._create { dev = diskoLib.deviceNumbering dev index; })}
'') config.partitions)}
2023-01-28 18:19:13 +03:00
'';
};
_mount = diskoLib.mkMountOption {
inherit config options;
default = { dev }:
let
2023-05-12 18:20:14 +03:00
partMounts = lib.foldr lib.recursiveUpdate { } (lib.imap
(index: partition:
lib.optionalAttrs (partition.content != null) (partition.content._mount { dev = diskoLib.deviceNumbering dev index; })
)
config.partitions);
2023-01-28 18:19:13 +03:00
in
{
2023-03-21 08:34:11 +03:00
dev = partMounts.dev or "";
2023-01-28 18:19:13 +03:00
fs = partMounts.fs or { };
};
};
_config = lib.mkOption {
internal = true;
readOnly = true;
default = dev:
2023-05-12 18:20:14 +03:00
lib.imap
(index: partition:
lib.optional (partition.content != null) (partition.content._config (diskoLib.deviceNumbering dev index))
)
config.partitions;
2023-01-28 18:19: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:
2023-05-12 18:20:14 +03:00
[ pkgs.parted pkgs.systemdMinimal ] ++ lib.flatten (map
(partition:
lib.optional (partition.content != null) (partition.content._pkgs pkgs)
)
config.partitions);
2023-01-28 18:19:13 +03:00
description = "Packages";
};
};
}