2023-05-16 13:40:03 +02:00
|
|
|
{ config, options, diskoLib, lib, rootMountPoint, ... }:
|
2023-01-28 16:19:13 +01:00
|
|
|
{
|
|
|
|
|
options = {
|
|
|
|
|
type = lib.mkOption {
|
|
|
|
|
type = lib.types.enum [ "btrfs" ];
|
|
|
|
|
internal = true;
|
|
|
|
|
description = "Type";
|
|
|
|
|
};
|
|
|
|
|
extraArgs = lib.mkOption {
|
2023-02-14 09:58:37 +01:00
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
|
default = [ ];
|
|
|
|
|
description = "Extra arguments";
|
2023-01-28 16:19:13 +01:00
|
|
|
};
|
|
|
|
|
mountOptions = lib.mkOption {
|
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
|
default = [ "defaults" ];
|
|
|
|
|
description = "A list of options to pass to mount.";
|
|
|
|
|
};
|
|
|
|
|
subvolumes = lib.mkOption {
|
2023-05-30 14:57:14 +02:00
|
|
|
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 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.";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}));
|
2023-01-28 16:19:13 +01:00
|
|
|
default = { };
|
|
|
|
|
description = "Subvolumes to define for BTRFS.";
|
|
|
|
|
};
|
|
|
|
|
mountpoint = lib.mkOption {
|
2023-05-16 13:40:03 +02:00
|
|
|
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
|
2023-01-28 16:19:13 +01:00
|
|
|
default = null;
|
|
|
|
|
description = "A path to mount the BTRFS filesystem to.";
|
|
|
|
|
};
|
|
|
|
|
_meta = lib.mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
readOnly = true;
|
|
|
|
|
type = lib.types.functionTo diskoLib.jsonType;
|
2023-05-30 14:57:14 +02:00
|
|
|
default = dev: { };
|
2023-01-28 16:19:13 +01:00
|
|
|
description = "Metadata";
|
|
|
|
|
};
|
|
|
|
|
_create = diskoLib.mkCreateOption {
|
|
|
|
|
inherit config options;
|
|
|
|
|
default = { dev }: ''
|
2023-02-14 09:58:37 +01:00
|
|
|
mkfs.btrfs ${dev} ${toString config.extraArgs}
|
2023-05-30 14:57:14 +02:00
|
|
|
${lib.concatMapStrings (subvol: ''
|
|
|
|
|
MNTPOINT=$(mktemp -d)
|
|
|
|
|
(
|
|
|
|
|
mount ${dev} "$MNTPOINT" -o subvol=/
|
|
|
|
|
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
|
|
|
|
|
btrfs subvolume create "$MNTPOINT"/${subvol.name} ${toString subvol.extraArgs}
|
|
|
|
|
)
|
|
|
|
|
'') (lib.attrValues config.subvolumes)}
|
2023-01-28 16:19:13 +01:00
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
_mount = diskoLib.mkMountOption {
|
|
|
|
|
inherit config options;
|
|
|
|
|
default = { dev }:
|
|
|
|
|
let
|
2023-05-30 14:57:14 +02:00
|
|
|
subvolMounts = lib.concatMapAttrs
|
|
|
|
|
(_: subvol:
|
|
|
|
|
let
|
|
|
|
|
mountpoint =
|
|
|
|
|
if (subvol.mountpoint != null) then subvol.mountpoint
|
|
|
|
|
else if (config.mountpoint == null) then subvol.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}") (subvol.mountOptions ++ [ "subvol=${subvol.name}" ])} \
|
|
|
|
|
-o X-mount.mkdir
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
config.subvolumes;
|
2023-01-28 16:19:13 +01:00
|
|
|
in
|
|
|
|
|
{
|
2023-02-06 14:24:34 +00:00
|
|
|
fs = subvolMounts.fs // lib.optionalAttrs (config.mountpoint != null) {
|
2023-01-28 16:19:13 +01:00
|
|
|
${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: [
|
2023-05-30 14:57:14 +02:00
|
|
|
(map
|
|
|
|
|
(subvol:
|
|
|
|
|
let
|
|
|
|
|
mountpoint =
|
|
|
|
|
if (subvol.mountpoint != null) then subvol.mountpoint
|
|
|
|
|
else if (config.mountpoint == null) then subvol.name
|
|
|
|
|
else null;
|
|
|
|
|
in
|
|
|
|
|
lib.optional (mountpoint != null) {
|
|
|
|
|
fileSystems.${mountpoint} = {
|
|
|
|
|
device = dev;
|
|
|
|
|
fsType = "btrfs";
|
|
|
|
|
options = subvol.mountOptions ++ [ "subvol=${subvol.name}" ];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
(lib.attrValues config.subvolumes))
|
2023-02-06 14:24:34 +00:00
|
|
|
(lib.optional (config.mountpoint != null) {
|
2023-01-28 16:19:13 +01:00
|
|
|
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:
|
2023-05-30 14:57:14 +02:00
|
|
|
[ pkgs.btrfs-progs pkgs.coreutils ];
|
2023-01-28 16:19:13 +01:00
|
|
|
description = "Packages";
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-05-16 13:40:03 +02:00
|
|
|
}
|