mirror of
https://github.com/TECHNOFAB11/disko.git
synced 2025-12-12 16:10:03 +01:00
lib: read types from subdir
This commit is contained in:
parent
22b33a4fd6
commit
1c31612335
17 changed files with 10 additions and 19 deletions
86
lib/types/btrfs.nix
Normal file
86
lib/types/btrfs.nix
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
{ config, options, diskoLib, lib, rootMountPoint, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "btrfs" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
extraArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
description = "A list of options to pass to mount.";
|
||||
};
|
||||
subvolumes = lib.mkOption {
|
||||
type = lib.types.attrsOf diskoLib.types.btrfs_subvol;
|
||||
default = { };
|
||||
description = "Subvolumes to define for BTRFS.";
|
||||
};
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "A path to mount the BTRFS filesystem to.";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev:
|
||||
diskoLib.deepMergeMap (subvol: subvol._meta dev) (lib.attrValues config.subvolumes);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
mkfs.btrfs ${dev} ${toString config.extraArgs}
|
||||
${lib.concatMapStrings (subvol: subvol._create { inherit dev; }) (lib.attrValues config.subvolumes)}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }:
|
||||
let
|
||||
subvolMounts = diskoLib.deepMergeMap (subvol: subvol._mount { inherit dev; parent = config.mountpoint; }) (lib.attrValues config.subvolumes);
|
||||
in
|
||||
{
|
||||
fs = subvolMounts.fs // lib.optionalAttrs (config.mountpoint != null) {
|
||||
${config.mountpoint} = ''
|
||||
if ! findmnt ${dev} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
|
||||
mount ${dev} "${rootMountPoint}${config.mountpoint}" \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
||||
-o X-mount.mkdir
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: [
|
||||
(map (subvol: subvol._config dev config.mountpoint) (lib.attrValues config.subvolumes))
|
||||
(lib.optional (config.mountpoint != null) {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = dev;
|
||||
fsType = "btrfs";
|
||||
options = config.mountOptions;
|
||||
};
|
||||
})
|
||||
];
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs:
|
||||
[ pkgs.btrfs-progs ] ++ lib.flatten (map (subvolume: subvolume._pkgs pkgs) (lib.attrValues config.subvolumes));
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
94
lib/types/btrfs_subvol.nix
Normal file
94
lib/types/btrfs_subvol.nix
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
{ config, options, diskoLib, lib, rootMountPoint, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name of the BTRFS subvolume.";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "btrfs_subvol" ];
|
||||
default = "btrfs_subvol";
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
extraArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
description = "Options to pass to mount";
|
||||
};
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "Location to mount the subvolume to.";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: { };
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
MNTPOINT=$(mktemp -d)
|
||||
(
|
||||
mount ${dev} "$MNTPOINT" -o subvol=/
|
||||
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
|
||||
btrfs subvolume create "$MNTPOINT"/${config.name} ${toString config.extraArgs}
|
||||
)
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev, parent }:
|
||||
let
|
||||
mountpoint =
|
||||
if (config.mountpoint != null) then config.mountpoint
|
||||
else if (parent == null) then config.name
|
||||
else null;
|
||||
in
|
||||
lib.optionalAttrs (mountpoint != null) {
|
||||
fs.${mountpoint} = ''
|
||||
if ! findmnt ${dev} "${rootMountPoint}${mountpoint}" > /dev/null 2>&1; then
|
||||
mount ${dev} "${rootMountPoint}${mountpoint}" \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") (config.mountOptions ++ [ "subvol=${config.name}" ])} \
|
||||
-o X-mount.mkdir
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: parent:
|
||||
let
|
||||
mountpoint =
|
||||
if (config.mountpoint != null) then config.mountpoint
|
||||
else if (parent == null) then config.name
|
||||
else null;
|
||||
in
|
||||
lib.optional (mountpoint != null) {
|
||||
fileSystems.${mountpoint} = {
|
||||
device = dev;
|
||||
fsType = "btrfs";
|
||||
options = config.mountOptions ++ [ "subvol=${config.name}" ];
|
||||
};
|
||||
};
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.coreutils ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
52
lib/types/disk.nix
Normal file
52
lib/types/disk.nix
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Device name";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "disk" ];
|
||||
default = "disk";
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
device = lib.mkOption {
|
||||
type = diskoLib.optionTypes.absolute-pathname; # TODO check if subpath of /dev ? - No! eg: /.swapfile
|
||||
description = "Device path";
|
||||
};
|
||||
content = diskoLib.deviceType;
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = diskoLib.jsonType;
|
||||
default =
|
||||
lib.optionalAttrs (config.content != null) (config.content._meta [ "disk" config.device ]);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = _: config.content._create { dev = config.device; };
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = _:
|
||||
lib.optionalAttrs (config.content != null) (config.content._mount { dev = config.device; });
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default =
|
||||
lib.optional (config.content != null) (config.content._config config.device);
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.jq ] ++ lib.optionals (config.content != null) (config.content._pkgs pkgs);
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
87
lib/types/filesystem.nix
Normal file
87
lib/types/filesystem.nix
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
{ config, options, lib, diskoLib, rootMountPoint, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "filesystem" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
extraArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
description = "Options to pass to mount";
|
||||
};
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "Path to mount the filesystem to";
|
||||
};
|
||||
format = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Format of the filesystem";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: { };
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
mkfs.${config.format} \
|
||||
${toString config.extraArgs} \
|
||||
${dev}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }: lib.optionalAttrs (config.mountpoint != null) {
|
||||
fs.${config.mountpoint} = ''
|
||||
if ! findmnt ${dev} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
|
||||
mount ${dev} "${rootMountPoint}${config.mountpoint}" \
|
||||
-t "${config.format}" \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
||||
-o X-mount.mkdir
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: lib.optional (config.mountpoint != null) {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = dev;
|
||||
fsType = config.format;
|
||||
options = config.mountOptions;
|
||||
};
|
||||
};
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
# type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs:
|
||||
[ pkgs.util-linux ] ++ (
|
||||
# TODO add many more
|
||||
if (config.format == "xfs") then [ pkgs.xfsprogs ]
|
||||
else if (config.format == "btrfs") then [ pkgs.btrfs-progs ]
|
||||
else if (config.format == "vfat") then [ pkgs.dosfstools ]
|
||||
else if (config.format == "ext2") then [ pkgs.e2fsprogs ]
|
||||
else if (config.format == "ext3") then [ pkgs.e2fsprogs ]
|
||||
else if (config.format == "ext4") then [ pkgs.e2fsprogs ]
|
||||
else if (config.format == "bcachefs") then [ pkgs.bcachefs-tools ]
|
||||
else [ ]
|
||||
);
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
85
lib/types/luks.nix
Normal file
85
lib/types/luks.nix
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "luks" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Name of the LUKS";
|
||||
};
|
||||
keyFile = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "Path to the key for encryption";
|
||||
example = "/tmp/disk.key";
|
||||
};
|
||||
initrdUnlock = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = "Whether to add a boot.initrd.luks.devices entry for the specified disk.";
|
||||
};
|
||||
extraFormatArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments to pass to `cryptsetup luksFormat` when formatting";
|
||||
example = [ "--pbkdf argon2id" ];
|
||||
};
|
||||
extraOpenArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments to pass to `cryptsetup luksOpen` when opening";
|
||||
example = [ "--allow-discards" ];
|
||||
};
|
||||
content = diskoLib.deviceType;
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev:
|
||||
lib.optionalAttrs (config.content != null) (config.content._meta dev);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
cryptsetup -q luksFormat ${dev} ${diskoLib.maybeStr config.keyFile} ${toString config.extraFormatArgs}
|
||||
cryptsetup luksOpen ${dev} ${config.name} ${toString config.extraOpenArgs} ${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"}
|
||||
${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/mapper/${config.name}";})}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }:
|
||||
let
|
||||
contentMount = config.content._mount { dev = "/dev/mapper/${config.name}"; };
|
||||
in
|
||||
{
|
||||
dev = ''
|
||||
cryptsetup status ${config.name} >/dev/null 2>/dev/null ||
|
||||
cryptsetup luksOpen ${dev} ${config.name} ${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"}
|
||||
${lib.optionalString (config.content != null) contentMount.dev or ""}
|
||||
'';
|
||||
fs = lib.optionalAttrs (config.content != null) contentMount.fs or { };
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: [ ]
|
||||
# If initrdUnlock is true, then add a device entry to the initrd.luks.devices config.
|
||||
++ (lib.optional config.initrdUnlock [{ boot.initrd.luks.devices.${config.name}.device = dev; }])
|
||||
++ (lib.optional (config.content != null) (config.content._config "/dev/mapper/${config.name}"));
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.cryptsetup ] ++ (lib.optionals (config.content != null) (config.content._pkgs pkgs));
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
48
lib/types/lvm_pv.nix
Normal file
48
lib/types/lvm_pv.nix
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "lvm_pv" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
vg = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Volume group";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: {
|
||||
deviceDependencies.lvm_vg.${config.vg} = [ dev ];
|
||||
};
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
pvcreate ${dev}
|
||||
echo "${dev}" >> "$disko_devices_dir"/lvm_${config.vg}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }:
|
||||
{ };
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: [ ];
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.lvm2 ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
120
lib/types/lvm_vg.nix
Normal file
120
lib/types/lvm_vg.nix
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name of the volume group";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "lvm_vg" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
lvs = lib.mkOption {
|
||||
type = lib.types.attrsOf (lib.types.submodule ({ config, ... }: {
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name of the logical volume";
|
||||
};
|
||||
size = lib.mkOption {
|
||||
type = lib.types.str; # TODO lvm size type
|
||||
description = "Size of the logical volume";
|
||||
};
|
||||
lvm_type = lib.mkOption {
|
||||
type = lib.types.nullOr (lib.types.enum [ "mirror" "raid0" "raid1" ]); # TODO add all lib.types
|
||||
default = null; # maybe there is always a default type?
|
||||
description = "LVM type";
|
||||
};
|
||||
extraArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Extra arguments";
|
||||
};
|
||||
content = diskoLib.partitionType;
|
||||
};
|
||||
}));
|
||||
default = { };
|
||||
description = "LVS for the volume group";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = diskoLib.jsonType;
|
||||
default =
|
||||
diskoLib.deepMergeMap
|
||||
(lv:
|
||||
lib.optionalAttrs (lv.content != null) (lv.content._meta [ "lvm_vg" config.name ])
|
||||
)
|
||||
(lib.attrValues config.lvs);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = _:
|
||||
let
|
||||
sortedLvs = lib.sort (a: _: !lib.hasInfix "100%" a.size) (lib.attrValues config.lvs);
|
||||
in
|
||||
''
|
||||
readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name})
|
||||
vgcreate ${config.name} \
|
||||
"''${lvm_devices[@]}"
|
||||
${lib.concatMapStrings (lv: ''
|
||||
lvcreate \
|
||||
--yes \
|
||||
${if lib.hasInfix "%" lv.size then "-l" else "-L"} ${lv.size} \
|
||||
-n ${lv.name} \
|
||||
${lib.optionalString (lv.lvm_type != null) "--type=${lv.lvm_type}"} \
|
||||
${toString lv.extraArgs} \
|
||||
${config.name}
|
||||
${lib.optionalString (lv.content != null) (lv.content._create {dev = "/dev/${config.name}/${lv.name}";})}
|
||||
'') sortedLvs}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = _:
|
||||
let
|
||||
lvMounts = diskoLib.deepMergeMap
|
||||
(lv:
|
||||
lib.optionalAttrs (lv.content != null) (lv.content._mount { dev = "/dev/${config.name}/${lv.name}"; })
|
||||
)
|
||||
(lib.attrValues config.lvs);
|
||||
in
|
||||
{
|
||||
dev = ''
|
||||
vgchange -a y
|
||||
${lib.concatMapStrings (x: x.dev or "") (lib.attrValues lvMounts)}
|
||||
'';
|
||||
fs = lvMounts.fs or { };
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default =
|
||||
map
|
||||
(lv: [
|
||||
(lib.optional (lv.content != null) (lv.content._config "/dev/${config.name}/${lv.name}"))
|
||||
(lib.optional (lv.lvm_type != null) {
|
||||
boot.initrd.kernelModules = [ "dm-${lv.lvm_type}" ];
|
||||
})
|
||||
])
|
||||
(lib.attrValues config.lvs);
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: lib.flatten (map
|
||||
(lv:
|
||||
lib.optional (lv.content != null) (lv.content._pkgs pkgs)
|
||||
)
|
||||
(lib.attrValues config.lvs));
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
70
lib/types/mdadm.nix
Normal file
70
lib/types/mdadm.nix
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "mdadm" ];
|
||||
default = "mdadm";
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
level = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
default = 1;
|
||||
description = "mdadm level";
|
||||
};
|
||||
metadata = lib.mkOption {
|
||||
type = lib.types.enum [ "1" "1.0" "1.1" "1.2" "default" "ddf" "imsm" ];
|
||||
default = "default";
|
||||
description = "Metadata";
|
||||
};
|
||||
content = diskoLib.deviceType;
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = diskoLib.jsonType;
|
||||
default =
|
||||
lib.optionalAttrs (config.content != null) (config.content._meta [ "mdadm" config.name ]);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = _: ''
|
||||
readarray -t disk_devices < <(cat "$disko_devices_dir"/raid_${config.name})
|
||||
echo 'y' | mdadm --create /dev/md/${config.name} \
|
||||
--level=${toString config.level} \
|
||||
--raid-devices="$(wc -l "$disko_devices_dir"/raid_${config.name} | cut -f 1 -d " ")" \
|
||||
--metadata=${config.metadata} \
|
||||
--force \
|
||||
--homehost=any \
|
||||
"''${disk_devices[@]}"
|
||||
udevadm trigger --subsystem-match=block; udevadm settle
|
||||
${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/md/${config.name}";})}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = _:
|
||||
lib.optionalAttrs (config.content != null) (config.content._mount { dev = "/dev/md/${config.name}"; });
|
||||
# TODO we probably need to assemble the mdadm somehow
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default =
|
||||
lib.optional (config.content != null) (config.content._config "/dev/md/${config.name}");
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: (lib.optionals (config.content != null) (config.content._pkgs pkgs));
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
48
lib/types/mdraid.nix
Normal file
48
lib/types/mdraid.nix
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "mdraid" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Name";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: {
|
||||
deviceDependencies.mdadm.${config.name} = [ dev ];
|
||||
};
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
echo "${dev}" >> "$disko_devices_dir"/raid_${config.name}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }:
|
||||
{ };
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: [ ];
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.mdadm ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
72
lib/types/nodev.nix
Normal file
72
lib/types/nodev.nix
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
{ lib, config, options, diskoLib, rootMountPoint, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "nodev" ];
|
||||
default = "nodev";
|
||||
internal = true;
|
||||
description = "Device type";
|
||||
};
|
||||
fsType = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "File system type";
|
||||
};
|
||||
device = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "none";
|
||||
description = "Device to use";
|
||||
};
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = config._module.args.name;
|
||||
description = "Location to mount the file system at";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
description = "Options to pass to mount";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = diskoLib.jsonType;
|
||||
default = { };
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = _: "";
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = _: lib.optionalAttrs (config.mountpoint != null) {
|
||||
fs.${config.mountpoint} = ''
|
||||
if ! findmnt ${config.fsType} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
|
||||
mount -t ${config.fsType} ${config.device} "${rootMountPoint}${config.mountpoint}" \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
||||
-o X-mount.mkdir
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = lib.optional (config.mountpoint != null) {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = config.device;
|
||||
fsType = config.fsType;
|
||||
options = config.mountOptions;
|
||||
};
|
||||
};
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
56
lib/types/swap.nix
Normal file
56
lib/types/swap.nix
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
{ diskoLib, config, options, lib, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "swap" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
randomEncryption = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Whether to randomly encrypt the swap";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: { };
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
mkswap ${dev}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }: {
|
||||
fs.${dev} = ''
|
||||
if ! swapon --show | grep -q '^${dev} '; then
|
||||
swapon ${dev}
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: [{
|
||||
swapDevices = [{
|
||||
device = dev;
|
||||
randomEncryption = config.randomEncryption;
|
||||
}];
|
||||
}];
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.gnugrep pkgs.util-linux ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
139
lib/types/table.nix
Normal file
139
lib/types/table.nix
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
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 {
|
||||
type = lib.types.listOf (lib.types.submodule ({ config, ... }: {
|
||||
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";
|
||||
};
|
||||
index = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
# TODO find a better way to get the index
|
||||
default = lib.toInt (lib.head (builtins.match ".*entry ([[:digit:]]+)]" config._module.args.name));
|
||||
description = "Index 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;
|
||||
};
|
||||
}));
|
||||
default = [ ];
|
||||
description = "List of partitions to add to the partition table";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev:
|
||||
lib.foldr lib.recursiveUpdate { } (lib.imap
|
||||
(index: partition:
|
||||
lib.optionalAttrs (partition.content != null) (partition.content._meta dev)
|
||||
)
|
||||
config.partitions);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
parted -s ${dev} -- mklabel ${config.format}
|
||||
${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)}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }:
|
||||
let
|
||||
partMounts = lib.foldr lib.recursiveUpdate { } (lib.imap
|
||||
(index: partition:
|
||||
lib.optionalAttrs (partition.content != null) (partition.content._mount { dev = diskoLib.deviceNumbering dev index; })
|
||||
)
|
||||
config.partitions);
|
||||
in
|
||||
{
|
||||
dev = partMounts.dev or "";
|
||||
fs = partMounts.fs or { };
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev:
|
||||
lib.imap
|
||||
(index: partition:
|
||||
lib.optional (partition.content != null) (partition.content._config (diskoLib.deviceNumbering dev index))
|
||||
)
|
||||
config.partitions;
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs:
|
||||
[ pkgs.parted pkgs.systemdMinimal ] ++ lib.flatten (map
|
||||
(partition:
|
||||
lib.optional (partition.content != null) (partition.content._pkgs pkgs)
|
||||
)
|
||||
config.partitions);
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
47
lib/types/zfs.nix
Normal file
47
lib/types/zfs.nix
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
{ config, options, lib, diskoLib, ... }:
|
||||
{
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "zfs" ];
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
pool = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Name of the ZFS pool";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: {
|
||||
deviceDependencies.zpool.${config.pool} = [ dev ];
|
||||
};
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { dev }: ''
|
||||
echo "${dev}" >> "$disko_devices_dir"/zfs_${config.pool}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { dev }:
|
||||
{ };
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = dev: [ ];
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.zfs ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
86
lib/types/zfs_fs.nix
Normal file
86
lib/types/zfs_fs.nix
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
{ config, options, lib, diskoLib, rootMountPoint, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name of the dataset";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "zfs_fs" ];
|
||||
default = "zfs_fs";
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
options = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
description = "Options to set for the dataset";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
description = "Mount options";
|
||||
};
|
||||
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "Path to mount the dataset to";
|
||||
};
|
||||
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev: { };
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
# -u prevents mounting newly created datasets, which is
|
||||
# important to prevent accidental shadowing of mount points
|
||||
# since (create order != mount order)
|
||||
default = { zpool }: ''
|
||||
zfs create -u ${zpool}/${config.name} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { zpool }:
|
||||
lib.optionalAttrs (config.options.mountpoint or "" != "none") {
|
||||
fs.${config.mountpoint} = ''
|
||||
if ! findmnt ${zpool}/${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
|
||||
mount ${zpool}/${config.name} "${rootMountPoint}${config.mountpoint}" \
|
||||
-o X-mount.mkdir \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
||||
${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \
|
||||
-t zfs
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = zpool:
|
||||
lib.optional (config.options.mountpoint or "" != "none") {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = "${zpool}/${config.name}";
|
||||
fsType = "zfs";
|
||||
options = config.mountOptions ++ lib.optional ((config.options.mountpoint or "") != "legacy") "zfsutil";
|
||||
};
|
||||
};
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.util-linux ];
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
73
lib/types/zfs_volume.nix
Normal file
73
lib/types/zfs_volume.nix
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
{ config, options, lib, diskoLib, rootMountPoint, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name of the dataset";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "zfs_volume" ];
|
||||
default = "zfs_volume";
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
options = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
description = "Options to set for the dataset";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
description = "Mount options";
|
||||
};
|
||||
|
||||
# volume options
|
||||
size = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str; # TODO size
|
||||
default = null;
|
||||
description = "Size of the dataset";
|
||||
};
|
||||
|
||||
content = diskoLib.partitionType;
|
||||
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo diskoLib.jsonType;
|
||||
default = dev:
|
||||
lib.optionalAttrs (config.content != null) (config.content._meta dev);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = { zpool }: ''
|
||||
zfs create ${zpool}/${config.name} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} -V ${config.size}
|
||||
udevadm trigger --subsystem-match=block; udevadm settle
|
||||
${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/zvol/${zpool}/${config.name}";})}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = { zpool }:
|
||||
lib.optionalAttrs (config.content != null) (config.content._mount { dev = "/dev/zvol/${zpool}/${config.name}"; });
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = zpool:
|
||||
lib.optional (config.content != null) (config.content._config "/dev/zvol/${zpool}/${config.name}");
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.util-linux ] ++ lib.optionals (config.content != null) (config.content._pkgs pkgs);
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
123
lib/types/zpool.nix
Normal file
123
lib/types/zpool.nix
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
{ config, options, lib, diskoLib, rootMountPoint, ... }:
|
||||
{
|
||||
options = {
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config._module.args.name;
|
||||
description = "Name of the ZFS pool";
|
||||
};
|
||||
type = lib.mkOption {
|
||||
type = lib.types.enum [ "zpool" ];
|
||||
default = "zpool";
|
||||
internal = true;
|
||||
description = "Type";
|
||||
};
|
||||
mode = lib.mkOption {
|
||||
type = lib.types.str; # TODO zfs modes
|
||||
default = "";
|
||||
description = "Mode of the ZFS pool";
|
||||
};
|
||||
options = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
description = "Options for the ZFS pool";
|
||||
};
|
||||
rootFsOptions = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
description = "Options for the root filesystem";
|
||||
};
|
||||
mountpoint = lib.mkOption {
|
||||
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
||||
default = null;
|
||||
description = "The mountpoint of the pool";
|
||||
};
|
||||
mountOptions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ "defaults" ];
|
||||
description = "Options to pass to mount";
|
||||
};
|
||||
mountRoot = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "/";
|
||||
example = "/mnt";
|
||||
description = ''
|
||||
The root location where the zpool should be mounted.
|
||||
|
||||
Note:
|
||||
Leaving this at the default "/" might break your live system.
|
||||
'';
|
||||
};
|
||||
datasets = lib.mkOption {
|
||||
type = lib.types.attrsOf (diskoLib.subType { inherit (diskoLib.types) zfs_fs zfs_volume; });
|
||||
description = "List of datasets to define";
|
||||
};
|
||||
_meta = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = diskoLib.jsonType;
|
||||
default =
|
||||
diskoLib.deepMergeMap (dataset: dataset._meta [ "zpool" config.name ]) (lib.attrValues config.datasets);
|
||||
description = "Metadata";
|
||||
};
|
||||
_create = diskoLib.mkCreateOption {
|
||||
inherit config options;
|
||||
default = _: ''
|
||||
readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name})
|
||||
zpool create ${config.name} \
|
||||
-R ${config.mountRoot} ${config.mode} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
|
||||
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
|
||||
"''${zfs_devices[@]}"
|
||||
${lib.concatMapStrings (dataset: dataset._create {zpool = config.name;}) (lib.attrValues config.datasets)}
|
||||
'';
|
||||
};
|
||||
_mount = diskoLib.mkMountOption {
|
||||
inherit config options;
|
||||
default = _:
|
||||
let
|
||||
datasetMounts = diskoLib.deepMergeMap (dataset: dataset._mount { zpool = config.name; }) (lib.attrValues config.datasets);
|
||||
in
|
||||
{
|
||||
dev = ''
|
||||
zpool list '${config.name}' >/dev/null 2>/dev/null || \
|
||||
zpool import -l -R ${config.mountRoot} '${config.name}'
|
||||
${lib.concatMapStrings (x: x.dev or "") (lib.attrValues datasetMounts)}
|
||||
'';
|
||||
fs = (datasetMounts.fs or { }) // lib.optionalAttrs (config.mountpoint != null) {
|
||||
${config.mountpoint} = ''
|
||||
if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
|
||||
mount ${config.name} "${rootMountPoint}${config.mountpoint}" \
|
||||
${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \
|
||||
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
|
||||
-o X-mount.mkdir \
|
||||
-t zfs
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
_config = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = [
|
||||
(map (dataset: dataset._config config.name) (lib.attrValues config.datasets))
|
||||
(lib.optional (config.mountpoint != null) {
|
||||
fileSystems.${config.mountpoint} = {
|
||||
device = config.name;
|
||||
fsType = "zfs";
|
||||
options = config.mountOptions ++ lib.optional ((config.options.mountpoint or "") != "legacy") "zfsutil";
|
||||
};
|
||||
})
|
||||
];
|
||||
description = "NixOS configuration";
|
||||
};
|
||||
_pkgs = lib.mkOption {
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
||||
default = pkgs: [ pkgs.util-linux ] ++ lib.flatten (map (dataset: dataset._pkgs pkgs) (lib.attrValues config.datasets));
|
||||
description = "Packages";
|
||||
};
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue