Skip to content

Commit 7565991

Browse files
committed
Add bcachefs type with support for encryption
1 parent 241c878 commit 7565991

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ You can keep your configuration and re-use it for other installations, or for a
2222

2323
- Disk layouts: GPT, MBR, and mixed.
2424
- Partition tools: LVM, mdadm, LUKS, and more.
25-
- Filesystems: ext4, btrfs, ZFS, bcachefs, tmpfs, and others.
25+
- Filesystems: ext4, bcachefs, btrfs, ZFS, bcachefs, tmpfs, and others.
2626

2727
It can work with these in various configurations and orders, and supports recursive layouts.
2828

example/bcachefs.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
name = "root";
2121
end = "-0";
2222
content = {
23-
type = "filesystem";
24-
format = "bcachefs";
23+
type = "bcachefs";
24+
keyFile = "/tmp/secret.key";
2525
mountpoint = "/";
2626
};
2727
};

lib/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let
3535
# option for valid contents of partitions (basically like devices, but without tables)
3636
partitionType = extraArgs: lib.mkOption {
3737
type = lib.types.nullOr (diskoLib.subType {
38-
types = { inherit (diskoLib.types) btrfs filesystem zfs mdraid luks lvm_pv swap; };
38+
types = { inherit (diskoLib.types) bcachefs btrfs filesystem zfs mdraid luks lvm_pv swap; };
3939
inherit extraArgs;
4040
});
4141
default = null;
@@ -45,7 +45,7 @@ let
4545
# option for valid contents of devices
4646
deviceType = extraArgs: lib.mkOption {
4747
type = lib.types.nullOr (diskoLib.subType {
48-
types = { inherit (diskoLib.types) table gpt btrfs filesystem zfs mdraid luks lvm_pv swap; };
48+
types = { inherit (diskoLib.types) table gpt bcachefs btrfs filesystem zfs mdraid luks lvm_pv swap; };
4949
inherit extraArgs;
5050
});
5151
default = null;

lib/types/bcachefs.nix

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{ config, options, diskoLib, lib, rootMountPoint, parent, device, ... }:
2+
{
3+
options = {
4+
type = lib.mkOption {
5+
type = lib.types.enum [ "bcachefs" ];
6+
internal = true;
7+
description = "Type";
8+
};
9+
device = lib.mkOption {
10+
type = lib.types.str;
11+
default = device;
12+
description = "Device to use";
13+
};
14+
extraArgs = lib.mkOption {
15+
type = lib.types.listOf lib.types.str;
16+
default = [ ];
17+
description = "Extra arguments";
18+
};
19+
keyFile = lib.mkOption {
20+
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
21+
default = null;
22+
description = "Path to the key for encryption";
23+
example = "/tmp/disk.key";
24+
};
25+
mountOptions = lib.mkOption {
26+
type = lib.types.listOf lib.types.str;
27+
default = [ "defaults" ];
28+
description = "A list of options to pass to mount.";
29+
};
30+
mountpoint = lib.mkOption {
31+
type = lib.types.nullOr diskoLib.optionTypes.absolute-pathname;
32+
default = null;
33+
description = "A path to mount the Bcachefs filesystem to.";
34+
};
35+
_parent = lib.mkOption {
36+
internal = true;
37+
default = parent;
38+
};
39+
_meta = lib.mkOption {
40+
internal = true;
41+
readOnly = true;
42+
type = lib.types.functionTo diskoLib.jsonType;
43+
default = dev: { };
44+
description = "Metadata";
45+
};
46+
_create = diskoLib.mkCreateOption {
47+
inherit config options;
48+
default = ''
49+
bcachefs format ${config.device} ${toString config.extraArgs}
50+
'';
51+
};
52+
_mount = diskoLib.mkMountOption {
53+
inherit config options;
54+
default = {
55+
fs = lib.optionalAttrs (config.mountpoint != null) {
56+
${config.mountpoint} = ''
57+
if ! findmnt ${config.device} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
58+
mount -t bcachefs ${config.device} "${rootMountPoint}${config.mountpoint}" \
59+
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \
60+
-o X-mount.mkdir
61+
fi
62+
'';
63+
};
64+
};
65+
};
66+
_config = lib.mkOption {
67+
internal = true;
68+
readOnly = true;
69+
default = [
70+
(lib.optional (config.mountpoint != null) {
71+
fileSystems.${config.mountpoint} = {
72+
device = config.device;
73+
fsType = "bcachefs";
74+
options = config.mountOptions;
75+
};
76+
})
77+
];
78+
description = "NixOS configuration";
79+
};
80+
_pkgs = lib.mkOption {
81+
internal = true;
82+
readOnly = true;
83+
type = lib.types.functionTo (lib.types.listOf lib.types.package);
84+
default = pkgs:
85+
[ pkgs.bcachefs-tools pkgs.coreutils ];
86+
description = "Packages";
87+
};
88+
};
89+
}

lib/types/table.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
description = "Partition type";
2626
};
2727
fs-type = lib.mkOption {
28-
type = lib.types.nullOr (lib.types.enum [ "btrfs" "ext2" "ext3" "ext4" "fat16" "fat32" "hfs" "hfs+" "linux-swap" "ntfs" "reiserfs" "udf" "xfs" ]);
28+
type = lib.types.nullOr (lib.types.enum [ "bcachefs" "btrfs" "ext2" "ext3" "ext4" "fat16" "fat32" "hfs" "hfs+" "linux-swap" "ntfs" "reiserfs" "udf" "xfs" ]);
2929
default = null;
3030
description = "Filesystem type to use";
3131
};

0 commit comments

Comments
 (0)