blob: 634230ead11a7f4ac4848103c416c2868259323c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
{ inputs, ... }:
{
flake.modules.nixos.base =
{
config,
lib,
pkgs,
...
}:
{
imports = [ inputs.impermanence.nixosModules.impermanence ];
options.infra.persist = {
dir = lib.mkOption {
type = lib.types.path;
default = "/persist";
description = "The root directory to use for persistence.";
};
directories = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "The list of root owned directories to persist across reboots.";
};
files = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "The list of root owned files to persist across reboots.";
};
users = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options = {
directories = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "The list of directories to persist within the users home directory.";
};
files = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "The list of files to persist within the users home directory.";
};
};
}
);
default = { };
description = "The directories and files to persist for a user.";
};
};
config =
let
cfg = config.infra.persist;
blank = "${config.infra.zfs.pool}/local/root@blank";
in
{
boot = {
initrd = {
supportedFilesystems.zfs = true;
systemd = {
storePaths = [ pkgs.zfs ];
services.infra-rollback-root = {
description = "Rollback root ZFS dataset to blank snapshot";
wantedBy = [ "initrd.target" ];
after = [ "zfs-import-${config.infra.zfs.pool}.service" ];
before = [ "sysroot.mount" ];
enableStrictShellChecks = true;
serviceConfig = {
Type = "oneshot";
};
script = ''
if "${pkgs.zfs}/bin/zfs" list -H -o name "${blank}" >/dev/null 2>&1; then
"${pkgs.zfs}/bin/zfs" rollback -r "${blank}"
fi
'';
};
};
};
};
fileSystems."${cfg.dir}".neededForBoot = true;
environment.persistence."${cfg.dir}" = {
directories = [
# keep-sorted start
"/var/lib/nixos"
"/var/lib/systemd"
"/var/log/journal"
# keep-sorted end
]
++ cfg.directories;
files = [
"/etc/machine-id"
# Needed for sops-nix to be able to decrypt secrets.
"/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub"
]
++ cfg.files;
};
assertions = [
{
assertion = config.boot.initrd.systemd.enable;
message = "initrd systemd must be enabled for persistence (impermanence)";
}
];
};
};
}
|