diff --git a/lib/default.nix b/lib/default.nix index be96594..79cba7a 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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; }; } ] diff --git a/lib/types/btrfs.nix b/lib/types/btrfs.nix index 88f207a..18718f9 100644 --- a/lib/types/btrfs.nix +++ b/lib/types/btrfs.nix @@ -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; diff --git a/lib/types/disk.nix b/lib/types/disk.nix index 06499ac..1f30788 100644 --- a/lib/types/disk.nix +++ b/lib/types/disk.nix @@ -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; diff --git a/lib/types/filesystem.nix b/lib/types/filesystem.nix index c604938..1e15e51 100644 --- a/lib/types/filesystem.nix +++ b/lib/types/filesystem.nix @@ -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; diff --git a/lib/types/luks.nix b/lib/types/luks.nix index bd8a5fc..55fd994 100644 --- a/lib/types/luks.nix +++ b/lib/types/luks.nix @@ -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; diff --git a/lib/types/lvm_pv.nix b/lib/types/lvm_pv.nix index bc988dc..98c193a 100644 --- a/lib/types/lvm_pv.nix +++ b/lib/types/lvm_pv.nix @@ -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; diff --git a/lib/types/lvm_vg.nix b/lib/types/lvm_vg.nix index 1a6d3e5..53e3d31 100644 --- a/lib/types/lvm_vg.nix +++ b/lib/types/lvm_vg.nix @@ -33,7 +33,7 @@ default = [ ]; description = "Extra arguments"; }; - content = diskoLib.partitionType; + content = diskoLib.partitionType { parent = config; }; }; })); default = { }; diff --git a/lib/types/mdadm.nix b/lib/types/mdadm.nix index 5d1f043..f2edd6d 100644 --- a/lib/types/mdadm.nix +++ b/lib/types/mdadm.nix @@ -22,7 +22,7 @@ default = "default"; description = "Metadata"; }; - content = diskoLib.deviceType; + content = diskoLib.deviceType { parent = config; }; _meta = lib.mkOption { internal = true; readOnly = true; diff --git a/lib/types/mdraid.nix b/lib/types/mdraid.nix index 1c7a0d0..79d097c 100644 --- a/lib/types/mdraid.nix +++ b/lib/types/mdraid.nix @@ -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; diff --git a/lib/types/swap.nix b/lib/types/swap.nix index e886453..496be46 100644 --- a/lib/types/swap.nix +++ b/lib/types/swap.nix @@ -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; diff --git a/lib/types/table.nix b/lib/types/table.nix index 0009990..9353507 100644 --- a/lib/types/table.nix +++ b/lib/types/table.nix @@ -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; diff --git a/lib/types/zfs.nix b/lib/types/zfs.nix index 257afa9..7e7589a 100644 --- a/lib/types/zfs.nix +++ b/lib/types/zfs.nix @@ -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; diff --git a/lib/types/zfs_fs.nix b/lib/types/zfs_fs.nix index 0c5e96b..c3e7ce5 100644 --- a/lib/types/zfs_fs.nix +++ b/lib/types/zfs_fs.nix @@ -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; diff --git a/lib/types/zfs_volume.nix b/lib/types/zfs_volume.nix index f0cd849..bd231f9 100644 --- a/lib/types/zfs_volume.nix +++ b/lib/types/zfs_volume.nix @@ -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; diff --git a/lib/types/zpool.nix b/lib/types/zpool.nix index 2733e8f..e53fea7 100644 --- a/lib/types/zpool.nix +++ b/lib/types/zpool.nix @@ -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 {