nixos-config/modules/environment/user-impermanence.nix
Charlotte 🦝 Delenk 3993029950
Some checks failed
Hydra packages.aarch64-linux.kodi-joyn Hydra build #25894 of nixos-config:pr618:packages.aarch64-linux.kodi-joyn
Hydra packages.aarch64-linux.art-lotte-bgs-sfw Hydra build #25893 of nixos-config:pr618:packages.aarch64-linux.art-lotte-bgs-sfw
Hydra packages.x86_64-linux.art-lotte-bgs-sfw Hydra build #25899 of nixos-config:pr618:packages.x86_64-linux.art-lotte-bgs-sfw
Hydra packages.x86_64-linux.kodi-joyn Hydra build #25900 of nixos-config:pr618:packages.x86_64-linux.kodi-joyn
Hydra devShells.x86_64-linux.default Hydra build #25879 of nixos-config:pr618:devShells.x86_64-linux.default
Hydra packages.x86_64-linux.art-lotte Hydra build #25897 of nixos-config:pr618:packages.x86_64-linux.art-lotte
Hydra packages.x86_64-linux.package-updater Hydra build #25901 of nixos-config:pr618:packages.x86_64-linux.package-updater
Hydra packages.x86_64-linux.art-lotte-bgs-nsfw Hydra build #25898 of nixos-config:pr618:packages.x86_64-linux.art-lotte-bgs-nsfw
Hydra packages.aarch64-linux.art-lotte Hydra build #25891 of nixos-config:pr618:packages.aarch64-linux.art-lotte
Hydra nixosConfigurations.not522-installer Hydra build #26074 of nixos-config:pr618:nixosConfigurations.not522-installer
Hydra nixosConfigurations.thinkrac Hydra build #26077 of nixos-config:pr618:nixosConfigurations.thinkrac
Hydra nixosConfigurations.not522 Hydra build #26073 of nixos-config:pr618:nixosConfigurations.not522
Hydra nixosConfigurations.pc-installer Hydra build #26075 of nixos-config:pr618:nixosConfigurations.pc-installer
Hydra packages.aarch64-linux.art-lotte-bgs-nsfw Hydra build #25892 of nixos-config:pr618:packages.aarch64-linux.art-lotte-bgs-nsfw
Hydra packages.riscv64-linux.art-lotte Hydra build #25752 of nixos-config:pr618:packages.riscv64-linux.art-lotte
Hydra packages.aarch64-linux.package-updater Hydra build #25895 of nixos-config:pr618:packages.aarch64-linux.package-updater
Hydra packages.riscv64-linux.art-lotte-bgs-sfw Hydra build #25754 of nixos-config:pr618:packages.riscv64-linux.art-lotte-bgs-sfw
Hydra packages.riscv64-linux.art-lotte-bgs-nsfw Hydra build #25753 of nixos-config:pr618:packages.riscv64-linux.art-lotte-bgs-nsfw
Hydra nixosConfigurations.rainbow-resort Hydra build #26076 of nixos-config:pr618:nixosConfigurations.rainbow-resort
Hydra packages.riscv64-linux.package-updater Hydra build #25896 of nixos-config:pr618:packages.riscv64-linux.package-updater
use symlinks by default
2024-11-17 09:37:40 +01:00

122 lines
4.2 KiB
Nix

{
config,
lib,
pkgs,
...
}:
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 && config.environment.impermanence.users != [ ])
{
programs.fuse.userAllowOther = true;
home-manager.users = listToAttrs (
map (name: {
inherit name;
value =
{ config, ... }:
{
home.file."${config.home.homeDirectory}/.cache/.keep" = {
enable = false;
};
home.persistence.default = {
persistentStoragePath = "/persistent/home/${name}";
allowOther = true;
defaultMethod = "symlink";
directories = [
"Downloads"
"Music"
"Pictures"
"Documents"
"Videos"
".cache"
"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
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"
}
for i in $(find /persistent/old-homedirs/${name} -maxdepth 1 -atime +30); do
delete_subvolume_recursively "$i"
done
${pkgs.btrfs-progs}/bin/btrfs subvolume create ${cfg.home}
chown -R ${name}:${cfg.group} ${cfg.home}
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
)
);
};
}