types: pass parent node to all subTypes

This commit is contained in:
lassulus 2023-06-07 13:42:39 +02:00
parent e0179917d8
commit 62f939e213
15 changed files with 79 additions and 27 deletions

View File

@ -7,31 +7,40 @@ let
diskoLib = {
# like lib.types.oneOf but instead of a list takes an attrset
# uses the field "type" to find the correct type in the attrset
subType = typeAttr: lib.mkOptionType rec {
subType = { types, extraArgs ? { parent = { type = "rootNode"; name = "root"; }; } }: lib.mkOptionType rec {
name = "subType";
description = "one of ${concatStringsSep "," (attrNames typeAttr)}";
check = x: if x ? type then typeAttr.${x.type}.check x else throw "No type option set in:\n${generators.toPretty {} x}";
merge = loc: foldl' (_res: def: typeAttr.${def.value.type}.merge loc [ def ]) { };
nestedTypes = typeAttr;
description = "one of ${concatStringsSep "," (attrNames types)}";
check = x: if x ? type then types.${x.type}.check x else throw "No type option set in:\n${generators.toPretty {} x}";
merge = loc: foldl' (res: def: types.${def.value.type}.merge loc [
# we add a dummy root parent node to render documentation
(lib.recursiveUpdate { value._module.args = extraArgs; } def)
]) { };
nestedTypes = types;
};
# option for valid contents of partitions (basically like devices, but without tables)
partitionType = lib.mkOption {
type = lib.types.nullOr (diskoLib.subType { inherit (diskoLib.types) btrfs filesystem zfs mdraid luks lvm_pv swap; });
partitionType = extraArgs: lib.mkOption {
type = lib.types.nullOr (diskoLib.subType {
types = { inherit (diskoLib.types) btrfs filesystem zfs mdraid luks lvm_pv swap; };
inherit extraArgs;
});
default = null;
description = "The type of partition";
};
# option for valid contents of devices
deviceType = lib.mkOption {
type = lib.types.nullOr (diskoLib.subType { inherit (diskoLib.types) table btrfs filesystem zfs mdraid luks lvm_pv swap; });
deviceType = extraArgs: lib.mkOption {
type = lib.types.nullOr (diskoLib.subType {
types = { inherit (diskoLib.types) table table_gpt btrfs filesystem zfs mdraid luks lvm_pv swap; };
inherit extraArgs;
});
default = null;
description = "The type of device";
};
/* deepMergeMap takes a function and a list of attrsets and deep merges them
deepMergeMap :: -> (AttrSet -> AttrSet ) -> [ AttrSet ] -> Attrset
deepMergeMap :: (AttrSet -> AttrSet ) -> [ AttrSet ] -> Attrset
Example:
deepMergeMap (x: x.t = "test") [ { x = { y = 1; z = 3; }; } { x = { bla = 234; }; } ]

View File

@ -1,4 +1,4 @@
{ config, options, diskoLib, lib, rootMountPoint, ... }:
{ config, options, diskoLib, lib, rootMountPoint, parent, ... }:
{
options = {
type = lib.mkOption {
@ -55,6 +55,10 @@
default = null;
description = "A path to mount the BTRFS filesystem to.";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -16,7 +16,7 @@
type = diskoLib.optionTypes.absolute-pathname; # TODO check if subpath of /dev ? - No! eg: /.swapfile
description = "Device path";
};
content = diskoLib.deviceType;
content = diskoLib.deviceType { parent = config; };
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, rootMountPoint, ... }:
{ config, options, lib, diskoLib, rootMountPoint, parent, ... }:
{
options = {
type = lib.mkOption {
@ -25,6 +25,10 @@
type = lib.types.str;
description = "Format of the filesystem";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, parent, ... }:
{
options = {
type = lib.mkOption {
@ -33,7 +33,11 @@
description = "Extra arguments to pass to `cryptsetup luksOpen` when opening";
example = [ "--allow-discards" ];
};
content = diskoLib.deviceType;
content = diskoLib.deviceType { parent = config; };
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, parent, ... }:
{
options = {
type = lib.mkOption {
@ -10,6 +10,10 @@
type = lib.types.str;
description = "Volume group";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -33,7 +33,7 @@
default = [ ];
description = "Extra arguments";
};
content = diskoLib.partitionType;
content = diskoLib.partitionType { parent = config; };
};
}));
default = { };

View File

@ -22,7 +22,7 @@
default = "default";
description = "Metadata";
};
content = diskoLib.deviceType;
content = diskoLib.deviceType { parent = config; };
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, parent, ... }:
{
options = {
type = lib.mkOption {
@ -11,6 +11,10 @@
type = lib.types.str;
description = "Name";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ diskoLib, config, options, lib, ... }:
{ diskoLib, config, options, lib, parent, ... }:
{
options = {
type = lib.mkOption {
@ -11,6 +11,10 @@
default = false;
description = "Whether to randomly encrypt the swap";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, parent, ... }:
{
options = {
type = lib.mkOption {
@ -48,12 +48,16 @@
default = false;
description = "Whether to make the partition bootable";
};
content = diskoLib.partitionType;
content = diskoLib.partitionType { parent = config; };
};
}));
default = [ ];
description = "List of partitions to add to the partition table";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, parent, ... }:
{
options = {
type = lib.mkOption {
@ -10,6 +10,10 @@
type = lib.types.str;
description = "Name of the ZFS pool";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, rootMountPoint, ... }:
{ config, options, lib, diskoLib, rootMountPoint, parent, ... }:
{
options = {
name = lib.mkOption {
@ -29,6 +29,10 @@
description = "Path to mount the dataset to";
};
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -1,4 +1,4 @@
{ config, options, lib, diskoLib, ... }:
{ config, options, lib, diskoLib, parent, ... }:
{
options = {
name = lib.mkOption {
@ -30,8 +30,12 @@
description = "Size of the dataset";
};
content = diskoLib.partitionType;
content = diskoLib.partitionType { parent = config; };
_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;

View File

@ -49,7 +49,10 @@
'';
};
datasets = lib.mkOption {
type = lib.types.attrsOf (diskoLib.subType { inherit (diskoLib.types) zfs_fs zfs_volume; });
type = lib.types.attrsOf (diskoLib.subType {
types = { inherit (diskoLib.types) zfs_fs zfs_volume; };
extraArgs.parent = config;
});
description = "List of datasets to define";
};
_meta = lib.mkOption {