2024-12-06 16:46:10 +00:00
|
|
|
{
|
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
namespace,
|
|
|
|
...
|
|
|
|
}:
|
2024-07-02 22:15:37 +00:00
|
|
|
let
|
2024-12-06 16:46:10 +00:00
|
|
|
cfg = config.${namespace}.filesystem;
|
2024-07-02 22:15:37 +00:00
|
|
|
|
2024-07-05 13:36:11 +00:00
|
|
|
# LUKS partition will decrypt to /dev/mapper/nixos-root
|
|
|
|
decryptPart = "nixos-root";
|
|
|
|
decryptPath = "/dev/mapper/${decryptPart}";
|
2024-10-10 18:30:07 +00:00
|
|
|
|
|
|
|
# Default mount options for your main partitions
|
|
|
|
primaryPartOpts = [
|
|
|
|
"compress=zstd"
|
|
|
|
(lib.mkIf cfg.discard "discard=async").content
|
|
|
|
];
|
2024-07-02 22:15:37 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2024-12-06 16:46:10 +00:00
|
|
|
${namespace}.filesystem = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables standard BTRFS subvolumes and parameters.";
|
2024-07-05 13:36:11 +00:00
|
|
|
partitions = {
|
|
|
|
boot = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The ID of your boot partition. Use /dev/disk/by-uuid for best results.";
|
|
|
|
default = "";
|
2024-07-02 22:15:37 +00:00
|
|
|
};
|
2024-07-05 13:36:11 +00:00
|
|
|
luks = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The ID of your LUKS partition. Use /dev/disk/by-uuid for best results.";
|
|
|
|
default = "";
|
2024-07-02 22:15:37 +00:00
|
|
|
};
|
|
|
|
};
|
2024-08-24 16:39:04 +00:00
|
|
|
discard = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = "Whether to enable TRIM for SSD and NVMe drives. Defaults to true.";
|
|
|
|
default = true;
|
|
|
|
};
|
2024-07-05 13:36:11 +00:00
|
|
|
swapFile = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables the creation of a swap file.";
|
2024-07-05 13:36:11 +00:00
|
|
|
size = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
description = "The size of the swap file to create in MB (defaults to 8192, or ~8 gigabytes).";
|
|
|
|
default = 8192;
|
2024-07-02 22:15:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-07-05 13:36:11 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2024-07-02 22:15:37 +00:00
|
|
|
|
|
|
|
# Check for blank parameters
|
|
|
|
assertions = [
|
|
|
|
{
|
2024-07-05 13:36:11 +00:00
|
|
|
assertion = cfg.partitions.luks != "";
|
|
|
|
message = "Please specify a LUKS partition to use as the root filesystem.";
|
2024-07-02 22:15:37 +00:00
|
|
|
}
|
|
|
|
{
|
2024-07-05 13:36:11 +00:00
|
|
|
assertion = cfg.partitions.boot != "";
|
|
|
|
message = "Please specify your boot partition.";
|
2024-07-02 22:15:37 +00:00
|
|
|
}
|
|
|
|
];
|
2024-07-05 13:36:11 +00:00
|
|
|
boot.initrd.luks.devices.${decryptPart} = {
|
|
|
|
device = cfg.partitions.luks;
|
|
|
|
# Enable TPM auto-unlocking if configured
|
2024-12-06 16:46:10 +00:00
|
|
|
crypttabExtraOpts = lib.mkIf config.${namespace}.bootloader.tpm2.enable [ "tpm2-device=auto" ];
|
2024-07-04 01:13:13 +00:00
|
|
|
};
|
2024-10-10 18:30:07 +00:00
|
|
|
fileSystems = {
|
|
|
|
"/" = {
|
|
|
|
device = decryptPath;
|
|
|
|
fsType = "btrfs";
|
|
|
|
options = [ "subvol=@" ] ++ primaryPartOpts;
|
|
|
|
};
|
|
|
|
"/boot" = {
|
|
|
|
device = cfg.partitions.boot;
|
|
|
|
fsType = "vfat";
|
|
|
|
};
|
|
|
|
"/home" = {
|
|
|
|
device = decryptPath;
|
|
|
|
fsType = "btrfs";
|
|
|
|
options = [ "subvol=@home" ] ++ primaryPartOpts;
|
|
|
|
};
|
|
|
|
"/var/log" = {
|
|
|
|
device = decryptPath;
|
|
|
|
fsType = "btrfs";
|
|
|
|
options = [ "subvol=@log" ] ++ primaryPartOpts;
|
2024-07-02 22:15:37 +00:00
|
|
|
};
|
2024-10-10 18:30:07 +00:00
|
|
|
"/nix" = {
|
|
|
|
device = decryptPath;
|
|
|
|
fsType = "btrfs";
|
|
|
|
options = [
|
|
|
|
"subvol=@nix"
|
|
|
|
"noatime"
|
|
|
|
] ++ primaryPartOpts;
|
|
|
|
};
|
|
|
|
"/swap" = lib.mkIf cfg.swapFile.enable {
|
|
|
|
device = decryptPath;
|
|
|
|
fsType = "btrfs";
|
|
|
|
options = [
|
|
|
|
"subvol=@swap"
|
|
|
|
"noatime"
|
|
|
|
(lib.mkIf cfg.discard "discard=async").content
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2024-07-02 22:15:37 +00:00
|
|
|
|
2024-07-05 13:36:11 +00:00
|
|
|
swapDevices = lib.mkIf cfg.swapFile.enable [
|
2024-07-02 22:15:37 +00:00
|
|
|
{
|
|
|
|
device = "/swap/swapfile";
|
2024-07-05 13:36:11 +00:00
|
|
|
size = cfg.swapFile.size;
|
2024-07-02 22:15:37 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|