nix-config/nixos/_mixins/optin-persistence.nix

40 lines
1.2 KiB
Nix

# https://nixos.wiki/wiki/Impermanence
#
# https://nixos.wiki/wiki/Impermanence
# https://grahamc.com/blog/erase-your-darlings/
# https://lantian.pub/en/article/modify-computer/nixos-impermanence.lantian/
#
# This file defines the "non-hardware dependent" part of opt-in persistence
# It imports impermanence, defines the basic persisted dirs, and ensures each
# users' home persist dir exists and has the right permissions
#
# It works even if / is tmpfs, btrfs snapshot, or even not ephemeral at all.
{ lib, inputs, config, ... }: {
imports = [
inputs.impermanence.nixosModules.impermanence
];
environment.persistence = {
"/persist" = {
directories = [
"/var/lib/containers"
"/var/lib/systemd"
"/var/lib/nixos"
# "/var/log"
"/srv"
];
};
};
programs.fuse.userAllowOther = true;
system.activationScripts.persistent-dirs.text =
let
mkHomePersist = user: lib.optionalString user.createHome ''
mkdir -p /persist/${user.home}
chown ${user.name}:${user.group} /persist/${user.home}
chmod ${user.homeMode} /persist/${user.home}
'';
users = lib.attrValues config.users.users;
in
lib.concatLines (map mkHomePersist users);
}