nixos-config/modules/environment/impermanence.nix

118 lines
3.4 KiB
Nix
Raw Normal View History

2024-10-29 08:21:03 +00:00
{
impermanence,
config,
lib,
pkgs,
inTester,
...
}:
with lib; {
2024-11-02 13:24:01 +00:00
imports = [
"${impermanence}/nixos.nix"
./user-impermanence.nix
];
2024-10-29 08:21:03 +00:00
options = {
2024-11-02 08:27:46 +00:00
environment.impermanence.enable = mkEnableOption "Enables impermanence";
2024-10-29 08:21:03 +00:00
};
config = mkMerge [
{
2024-11-02 08:27:46 +00:00
environment.impermanence.enable = mkDefault (!config.boot.isContainer && !inTester);
2024-10-29 08:21:03 +00:00
}
2024-11-02 08:49:12 +00:00
(mkIf config.environment.impermanence.enable {
2024-10-29 12:58:15 +00:00
boot.initrd.systemd.services.rootfs-cleanup = {
2024-11-01 07:39:59 +00:00
description = "Clean file system root";
2024-10-29 12:58:15 +00:00
wantedBy = [
"initrd.target"
];
after = [
"initrd-root-device.target"
];
before = [
"sysroot.mount"
];
unitConfig.DefaultDependencies = "no";
serviceConfig.Type = "oneshot";
script = ''
2024-11-01 14:05:23 +00:00
# workaround for machines without working rtc battery
# The time may not yet be correctly set, so wait until it is
2024-11-02 07:01:18 +00:00
if [[ $(date '+%s') -lt 1730469314 ]]; then
2024-11-01 14:05:23 +00:00
sleep 30 # this should hopefully be enough
fi
2024-10-29 12:58:15 +00:00
mkdir /btrfs_tmp
mount ${config.fileSystems."/".device} -t btrfs /btrfs_tmp
if [[ -e /btrfs_tmp/root ]]; then
mkdir -p /btrfs_tmp/old_roots
2024-11-02 08:27:46 +00:00
timestamp=$(date --date="@$(stat -c %X /btrfs_tmp/root)" "+%Y-%m-%d_%H:%M:%S")
2024-10-29 13:03:45 +00:00
mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp"
2024-10-29 12:58:15 +00:00
fi
2024-10-29 08:21:03 +00:00
2024-10-29 12:58:15 +00:00
delete_subvolume_recursively() {
IFS=$'\n'
for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do
delete_subvolume_recursively "/btrfs_tmp/$i"
done
2024-11-05 16:14:22 +00:00
btrfs subvolume delete "$1" || rm -rf "$1"
2024-10-29 12:58:15 +00:00
}
2024-10-29 08:21:03 +00:00
2024-11-05 14:52:50 +00:00
for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -atime +30); do
2024-10-29 12:58:15 +00:00
delete_subvolume_recursively "$i"
done
2024-10-29 08:21:03 +00:00
2024-10-29 12:58:15 +00:00
btrfs subvolume create /btrfs_tmp/root
umount /btrfs_tmp
'';
};
2024-10-29 08:21:03 +00:00
assertions = [
{
assertion = hasAttr "/" config.fileSystems;
message = "To use impermanence, you need to define a root volume";
}
{
assertion =
if hasAttr "/" config.fileSystems
then config.fileSystems."/".fsType == "btrfs"
else false;
message = "rootfs must be btrfs";
}
{
assertion =
if hasAttr "/" config.fileSystems
2024-10-29 12:29:05 +00:00
then any (t: t == "subvol=root" || t == "subvol=/root") config.fileSystems."/".options
2024-10-29 08:21:03 +00:00
else false;
message = "rootfs must mount subvolume root";
}
];
fileSystems."/persistent" = {
device =
if hasAttr "/" config.fileSystems
then mkDefault config.fileSystems."/".device
else "/dev/null";
fsType = "btrfs";
options = ["subvol=persistent"];
neededForBoot = true;
};
environment.persistence."/persistent" = {
enable = true;
hideMounts = true;
directories = [
"/var/log"
"/var/lib/nixos"
2024-11-06 08:17:37 +00:00
"/var/cache"
2024-10-29 08:21:03 +00:00
];
files = [
"/etc/ssh/ssh_host_ecdsa_key"
"/etc/ssh/ssh_host_ecdsa_key.pub"
"/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub"
"/etc/ssh/ssh_host_rsa_key"
"/etc/ssh/ssh_host_rsa_key.pub"
];
};
2024-11-06 08:17:37 +00:00
systemd.tmpfiles.rules = [
"d /persistent/var/cache 1777 root root 7d -"
];
2024-10-29 08:21:03 +00:00
})
];
}