nix-config/modules-hm/impermanence.nix

81 lines
2.5 KiB
Nix
Raw Permalink Normal View History

2024-07-26 22:12:48 +02:00
{ config, lib, ... }:
let
inherit (lib)
2023-09-05 17:50:55 +02:00
flip
mapAttrs
attrNames
mkOption
types
mkMerge
isAttrs
;
2024-07-26 22:12:48 +02:00
in
{
2023-09-05 17:50:55 +02:00
# Expose a home manager module for each user that allows extending
# environment.persistence.${sourceDir}.users.${userName} simply by
# specifying home.persistence.${sourceDir} in home manager.
home-manager.sharedModules = [
{
options.home.persistence = mkOption {
description = "Additional persistence config for the given source path";
2024-07-26 22:12:48 +02:00
default = { };
type = types.attrsOf (
types.submodule {
options = {
files = mkOption {
description = "Additional files to persist via NixOS impermanence.";
type = types.listOf (types.either types.attrs types.str);
default = [ ];
};
2023-09-05 17:50:55 +02:00
2024-07-26 22:12:48 +02:00
directories = mkOption {
description = "Additional directories to persist via NixOS impermanence.";
type = types.listOf (types.either types.attrs types.str);
default = [ ];
};
2023-09-05 17:50:55 +02:00
};
2024-07-26 22:12:48 +02:00
}
);
2023-09-05 17:50:55 +02:00
};
}
];
# For each user that has a home-manager config, merge the locally defined
# persistence options that we defined above.
2024-07-26 22:12:48 +02:00
imports =
let
mkUserFiles = map (
x: { parentDirectory.mode = "700"; } // (if isAttrs x then x else { file = x; })
2023-09-05 17:50:55 +02:00
);
2024-07-26 22:12:48 +02:00
mkUserDirs = map (x: { mode = "700"; } // (if isAttrs x then x else { directory = x; }));
in
[
{
environment.persistence = mkMerge (
flip map (attrNames config.home-manager.users) (
user:
let
hmUserCfg = config.home-manager.users.${user};
in
flip mapAttrs hmUserCfg.home.persistence (
_: sourceCfg: {
users.${user} = {
# This needs to be set for allo users with non
# standart home (not /home/<userName>
# due to nixpkgs it
# can't be deduced from homeDirectory
# as there will be infinite recursion
# If this setting is forgotten there
# are assertions in place warning you
home = { root = "/root"; }.${user} or "/home/${user}";
files = mkUserFiles sourceCfg.files;
directories = mkUserDirs sourceCfg.directories;
};
}
)
)
);
}
];
}