Skip to content

Commit 9092ebd

Browse files
committed
snapshot
1 parent b7c3a65 commit 9092ebd

1 file changed

Lines changed: 113 additions & 111 deletions

File tree

hosts/idols-ai/impermanence_addon.nix

Lines changed: 113 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let
1111
in
1212
{
1313
options.modules.desktop.impermanence-rootfs = {
14+
enable = mkEnableOption "impermanence-rootfs";
1415
fsType = mkOption {
1516
type = types.enum [
1617
"tmpfs"
@@ -51,125 +52,126 @@ in
5152
};
5253
};
5354

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+
})
7476

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+
})
8486

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+
})
9395

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+
})
105107

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
126128
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+
}
134136
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
138140
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
156158
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+
}
164166
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
168170
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+
];
175177
}

0 commit comments

Comments
 (0)