Skip to content

Commit

Permalink
add bcachefs type with support for encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
onny committed May 20, 2023
1 parent efb2016 commit 3acf796
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
63 changes: 63 additions & 0 deletions types/bcachefs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{ config, options, lib, diskoLib, optionTypes, ... }:
{
options = {
type = lib.mkOption {
type = lib.types.enum [ "bcachefs" ];
internal = true;
description = "Type";
};
name = lib.mkOption {
type = lib.types.str;
description = "Name of the Bcachefs partition";
};
keyFile = lib.mkOption {
type = lib.types.nullOr optionTypes.absolute-pathname;
default = null;
description = "Path to the key for encryption";
example = "/tmp/disk.key";
};
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 }: ''
mkfs.bcachefs ${dev}
if ${config.keyFile}; then
bcachefs set-passphrase ${dev} < ${config.keyFile}
bcachefs unlock ${dev} < ${config.keyFile}
fi
'';
};
_mount = diskoLib.mkMountOption {
inherit config options;
default = { 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 ""}
'';
};
_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";
};
};
}
3 changes: 2 additions & 1 deletion types/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rec {

# option for valid contents of devices
deviceType = lib.mkOption {
type = lib.types.nullOr (diskoLib.subType { inherit (subTypes) table btrfs filesystem zfs mdraid luks lvm_pv swap; });
type = lib.types.nullOr (diskoLib.subType { inherit (subTypes) table bcachefs btrfs filesystem zfs mdraid luks lvm_pv swap; });
default = null;
description = "The type of device";
};
Expand Down Expand Up @@ -340,6 +340,7 @@ rec {

subTypes = lib.mapAttrs (_: diskoLib.mkSubType) {
nodev = ./nodev.nix;
bcachefs = ./bcachefs.nix;
btrfs = ./btrfs.nix;
btrfs_subvol = ./btrfs_subvol.nix;
filesystem = ./filesystem.nix;
Expand Down
2 changes: 1 addition & 1 deletion types/table.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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" ]);
type = lib.types.nullOr (lib.types.enum [ "bcachefs" "btrfs" "ext2" "ext3" "ext4" "fat16" "fat32" "hfs" "hfs+" "linux-swap" "ntfs" "reiserfs" "udf" "xfs" ]);
default = null;
description = "Filesystem type to use";
};
Expand Down

0 comments on commit 3acf796

Please sign in to comment.