disko/types/btrfs_subvol.nix

95 lines
2.9 KiB
Nix
Raw Normal View History

2023-01-28 16:19:13 +01:00
{ config, options, diskoLib, lib, optionTypes, 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 {
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 = "Options to pass to mount";
};
mountpoint = lib.mkOption {
type = lib.types.nullOr 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
2023-02-14 09:58:37 +01:00
btrfs subvolume create "$MNTPOINT"/${config.name} ${toString config.extraArgs}
2023-01-28 16:19:13 +01:00
)
'';
};
_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
2023-01-28 16:19:13 +01:00
else null;
in
lib.optionalAttrs (mountpoint != null) {
2023-01-28 16:19:13 +01:00
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
2023-01-28 16:19:13 +01:00
else null;
in
lib.optional (mountpoint != null) {
2023-01-28 16:19:13 +01:00
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";
};
};
}