nixos-config/modules/environment/user-impermanence.nix

123 lines
4.2 KiB
Nix
Raw Normal View History

2024-11-02 13:24:01 +00:00
{
config,
lib,
2024-11-03 09:56:04 +00:00
pkgs,
2024-11-02 13:24:01 +00:00
...
}:
2024-11-09 14:02:26 +00:00
with lib;
{
2024-11-02 13:24:01 +00:00
options = {
environment.impermanence.users = mkOption {
description = "Which users to clean up the home directory for";
2024-11-09 14:02:26 +00:00
default = [ ];
2024-11-02 13:24:01 +00:00
type = types.listOf types.str;
};
};
2024-11-09 14:02:26 +00:00
config =
mkIf (config.environment.impermanence.enable && config.environment.impermanence.users != [ ])
{
programs.fuse.userAllowOther = true;
home-manager.users = listToAttrs (
map (name: {
inherit name;
value =
{ config, ... }:
2024-11-09 07:53:00 +00:00
{
2024-11-09 14:02:26 +00:00
home.file."${config.home.homeDirectory}/.cache/.keep" = {
enable = false;
};
home.persistence.default = {
persistentStoragePath = "/persistent/home/${name}";
allowOther = true;
2024-11-17 08:37:40 +00:00
defaultMethod = "symlink";
2024-11-09 14:02:26 +00:00
directories = [
"Downloads"
"Music"
"Pictures"
"Documents"
"Videos"
2024-11-17 08:37:40 +00:00
".cache"
2024-11-09 14:02:26 +00:00
"Data"
];
};
};
}) config.environment.impermanence.users
);
systemd.tmpfiles.rules = mkMerge (
map (name: [
"d /persistent/home/${name} 700 ${name} ${config.users.users.${name}.group} - -"
"d /persistent/home/${name}/.cache 700 ${name} ${config.users.users.${name}.group} 7d -"
]) config.environment.impermanence.users
);
systemd.services = listToAttrs (
flatten (
map (
name:
let
cfg = config.users.users.${name};
in
[
{
name = "cleanup-home-${name}";
description = "Clean home directory for ${name}";
value = {
before = [
"user@${toString cfg.uid}.service"
"home-manager-${name}.service"
];
partOf = [
"user@${toString cfg.uid}.service"
];
serviceConfig.Type = "oneshot";
script = ''
if [[ -e ${cfg.home} ]]; then
timestamp=$(date --date="@$(stat -c %X ${cfg.home})" "+%Y-%m-%d_%H:%M:%S")
mkdir -p /persistent/old-homedirs/${name}
mv ${cfg.home} /persistent/old-homedirs/${name}/$timestamp
fi
2024-11-02 13:24:01 +00:00
2024-11-09 14:02:26 +00:00
delete_subvolume_recursively() {
IFS=$'\n'
for i in $(${pkgs.btrfs-progs}/bin/btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do
delete_subvolume_recursively "/persistent/old-homedirs/${name}/$i"
done
${pkgs.btrfs-progs}/bin/btrfs subvolume delete "$1" || rm -rf "$1"
}
2024-11-02 13:24:01 +00:00
2024-11-09 14:02:26 +00:00
for i in $(find /persistent/old-homedirs/${name} -maxdepth 1 -atime +30); do
delete_subvolume_recursively "$i"
done
2024-11-02 13:24:01 +00:00
2024-11-09 14:02:26 +00:00
${pkgs.btrfs-progs}/bin/btrfs subvolume create ${cfg.home}
chown -R ${name}:${cfg.group} ${cfg.home}
2024-11-09 14:02:26 +00:00
mkdir -p /persistent/home/${name}
chown -R ${name}:${cfg.group} /persistent/home/${name}
'';
};
}
{
name = "home-manager-${name}";
value = {
wantedBy = mkForce [
"user@${toString cfg.uid}.service"
];
after = [
"cleanup-home-${name}.service"
];
before = [
"user@${toString cfg.uid}.service"
];
partOf = [
"user@${toString cfg.uid}.service"
];
};
}
]
) config.environment.impermanence.users
)
);
};
2024-11-02 13:24:01 +00:00
}