nixos-config/modules/environment/user-impermanence.nix
Charlotte 🦝 Delenk 047a3a4bac
All checks were successful
Hydra nixosConfigurations.container-default-x86_64-linux Hydra build #23206 of nixos-config:pr618:nixosConfigurations.container-default-x86_64-linux
Hydra nixosConfigurations.container-default-aarch64-linux Hydra build #23204 of nixos-config:pr618:nixosConfigurations.container-default-aarch64-linux
Hydra checks.x86_64-linux.containers-default Hydra build #23203 of nixos-config:pr618:checks.x86_64-linux.containers-default
Hydra nixosConfigurations.container-default-riscv64-linux Hydra build #23205 of nixos-config:pr618:nixosConfigurations.container-default-riscv64-linux
Hydra nixosConfigurations.not522 Hydra build #23207 of nixos-config:pr618:nixosConfigurations.not522
Hydra nixosConfigurations.not522-installer Hydra build #23208 of nixos-config:pr618:nixosConfigurations.not522-installer
use user@uid.service and not user-uid.slice
2024-11-03 08:21:43 +01:00

71 lines
2.1 KiB
Nix

{
config,
lib,
...
}:
with lib; {
options = {
environment.impermanence.users = mkOption {
description = "Which users to clean up the home directory for";
default = [];
type = types.listOf types.str;
};
};
config = mkIf config.environment.impermanence.enable {
systemd.services = listToAttrs (flatten (map (name: let
cfg = config.users.users.${name};
in [
{
name = "cleanup-home-${name}";
description = "Clean home directory for ${name}";
value = {
wantedBy = [
"user@${toString cfg.uid}.service"
];
before = [
"user@${toString cfg.uid}.service"
"home-manager-${name}.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} /home/old-homedirs/${name}/$timestamp
fi
delete_subvolume_recursively() {
IFS=$'\n'
for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do
delete_subvolume_recursively "/persistent/old-homedirs/${name}/$i"
done
btrfs subvolume delete "$1"
}
for i in $(find /persistent/old-homedirs/${name} -maxdepth 1 -mtime +30); do
delete_subvolume_recursively "$i"
done
btrfs subvolume create ${cfg.home}
'';
};
}
{
name = "home-manager-${name}";
value = {
wantedBy = mkForce [
"user@${toString cfg.uid}.service"
];
after = [
"cleanup-home-${name}.service"
];
before = [
"user@${toString cfg.uid}.service"
];
};
}
])
config.environment.impermanence.users));
};
}