From b0a8874a93d73e0719e8376bd280278cfe2757e4 Mon Sep 17 00:00:00 2001 From: Jan-Bulthuis Date: Thu, 29 May 2025 16:34:24 +0200 Subject: [PATCH] Set up impermanence --- modules/home/utilities/impermanence.nix | 34 ++++++++++++++++++++ modules/nixos/impermanence.nix | 42 +++++++++++++++++++++++++ profiles/nixos/vm.nix | 8 +++++ 3 files changed, 84 insertions(+) create mode 100644 modules/home/utilities/impermanence.nix create mode 100644 modules/nixos/impermanence.nix diff --git a/modules/home/utilities/impermanence.nix b/modules/home/utilities/impermanence.nix new file mode 100644 index 0000000..a7bb2a7 --- /dev/null +++ b/modules/home/utilities/impermanence.nix @@ -0,0 +1,34 @@ +{ lib, config, ... }: + +with lib; +let + cfg = config.modules.impermanence; +in +{ + options.modules.impermanence = { + enable = mkEnableOption "Impermanence"; + directories = mkOption { + type = types.listOf types.str; + default = [ ]; + description = '' + Directories that should be stored in persistent storage. + ''; + }; + files = mkOption { + type = types.listOf types.str; + default = [ ]; + description = '' + Files that should be stored in persistent storage. + ''; + }; + }; + + config = mkIf cfg.enable { + home.persistence."/persist/home" = { + enable = true; + hideMounts = true; + directories = cfg.directories; + files = cfg.files; + }; + }; +} diff --git a/modules/nixos/impermanence.nix b/modules/nixos/impermanence.nix new file mode 100644 index 0000000..b0ff42f --- /dev/null +++ b/modules/nixos/impermanence.nix @@ -0,0 +1,42 @@ +{ lib, config, ... }: + +with lib; +let + cfg = config.modules.impermanence; +in +{ + options.modules.impermanence = { + enable = mkEnableOption "Impermanence"; + directories = mkOption { + type = types.listOf types.str; + default = [ ]; + description = '' + Directories that should be stored in persistent storage. + ''; + }; + files = mkOption { + type = types.listOf types.str; + default = [ ]; + description = '' + Files that should be stored in persistent storage. + ''; + }; + resetScript = mkOption { + type = types.lines; + description = '' + Script to run on boot that resets the root partition. + ''; + }; + }; + + config = mkIf cfg.enable { + boot.initrd.postResumeCommands = mkAfter cfg.resetScript; + + environment.persistence."/persist/system" = { + enable = true; + hideMounts = true; + directories = cfg.directories; + files = cfg.files; + }; + }; +} diff --git a/profiles/nixos/vm.nix b/profiles/nixos/vm.nix index c07cd0c..341a139 100644 --- a/profiles/nixos/vm.nix +++ b/profiles/nixos/vm.nix @@ -23,6 +23,14 @@ in enable = true; profile = "vm"; }; + impermanence = { + enable = true; + directories = [ "/var/lib/nixos" ]; + resetScript = '' + # Revert to the blank state for the root directory + zfs rollback -r tank/root@blank + ''; + }; ssh.enable = true; };