feat: made impermanence create datasets for zfs and persistence

This commit is contained in:
Leyla Becker 2025-11-07 16:29:56 -06:00
parent 409fdb7276
commit adc6b90c93
4 changed files with 188 additions and 43 deletions

View file

@ -1,33 +1,90 @@
args @ {lib, ...}: let
impermanenceDatasetSubmodules = (import ./submodules/impermanenceDataset.nix) args;
args @ {
lib,
config,
...
}: let
datasetSubmodules = (import ./submodules/dataset.nix) args;
impermanenceDatasetSubmodule = (import ./submodules/impermanenceDataset.nix) args;
permissionsToMode = permissions: let
permSetToDigit = permSet:
(
if permSet.read
then 4
else 0
)
+ (
if permSet.write
then 2
else 0
)
+ (
if permSet.execute
then 1
else 0
);
ownerDigit = permSetToDigit permissions.owner.permissions;
groupDigit = permSetToDigit permissions.group.permissions;
otherDigit = permSetToDigit permissions.other.permissions;
in
toString ownerDigit + toString groupDigit + toString otherDigit;
# Get the option names from both submodules to automatically determine which are impermanence-specific
regularDatasetEval = lib.evalModules {
modules = [datasetSubmodules];
specialArgs = args;
};
impermanenceDatasetEval = lib.evalModules {
modules = [impermanenceDatasetSubmodule];
specialArgs = args;
};
regularDatasetOptions = builtins.attrNames regularDatasetEval.options;
impermanenceDatasetOptions = builtins.attrNames impermanenceDatasetEval.options;
# Find options that are only in impermanence datasets (not in regular ZFS datasets)
impermanenceOnlyOptions = lib.lists.subtractLists regularDatasetOptions impermanenceDatasetOptions;
in {
options.storage = {
impermanence = {
enable = lib.mkEnableOption "should impermanence be enabled for this system";
# TODO: enable option implementation
# TODO: assertion that zfs needs to be enabled when impermanence is enabled
# TODO: datasets option that is a submodule that will be used to define what datasets to add to the storage system
# We should by default create the `local`, `local/system/nix`, `local/system/root`, `persist` `persist/system/root`, and `persist/system/var/log` datasets
# We should also create datasets for systemd modules that have have impermanence enabled for them
datasets = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule impermanenceDatasetSubmodules);
type = lib.types.attrsOf (lib.types.submodule impermanenceDatasetSubmodule);
default = {};
};
# TODO: this should just live under home-manager.users.<user>.storage.impermanence
home-manager = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule ({name, ...}: {
enable = lib.mkEnableOption "should impermanence be enabled for this user";
# We should by default create the `local/home/${name}`, and `persist/home/${name}` datasets
datasets = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule impermanenceDatasetSubmodules);
default = {};
};
}));
};
};
};
config = lib.mkIf config.storage.impermanence.enable (lib.mkMerge [
{
environment.persistence =
lib.mapAttrs (datasetName: dataset: {
enable = true;
hideMounts = true;
directories = lib.mapAttrsToList (path: dirConfig: {
directory = path;
user = dirConfig.owner.name;
group = dirConfig.group.name;
mode = permissionsToMode dirConfig;
}) (lib.filterAttrs (_: dirConfig: dirConfig.enable) dataset.directories);
files = lib.mapAttrsToList (path: fileConfig: {
file = path;
user = fileConfig.owner.name;
group = fileConfig.group.name;
mode = permissionsToMode fileConfig;
}) (lib.filterAttrs (_: fileConfig: fileConfig.enable) dataset.files);
})
config.storage.impermanence.datasets;
}
(lib.mkIf config.storage.zfs.enable {
storage.zfs.datasets =
lib.mapAttrs (
datasetName: dataset:
builtins.removeAttrs dataset impermanenceOnlyOptions
)
config.storage.impermanence.datasets;
})
]);
}