Skip to content

Commit

Permalink
WIP bcachefs
Browse files Browse the repository at this point in the history
  • Loading branch information
jopejoe1 committed Jun 5, 2024
1 parent 7fc7009 commit 1f79052
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/types/bcachefs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{ config, options, lib, diskoLib, parent, device, ... }:
{
options = {
type = lib.mkOption {
type = lib.types.enum [ "bcachefs" ];
internal = true;
description = "Type";
};
device = lib.mkOption {
type = lib.types.str;
description = "Device";
default = device;
};

name = lib.mkOption {
type = lib.types.str;
description = "Name";
};

label = lib.mkOption {
type = lib.types.str;
description = "Label";
};

formatOptions = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ "defaults" ];
description = "Format options";
};

_parent = lib.mkOption {
internal = true;
default = parent;
};
_meta = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo diskoLib.jsonType;
default = dev: {
deviceDependencies.bcachefspool.${config.name} = [ dev ];
};
description = "Metadata";
};
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
echo ${config.device} >>"$disko_devices_dir"/bcachefs_${config.name}/devices
echo ${config.label} >>"$disko_devices_dir"/bcachefs_${config.name}/labels
echo ${lib.concatStringsSep " " config.formatOptions} >>"$disko_devices_dir"/bcachefs_${config.name}/format_options
'';
};
_mount = diskoLib.mkMountOption {
inherit config options;
default = { };
};
_config = lib.mkOption {
internal = true;
readOnly = true;
default = [ ];
description = "NixOS configuration";
};
_pkgs = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.bcachefs-tools ];
description = "Packages";
};
};
}
90 changes: 90 additions & 0 deletions lib/types/bcachefspool.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{ 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 [ "bcachefspool" ];
default = "bcachefspool";
internal = true;
description = "Type";
};

formatOptions = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ "defaults" ];
description = "Format options";
};

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 = "A path to mount the bcachefs filesystem to.";
};

_meta = lib.mkOption {
internal = true;
readOnly = true;
type = diskoLib.jsonType;
default =
lib.optionalAttrs (config.content != null) (config.content._meta [ "bcachefspool" config.name ]);
description = "Metadata";
};

_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
readarray -t bcachefs_devices < <(cat "$disko_devices_dir"/bcachefs_${config.name}/devices)
readarray -t bcachefs_labels < <(cat "$disko_devices_dir"/bcachefs_${config.name}/labels)
readarray -t bcachefs_format_options < <(cat "$disko_devices_dir"/bcachefs_${config.name}/format_options)
device_configs=()
for ((i=0; i<''${#bcachefs_devices[@]}; i++)); do
device=''${bcachefs_devices[$i]}
label=''${bcachefs_labels[$i]}
format_options=''${bcachefs_format_options[$i]}
device_configs+=("--label=$label $format_options $device")
done
bcachefs format --fs_label=${config.name} ${lib.concatStringsSep " " config.formatOptions} \
$(IFS=' \' ; echo "''${device_configs[*]}")
'';
};

_mount = diskoLib.mkMountOption {
inherit config options;
default = "TODO";
};

_config = lib.mkOption {
internal = true;
readOnly = true;
default = lib.optional (config.options.mountpoint or "" != "none") {
fileSystems.${config.mountpoint} = {
device = "${lib.concatStringsSep ":" config.formatOptions}";
fsType = "bcachefs";
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";
};
};
}

0 comments on commit 1f79052

Please sign in to comment.