Skip to content

Commit 64e670c

Browse files
committed
feat(impermanence): wipe root on reboot
In addition, all the files that should be saved across reboots (i.e. known networks, SSH keys, printers, bluetooth devices) are being written to /persist and managed through the nix-community impermanence module
1 parent 3637015 commit 64e670c

File tree

9 files changed

+112
-17
lines changed

9 files changed

+112
-17
lines changed

flake.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
# NOTE: nix version is manually kept in sync with home-manager
2020
inputs = {
2121
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
22-
home-manager = {
23-
url = "github:nix-community/home-manager";
24-
inputs.nixpkgs.follows = "nixpkgs";
25-
};
22+
impermanence.url = "github:nix-community/impermanence";
2623
sops-nix = {
2724
url = "github:Mic92/sops-nix";
2825
inputs.nixpkgs.follows = "nixpkgs";
@@ -31,14 +28,19 @@
3128
url = "github:nix-community/disko";
3229
inputs.nixpkgs.follows = "nixpkgs";
3330
};
31+
home-manager = {
32+
url = "github:nix-community/home-manager";
33+
inputs.nixpkgs.follows = "nixpkgs";
34+
};
3435
};
3536

3637
outputs =
3738
{
3839
nixpkgs,
3940
self,
40-
disko,
4141
sops-nix,
42+
disko,
43+
impermanence,
4244
...
4345
}@inputs:
4446
let
@@ -65,6 +67,7 @@
6567
nixpkgs.lib.nixosSystem {
6668
modules = [
6769
disko.nixosModules.disko
70+
impermanence.nixosModules.impermanence
6871
(import ./modules)
6972
(import ./users)
7073
]

hosts/luna/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ in
3939
efi.enable = true;
4040
encryption.enable = true;
4141
btrfs.enable = true;
42+
impermanence.enable = true;
4243
swap.enable = true;
4344

4445
network = {

modules/bluetooth.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@ with lib;
3232
deps = [ ];
3333
};
3434
};
35+
36+
host.impermanence.directories = mkIf config.host.impermanence.enable [
37+
"/var/lib/bluetooth"
38+
];
3539
};
3640
}

modules/impermanence.nix

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{ config, lib, ... }:
2+
3+
let
4+
cfg = config.host.impermanence;
5+
in
6+
with lib;
7+
{
8+
options.host.impermanence = {
9+
enable = mkEnableOption "impermanence";
10+
directories = lib.mkOption {
11+
type = lib.types.listOf lib.types.str;
12+
description = "folders that should be stored in /persist";
13+
};
14+
};
15+
16+
config = mkIf cfg.enable {
17+
environment.persistence."/persist" = {
18+
directories = [
19+
"/root"
20+
"/var/lib/nixos"
21+
]
22+
++ config.host.impermanence.directories;
23+
hideMounts = true;
24+
};
25+
26+
boot.initrd.systemd = {
27+
services.rollback = {
28+
description = "Rollback BTRFS root subvolume to a pristine state";
29+
wantedBy = [ "initrd.target" ];
30+
after = [ "systemd-cryptsetup@pool0_0.service" ];
31+
before = [ "sysroot.mount" ];
32+
unitConfig.DefaultDependencies = "no";
33+
serviceConfig.Type = "oneshot";
34+
script = # sh
35+
''
36+
mkdir -p /mnt
37+
mount -o subvol=/ /dev/mapper/pool0_0 /mnt
38+
39+
btrfs subvolume list -o /mnt/root | cut -f9 -d ' ' | while read subvolume; do
40+
echo "deleting /$subvolume subvolume..."
41+
btrfs subvolume delete "/mnt/$subvolume"
42+
done
43+
44+
echo "deleting /root subvolume..."
45+
btrfs subvolume delete /mnt/root
46+
47+
echo "restoring blank /root subvolume..."
48+
btrfs subvolume snapshot /mnt/root-blank /mnt/root
49+
50+
umount /mnt
51+
'';
52+
};
53+
};
54+
};
55+
}

modules/network/manager.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ with lib;
3333
iwd.enable = true;
3434
};
3535
};
36+
37+
host.impermanence.directories = mkIf config.host.impermanence.enable [
38+
"/var/lib/iwd"
39+
];
3640
};
3741
}

modules/openssh.nix

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ with lib;
1919
config = mkIf cfg.enable {
2020
services.openssh = {
2121
enable = true;
22-
hostKeys = [
23-
# TODO: impermanence baby
24-
# {
25-
# path = "/persist/etc/ssh/ssh_host_ed25519_key";
26-
# type = "ed25519";
27-
# }
28-
{
29-
path = "/etc/ssh/ssh_host_ed25519_key";
30-
type = "ed25519";
31-
}
32-
];
22+
hostKeys =
23+
if config.host.impermanence.enable then
24+
[
25+
{
26+
path = "/persist/etc/ssh/ssh_host_ed25519_key";
27+
type = "ed25519";
28+
}
29+
]
30+
else
31+
[
32+
{
33+
path = "/etc/ssh/ssh_host_ed25519_key";
34+
type = "ed25519";
35+
}
36+
];
3337
settings = {
3438
AllowUsers = users;
3539
KbdInteractiveAuthentication = mkDefault false;

modules/printing.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ with lib;
2323
brlaser
2424
];
2525
};
26+
27+
host.impermanence.directories = mkIf config.host.impermanence.enable [
28+
"/var/lib/cups"
29+
];
2630
};
2731
}

modules/sops.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ with lib;
2929
sops = {
3030
defaultSopsFile = ../.sops.yaml;
3131
age = {
32-
sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
32+
sshKeyPaths =
33+
if config.host.impermanence.enable then
34+
[ "/persist/etc/ssh/ssh_host_ed25519_key" ]
35+
else
36+
[ "/etc/ssh/ssh_host_ed25519_key" ];
3337
};
3438
};
3539
};

0 commit comments

Comments
 (0)