@@ -8,7 +8,6 @@ with lib;
8
8
with types ;
9
9
let
10
10
cfg = config . host . persistence ;
11
- inherit ( config . host ) drive ;
12
11
13
12
defaultPerms = {
14
13
mode = "0755" ;
160
159
root = mkOption {
161
160
type = str ;
162
161
default = "/persist" ;
162
+ readOnly = true ;
163
163
description = ''
164
164
The root directory for the host's persistent state.
165
165
'' ;
166
166
} ;
167
167
168
- type = mkOption {
169
- type = enum [
170
- "tmpfs"
171
- "snapshot"
172
- ] ;
173
- default = "tmpfs" ;
174
- } ;
175
-
176
168
files = mkOption {
177
169
type = listOf ( coercedTo str ( f : { file = f ; } ) rootFile ) ;
178
170
default = [ ] ;
250
242
] ;
251
243
} ;
252
244
253
- fileSystems = {
254
- "/persist" = {
255
- device = "/dev/disk/by-partlabel/${ drive . name } " ;
256
- fsType = drive . format ;
257
- options = [
258
- "subvol=@persist"
259
- "compress=zstd"
260
- ] ;
261
- neededForBoot = true ;
262
- } ;
263
-
264
- "/" = {
265
- device = if cfg . type == "tmpfs" then "none" else "/dev/disk/by-partlabel/${ drive . name } " ;
266
- fsType = if cfg . type == "tmpfs" then "tmpfs" else drive . format ;
267
- options =
268
- if cfg . type == "tmpfs" then
269
- [
270
- "defaults"
271
- "size=16G"
272
- "mode=755"
273
- ]
274
- else
275
- [
276
- "subvol=@root"
277
- "compress=zstd"
278
- ] ;
279
- neededForBoot = if cfg . type == "tmpfs" then false else true ;
280
- } ;
281
- } ;
282
-
283
- boot . initrd =
284
- let
285
- phase1Systemd = config . boot . initrd . systemd . enable ;
286
- wipeScript = ''
287
- mkdir /tmp -p
288
- MNTPOINT=$(mktemp -d)
289
- (
290
- mount -t btrfs -o subvol=/ /dev/disk/by-partlabel/${ drive . name } "$MNTPOINT"
291
- trap 'umount "$MNTPOINT"' EXIT
292
-
293
- echo "Creating needed directories"
294
- mkdir -p "$MNTPOINT"/@persist/var/{log,lib/{nixos,systemd}}
295
-
296
- echo "Cleaning root subvolume"
297
- btrfs subvolume list -o "$MNTPOINT/@root" | cut -f9 -d ' ' |
298
- while read -r subvolume; do
299
- btrfs subvolume delete "$MNTPOINT/$subvolume"
300
- done && btrfs subvolume delete "$MNTPOINT/@root"
301
-
302
- echo "Restoring blank subvolume"
303
- btrfs subvolume snapshot "$MNTPOINT/@root-blank" "$MNTPOINT/@root"
304
- )
305
- '' ;
306
- in
307
- mkIf ( drive . format == "btrfs" && cfg . type == "snapshot" ) {
308
- supportedFilesystems = [ "btrfs" ] ;
309
- postDeviceCommands = lib . mkIf ( ! phase1Systemd ) ( mkBefore wipeScript ) ;
310
- systemd . services . restore-root = mkIf phase1Systemd {
311
- description = "Rollback btrfs rootfs" ;
312
- wantedBy = [ "initrd.target" ] ;
313
- requires = [ "dev-disk-by\\ x2dpartlabel-${ drive . name } .device" ] ;
314
- after = [
315
- "dev-disk-by\\ x2dpartlabel-${ drive . name } .device"
316
- "systemd-cryptsetup@${ drive . name } .service"
317
- ] ;
318
- before = [ "sysroot.mount" ] ;
319
- unitConfig . DefaultDependencies = "no" ;
320
- serviceConfig . Type = "oneshot" ;
321
- script = wipeScript ;
322
- } ;
323
- } ;
324
-
325
- services . btrfs . autoScrub = mkIf ( drive . format == "btrfs" ) {
326
- enable = mkDefault false ;
327
- fileSystems = [ "/persist" ] ;
328
- interval = "Wed *-*-* 02:00:00" ;
329
- } ;
330
-
331
245
# services.snapper.configs = mkIf (drive.format == "btrfs") (builtins.foldl' recursiveUpdate { }
332
246
# ([{
333
247
# persist = {
0 commit comments