From 97f720558dbc2c9af8a0f30788e73a4c95b02ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 18 Jul 2023 16:32:45 +0300 Subject: [PATCH] base/snapshot: move sanoid there --- hosts/hel1-a/configuration.nix | 31 +++++----------- hosts/vm/configuration.nix | 10 ----- modules/base/snapshot/default.nix | 61 +++++++++++++++---------------- 3 files changed, 40 insertions(+), 62 deletions(-) diff --git a/hosts/hel1-a/configuration.nix b/hosts/hel1-a/configuration.nix index 50b056e..6ea71bd 100644 --- a/hosts/hel1-a/configuration.nix +++ b/hosts/hel1-a/configuration.nix @@ -67,10 +67,16 @@ in { stateVersion = "22.11"; timeZone = "UTC"; - base.initrd = { - enable = true; - authorizedKeys = builtins.attrValues myData.ssh_pubkeys; - hostKeys = ["/etc/secrets/initrd/ssh_host_ed25519_key"]; + base = { + initrd = { + enable = true; + authorizedKeys = builtins.attrValues myData.ssh_pubkeys; + hostKeys = ["/etc/secrets/initrd/ssh_host_ed25519_key"]; + }; + snapshot = { + enable = true; + mountpoints = ["/var/lib" "/var/log"]; + }; }; }; @@ -141,23 +147,6 @@ in { localuser = null; }; - sanoid = { - enable = true; - templates.prod = { - hourly = 24; - daily = 7; - autosnap = true; - autoprune = true; - }; - datasets = - lib.mapAttrs' (name: value: { - name = value.zfs_name; - value = {use_template = ["prod"];}; - }) - backup_paths; - extraArgs = ["--verbose"]; - }; - borgbackup.jobs = lib.mapAttrs' (name: value: let snapshot = { diff --git a/hosts/vm/configuration.nix b/hosts/vm/configuration.nix index 41ef56b..c0eeaf3 100644 --- a/hosts/vm/configuration.nix +++ b/hosts/vm/configuration.nix @@ -10,16 +10,6 @@ in { stateVersion = "23.05"; timeZone = "UTC"; stubPasswords = true; - - base.snapshot = { - enable = true; - pools = { - var_lib = { - mountpoint = "/var/lib"; - zfs_name = "rpool/nixos/var/lib"; - }; - }; - }; }; environment = { diff --git a/modules/base/snapshot/default.nix b/modules/base/snapshot/default.nix index 1d18fa5..b028818 100644 --- a/modules/base/snapshot/default.nix +++ b/modules/base/snapshot/default.nix @@ -3,44 +3,43 @@ lib, myData, ... -}: -with lib; { +}: { options.mj.base.snapshot = { - enable = mkEnableOption "Enable zfs snapshots"; + enable = lib.mkEnableOption "Enable zfs snapshots"; - pools = mkOption { + mountpoints = lib.mkOption { default = {}; - type = with types; - attrsOf (submodule ( - {...}: { - options = { - mountpoint = mkOption {type = str;}; - zfs_name = mkOption {type = str;}; - #paths = mkOption { type = listOf str; }; - #backup_at = mkOption { type = str; }; - }; - } - )); + type = with lib.types; listOf str; }; }; - config = with config.mj.base.snapshot; - mkIf enable { - sanoid = { - enable = true; - templates.prod = { - hourly = 24; - daily = 7; - autosnap = true; - autoprune = true; - }; - datasets = - lib.mapAttrs' (name: value: { - name = value.zfs_name; + config = lib.mkIf config.mj.base.snapshot.enable { + services.sanoid = { + enable = true; + templates.prod = { + hourly = 24; + daily = 7; + autosnap = true; + autoprune = true; + }; + extraArgs = ["--verbose"]; + datasets = let + fs_zfs = lib.filterAttrs (n: v: v.fsType == "zfs") config.fileSystems; + mountpoint2fs = + builtins.listToAttrs + (map (mountpoint: { + name = mountpoint; + value = builtins.getAttr mountpoint fs_zfs; + }) + config.mj.base.snapshot.mountpoints); + s_datasets = + lib.mapAttrs' (mountpoint: fs: { + name = fs.device; value = {use_template = ["prod"];}; }) - pools; - extraArgs = ["--verbose"]; - }; + mountpoint2fs; + in + s_datasets; }; + }; }