disko/lib/types/zpool.nix

125 lines
4.3 KiB
Nix
Raw Normal View History

2023-05-16 14:40:03 +03:00
{ config, options, lib, diskoLib, rootMountPoint, ... }:
2023-01-28 18:19:13 +03:00
{
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 {
2023-09-05 00:02:33 +03:00
type = lib.types.enum [
""
"mirror"
"raidz"
"raidz2"
"raidz3"
];
2023-01-28 18:19:13 +03:00
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 {
2023-05-16 14:40:03 +03:00
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
2023-01-28 18:19:13 +03:00
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";
};
datasets = lib.mkOption {
type = lib.types.attrsOf (diskoLib.subType {
types = { inherit (diskoLib.types) zfs_fs zfs_volume; };
extraArgs.parent = config;
});
2023-01-28 18:19:13 +03:00
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 = ''
2023-02-14 10:35:12 +03:00
readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name})
2023-06-14 23:19:02 +03:00
zpool create -f ${config.name} \
2023-05-19 02:55:12 +03:00
-R ${rootMountPoint} ${config.mode} \
2023-01-28 18:19:13 +03:00
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-O ${n}=${v}") config.rootFsOptions)} \
2023-02-14 10:35:12 +03:00
"''${zfs_devices[@]}"
if [[ $(zfs get -H mounted ${config.name} | cut -f3) == "yes" ]]; then
zfs unmount ${config.name}
fi
${lib.concatMapStrings (dataset: dataset._create) (lib.attrValues config.datasets)}
2023-01-28 18:19:13 +03:00
'';
};
_mount = diskoLib.mkMountOption {
inherit config options;
default =
2023-01-28 18:19:13 +03:00
let
datasetMounts = diskoLib.deepMergeMap (dataset: dataset._mount) (lib.attrValues config.datasets);
2023-01-28 18:19:13 +03:00
in
{
dev = ''
zpool list '${config.name}' >/dev/null 2>/dev/null ||
2023-05-19 02:55:12 +03:00
zpool import -l -R ${rootMountPoint} '${config.name}'
2023-01-28 18:19:13 +03:00
${lib.concatMapStrings (x: x.dev or "") (lib.attrValues datasetMounts)}
'';
2023-05-12 18:20:14 +03:00
fs = (datasetMounts.fs or { }) // lib.optionalAttrs (config.mountpoint != null) {
2023-01-28 18:19:13 +03:00
${config.mountpoint} = ''
if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then
2023-01-28 18:19:13 +03:00
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
2023-01-28 18:19:13 +03:00
fi
'';
};
};
};
_config = lib.mkOption {
internal = true;
readOnly = true;
default = [
(map (dataset: dataset._config) (lib.attrValues config.datasets))
(lib.optional (config.mountpoint != null) {
2023-01-28 18:19:13 +03:00
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";
};
};
}