|
11 | 11 | in |
12 | 12 | { |
13 | 13 | options.modules.desktop.impermanence-rootfs = { |
| 14 | + enable = mkEnableOption "impermanence-rootfs"; |
14 | 15 | fsType = mkOption { |
15 | 16 | type = types.enum [ |
16 | 17 | "tmpfs" |
|
51 | 52 | }; |
52 | 53 | }; |
53 | 54 |
|
54 | | - config = { |
55 | | - assertions = [ |
56 | | - { |
57 | | - assertion = cfg.fsType == "btrfs" -> cfg.btrfsBlockDevice != ""; |
58 | | - message = "fsType=btrfs requires btrfsBlockDevice to be set"; |
59 | | - } |
60 | | - ]; |
61 | | - } |
62 | | - // mkMerge [ |
63 | | - # TMPFS SUPPORT |
64 | | - (mkIf (cfg.fsType == "tmpfs" && !diskoEnabled) { |
65 | | - fileSystems."/" = { |
66 | | - device = "tmpfs"; |
67 | | - fsType = "tmpfs"; |
68 | | - options = [ |
69 | | - "relatime" |
70 | | - "mode=755" |
71 | | - ]; |
72 | | - }; |
73 | | - }) |
| 55 | + config = |
| 56 | + mkIf cfg.enable { |
| 57 | + assertions = [ |
| 58 | + { |
| 59 | + assertion = cfg.fsType == "btrfs" -> cfg.btrfsBlockDevice != ""; |
| 60 | + message = "fsType=btrfs requires btrfsBlockDevice to be set"; |
| 61 | + } |
| 62 | + ]; |
| 63 | + } |
| 64 | + // mkMerge [ |
| 65 | + # TMPFS SUPPORT |
| 66 | + (mkIf (cfg.enable && cfg.fsType == "tmpfs" && !diskoEnabled) { |
| 67 | + fileSystems."/" = { |
| 68 | + device = "tmpfs"; |
| 69 | + fsType = "tmpfs"; |
| 70 | + options = [ |
| 71 | + "relatime" |
| 72 | + "mode=755" |
| 73 | + ]; |
| 74 | + }; |
| 75 | + }) |
74 | 76 |
|
75 | | - (mkIf (cfg.fsType == "tmpfs" && diskoEnabled) { |
76 | | - disko.devices.nodev."/" = lib.mkForce { |
77 | | - fsType = "tmpfs"; |
78 | | - mountOptions = [ |
79 | | - "relatime" # Update inode access times relative to modify/change time |
80 | | - "mode=755" |
81 | | - ]; |
82 | | - }; |
83 | | - }) |
| 77 | + (mkIf (cfg.enable && cfg.fsType == "tmpfs" && diskoEnabled) { |
| 78 | + disko.devices.nodev."/" = lib.mkForce { |
| 79 | + fsType = "tmpfs"; |
| 80 | + mountOptions = [ |
| 81 | + "relatime" # Update inode access times relative to modify/change time |
| 82 | + "mode=755" |
| 83 | + ]; |
| 84 | + }; |
| 85 | + }) |
84 | 86 |
|
85 | | - # BTRFS SUPPORT |
86 | | - (mkIf (cfg.fsType == "btrfs" && !diskoEnabled) { |
87 | | - fileSystems."/" = lib.mkForce { |
88 | | - device = cfg.btrfsBlockDevice; |
89 | | - fsType = "btrfs"; |
90 | | - options = [ "subvol=root" ]; |
91 | | - }; |
92 | | - }) |
| 87 | + # BTRFS SUPPORT |
| 88 | + (mkIf (cfg.enable && cfg.fsType == "btrfs" && !diskoEnabled) { |
| 89 | + fileSystems."/" = lib.mkForce { |
| 90 | + device = cfg.btrfsBlockDevice; |
| 91 | + fsType = "btrfs"; |
| 92 | + options = [ "subvol=root" ]; |
| 93 | + }; |
| 94 | + }) |
93 | 95 |
|
94 | | - (mkIf (cfg.fsType == "btrfs" && diskoEnabled) { |
95 | | - # disko.devices.nodev."/" = lib.mkForce { |
96 | | - # fsType = "btrfs"; |
97 | | - # mountOptions = [ "subvol=root" ]; |
98 | | - # }; |
99 | | - fileSystems."/" = lib.mkForce { |
100 | | - device = cfg.btrfsBlockDevice; |
101 | | - fsType = "btrfs"; |
102 | | - options = [ "subvol=root" ]; |
103 | | - }; |
104 | | - }) |
| 96 | + (mkIf (cfg.enable && cfg.fsType == "btrfs" && diskoEnabled) { |
| 97 | + # disko.devices.nodev."/" = lib.mkForce { |
| 98 | + # fsType = "btrfs"; |
| 99 | + # mountOptions = [ "subvol=root" ]; |
| 100 | + # }; |
| 101 | + fileSystems."/" = lib.mkForce { |
| 102 | + device = cfg.btrfsBlockDevice; |
| 103 | + fsType = "btrfs"; |
| 104 | + options = [ "subvol=root" ]; |
| 105 | + }; |
| 106 | + }) |
105 | 107 |
|
106 | | - (mkIf (cfg.fsType == "btrfs" && config.boot.initrd.systemd.enable) { |
107 | | - boot.initrd.systemd.services.impermanence-setup = lib.mkForce { |
108 | | - description = "Impermanence setup for btrfs root subvolume"; |
109 | | - wantedBy = [ "initrd.target" ]; |
110 | | - before = [ "initrd.target" ]; |
111 | | - # before = [ "sysroot.mount" ]; |
112 | | - # unitConfig.DefaultDependencies = "no"; |
113 | | - # wants = cfg.wants; |
114 | | - # afters = cfg.afters; |
115 | | - serviceConfig.Type = "oneshot"; |
116 | | - script = '' |
117 | | - mkdir -p /btrfs_tmp |
118 | | - mount -t btrfs ${cfg.btrfsBlockDevice} /btrfs_tmp |
119 | | - if [ -e /btrfs_tmp/root ]; then |
120 | | - ${cfg.preBackupCommand} |
121 | | - mkdir -p /btrfs_tmp/old_roots |
122 | | - timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/root)" "+%Y-%m-%d_%H:%M:%S") |
123 | | - mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp" |
124 | | - ${cfg.postBackupCommand} |
125 | | - fi |
| 108 | + (mkIf (cfg.enable && cfg.fsType == "btrfs" && config.boot.initrd.systemd.enable) { |
| 109 | + boot.initrd.systemd.services.impermanence-setup = lib.mkForce { |
| 110 | + description = "Impermanence setup for btrfs root subvolume"; |
| 111 | + wantedBy = [ "initrd.target" ]; |
| 112 | + before = [ "initrd.target" ]; |
| 113 | + # before = [ "sysroot.mount" ]; |
| 114 | + # unitConfig.DefaultDependencies = "no"; |
| 115 | + # wants = cfg.wants; |
| 116 | + # afters = cfg.afters; |
| 117 | + serviceConfig.Type = "oneshot"; |
| 118 | + script = '' |
| 119 | + mkdir -p /btrfs_tmp |
| 120 | + mount -t btrfs ${cfg.btrfsBlockDevice} /btrfs_tmp |
| 121 | + if [ -e /btrfs_tmp/root ]; then |
| 122 | + ${cfg.preBackupCommand} |
| 123 | + mkdir -p /btrfs_tmp/old_roots |
| 124 | + timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/root)" "+%Y-%m-%d_%H:%M:%S") |
| 125 | + mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp" |
| 126 | + ${cfg.postBackupCommand} |
| 127 | + fi |
126 | 128 |
|
127 | | - delete_subvolume_recursively() { |
128 | | - IFS=$'\n' |
129 | | - for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do |
130 | | - delete_subvolume_recursively "/btrfs_tmp/$i" |
131 | | - done |
132 | | - btrfs subvolume delete "$1" |
133 | | - } |
| 129 | + delete_subvolume_recursively() { |
| 130 | + IFS=$'\n' |
| 131 | + for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do |
| 132 | + delete_subvolume_recursively "/btrfs_tmp/$i" |
| 133 | + done |
| 134 | + btrfs subvolume delete "$1" |
| 135 | + } |
134 | 136 |
|
135 | | - for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +${toString cfg.retentionPeriod}); do |
136 | | - delete_subvolume_recursively "$i" |
137 | | - done |
| 137 | + for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +${toString cfg.retentionPeriod}); do |
| 138 | + delete_subvolume_recursively "$i" |
| 139 | + done |
138 | 140 |
|
139 | | - btrfs subvolume create /btrfs_tmp/root |
140 | | - umount /btrfs_tmp |
141 | | - ''; |
142 | | - }; |
143 | | - }) |
144 | | - (mkIf (cfg.fsType == "btrfs" && !config.boot.initrd.systemd.enable) { |
145 | | - boot.initrd.postDeviceCommands = ( |
146 | | - lib.mkAfter '' |
147 | | - mkdir -p /btrfs_tmp |
148 | | - mount -t btrfs ${cfg.btrfsBlockDevice} /btrfs_tmp |
149 | | - if [ -e /btrfs_tmp/root ]; then |
150 | | - ${cfg.preBackupCommand} |
151 | | - mkdir -p /btrfs_tmp/old_roots |
152 | | - timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/root)" "+%Y-%m-%d_%H:%M:%S") |
153 | | - mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp" |
154 | | - ${cfg.postBackupCommand} |
155 | | - fi |
| 141 | + btrfs subvolume create /btrfs_tmp/root |
| 142 | + umount /btrfs_tmp |
| 143 | + ''; |
| 144 | + }; |
| 145 | + }) |
| 146 | + (mkIf (cfg.enable && cfg.fsType == "btrfs" && !config.boot.initrd.systemd.enable) { |
| 147 | + boot.initrd.postDeviceCommands = ( |
| 148 | + lib.mkAfter '' |
| 149 | + mkdir -p /btrfs_tmp |
| 150 | + mount -t btrfs ${cfg.btrfsBlockDevice} /btrfs_tmp |
| 151 | + if [ -e /btrfs_tmp/root ]; then |
| 152 | + ${cfg.preBackupCommand} |
| 153 | + mkdir -p /btrfs_tmp/old_roots |
| 154 | + timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/root)" "+%Y-%m-%d_%H:%M:%S") |
| 155 | + mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp" |
| 156 | + ${cfg.postBackupCommand} |
| 157 | + fi |
156 | 158 |
|
157 | | - delete_subvolume_recursively() { |
158 | | - IFS=$'\n' |
159 | | - for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do |
160 | | - delete_subvolume_recursively "/btrfs_tmp/$i" |
161 | | - done |
162 | | - btrfs subvolume delete "$1" |
163 | | - } |
| 159 | + delete_subvolume_recursively() { |
| 160 | + IFS=$'\n' |
| 161 | + for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do |
| 162 | + delete_subvolume_recursively "/btrfs_tmp/$i" |
| 163 | + done |
| 164 | + btrfs subvolume delete "$1" |
| 165 | + } |
164 | 166 |
|
165 | | - for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +${toString cfg.retentionPeriod}); do |
166 | | - delete_subvolume_recursively "$i" |
167 | | - done |
| 167 | + for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +${toString cfg.retentionPeriod}); do |
| 168 | + delete_subvolume_recursively "$i" |
| 169 | + done |
168 | 170 |
|
169 | | - btrfs subvolume create /btrfs_tmp/root |
170 | | - umount /btrfs_tmp |
171 | | - '' |
172 | | - ); |
173 | | - }) |
174 | | - ]; |
| 171 | + btrfs subvolume create /btrfs_tmp/root |
| 172 | + umount /btrfs_tmp |
| 173 | + '' |
| 174 | + ); |
| 175 | + }) |
| 176 | + ]; |
175 | 177 | } |
0 commit comments