Skip to content

Commit

Permalink
Add bcachefs module and mount point configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
yousiki committed Mar 20, 2024
1 parent ac18d43 commit 9f07b2d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cells/common/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ in {
transformer = defaultAsRoot;
};

importModules = {
src,
args ? {},
}:
haumea.lib.load {
inherit src;
inputs = args;
transformer = haumea.lib.transformers.liftDefault;
};

importProfiles = {
src,
args ? {},
Expand Down
2 changes: 2 additions & 0 deletions cells/nixos/hosts/hakase/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
inputs.nixos-hardware.nixosModules.common-pc-hdd
inputs.nixos-hardware.nixosModules.common-pc-ssd

inputs.cells.nixos.nixosModules.bcachefs

inputs.cells.nixos.nixosProfiles.core
inputs.cells.nixos.nixosProfiles.desktop
inputs.cells.nixos.nixosProfiles.nvidia
Expand Down
5 changes: 5 additions & 0 deletions cells/nixos/hosts/hakase/hardware-configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
{device = "/dev/disk/by-uuid/9dc3e2ac-b63d-4dda-8b4c-7be566aa349a";}
];

bcachefs.fileSystems."/mnt/data" = {
devices = ["/dev/nvme0n1p3" "/dev/sda1" "/dev/sdb1"];
options = ["noatime"];
};

# Enables DHCP on each ethernet and wireless interface. In case of scripted networking
# (the default) this is the recommended approach. When using systemd-networkd it's
# still possible to use this option, but it's recommended to use it in conjunction
Expand Down
86 changes: 86 additions & 0 deletions cells/nixos/modules/bcachefs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
inputs,
cell,
}: {
pkgs,
lib,
config,
...
}: let
l = builtins // lib;

bcachefsFileSystem = {
name,
config,
...
}: {
options = {
devices = l.mkOption {
type = l.types.listOf l.types.path;
description = lib.mdDoc "The devices to use for the filesystem.";
};

options = l.mkOption {
type = l.types.listOf l.types.str;
default = [];
description = lib.mdDoc "Additional options to pass to `mount`.";
};
};
};

pathToString = path: l.replaceStrings ["/"] ["-"] (l.toString path);

mkUnit = mountPoint: mountOptions: let
inherit (mountOptions) devices options;
deviceTargets = l.pipe devices [
(l.map pathToString)
(l.map (l.removePrefix "-"))
(l.map (device: "${device}.device"))
];
concatDevices = l.concatStringsSep ":" (l.map l.toString devices);
concatOptions = l.concatStringsSep "," options;
in {
description = "Mount bcachefs volume ${mountPoint}";
bindsTo = deviceTargets;
after = deviceTargets ++ ["local-fs-pre.target"];
before = ["umount.target" "local-fs.target"];
conflicts = ["umount.target"];
wantedBy = ["local-fs.target"];
unitConfig = {
RequiresMountsFor = mountPoint;
DefaultDependencies = false;
};
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.util-linux}/bin/mount -t bcachefs -o ${concatOptions} ${concatDevices} ${mountPoint}";
ExecStop = "${pkgs.util-linux}/umount ${mountPoint}";
};
};
in {
options = {
bcachefs.fileSystems = l.mkOption {
default = {};
example = l.literalExpression ''
{
"/data" = {
device = [ "/dev/sda1" "/dev/sdb1" ];
options = [ "noatime" ];
};
}
'';
type = l.types.attrsOf (l.types.submodule [bcachefsFileSystem]);
};
};

config = {
systemd = {
packages = with pkgs; [bcachefs-tools];
services = l.mapAttrs' (name: value:
l.nameValuePair
"mount-bcachefs-volume-${pathToString name}"
(mkUnit name value))
config.bcachefs.fileSystems;
};
};
}
10 changes: 10 additions & 0 deletions cells/nixos/nixosModules.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
inputs,
cell,
}: let
inherit (inputs.cells.common.lib) importModules;
in
importModules {
src = ./modules;
args = {inherit inputs cell;};
}
5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
hive.blockTypes.nixosConfigurations
];
}
{
nixosModules = hive.pick self [
["nixos" "nixosModules"]
];
}
{
nixosConfigurations = collect self "nixosConfigurations";
};
Expand Down

0 comments on commit 9f07b2d

Please sign in to comment.