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
|
|
|
...
|
|
|
|
}:
|
|
|
|
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 = [
|
2024-11-03 07:21:43 +00:00
|
|
|
"user@${toString cfg.uid}.service"
|
2024-11-02 13:24:01 +00:00
|
|
|
];
|
|
|
|
before = [
|
2024-11-03 07:21:43 +00:00
|
|
|
"user@${toString cfg.uid}.service"
|
2024-11-02 13:25:15 +00:00
|
|
|
"home-manager-${name}.service"
|
2024-11-02 13:24:01 +00:00
|
|
|
];
|
2024-11-03 08:10:25 +00:00
|
|
|
partOf = [
|
|
|
|
"user@${toString cfg.uid}.service"
|
|
|
|
];
|
2024-11-02 13:24:01 +00:00
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script = ''
|
|
|
|
if [[ -e ${cfg.home} ]]; then
|
|
|
|
timestamp=$(date --date="@$(stat -c %X ${cfg.home})" "+%Y-%m-%d_%H:%M:%S")
|
2024-11-02 13:47:17 +00:00
|
|
|
mkdir -p /persistent/old-homedirs/${name}
|
2024-11-03 08:05:13 +00:00
|
|
|
mv ${cfg.home} /persistent/old-homedirs/${name}/$timestamp
|
2024-11-02 13:24:01 +00:00
|
|
|
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
|
2024-11-03 09:56:04 +00:00
|
|
|
${pkgs.btrfs-progs}/bin/btrfs subvolume delete "$1"
|
2024-11-02 13:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in $(find /persistent/old-homedirs/${name} -maxdepth 1 -mtime +30); do
|
|
|
|
delete_subvolume_recursively "$i"
|
|
|
|
done
|
|
|
|
|
2024-11-03 09:56:04 +00:00
|
|
|
${pkgs.btrfs-progs}/bin/btrfs subvolume create ${cfg.home}
|
2024-11-02 13:24:01 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "home-manager-${name}";
|
|
|
|
value = {
|
|
|
|
wantedBy = mkForce [
|
2024-11-03 07:21:43 +00:00
|
|
|
"user@${toString cfg.uid}.service"
|
2024-11-02 13:24:01 +00:00
|
|
|
];
|
|
|
|
after = [
|
|
|
|
"cleanup-home-${name}.service"
|
|
|
|
];
|
|
|
|
before = [
|
2024-11-03 07:21:43 +00:00
|
|
|
"user@${toString cfg.uid}.service"
|
2024-11-02 13:24:01 +00:00
|
|
|
];
|
2024-11-03 08:10:25 +00:00
|
|
|
partOf = [
|
|
|
|
"user@${toString cfg.uid}.service"
|
|
|
|
];
|
2024-11-02 13:24:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
])
|
|
|
|
config.environment.impermanence.users));
|
|
|
|
};
|
|
|
|
}
|