diff --git a/machines/niczos/default.nix b/machines/niczos/default.nix index 081784d..4843ece 100644 --- a/machines/niczos/default.nix +++ b/machines/niczos/default.nix @@ -46,9 +46,17 @@ in }; }; - services.backup.enable = true; - services.backup.source = "/home/zo/dev"; - services.backup.dest = "/tmp/"; + services.backup = { + "/home/zo/dev" = { + enable = true; + dest = "/tmp/"; + }; + + "/home/zo/Documents" = { + enable = true; + dest = "/tmp/"; + }; + }; programs = { steam.enable = true; diff --git a/modules/backup.nix b/modules/backup.nix index bfd3df3..4a2d4ab 100644 --- a/modules/backup.nix +++ b/modules/backup.nix @@ -1,110 +1,119 @@ { lib, - config, pkgs, + config, ... }: -let - cfg = config.services.backup; -in { # Configuration - options.services.backup = { - enable = lib.mkEnableOption "Whether to enable backups or not."; + options.services.backup = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule ( + { ... }: + { + options = { + enable = lib.mkEnableOption "Whether to enable backups for this path or not."; - source = lib.mkOption { - type = lib.types.path; - default = null; - description = "The path to save."; - }; + dest = lib.mkOption { + type = lib.types.either lib.types.path lib.types.str; + default = null; + description = "A destination for the backup."; + }; - dest = lib.mkOption { - type = lib.types.either lib.types.path lib.types.str; - default = null; - description = "A destination for the backup."; - }; + compression = { + command = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = "tar -caf \"$DST\" \"$SRC\""; + description = "A bash command to compress backups. Defaults to an uncompressed tar archive."; + }; + }; - compression = { - command = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = "tar -caf \"$DST\" \"$SRC\""; - description = "A bash command to compress backups. Defaults to an uncompressed tar archive."; - }; - }; + encryption = { + command = lib.mkOption { + type = lib.types.str; + description = "A bash command to encrypt backups. Defaults to a clear archive (simple cat)."; + default = "cat \"$SRC\" > \"$DST\""; + }; + key = lib.mkOption { + type = lib.types.nullOr lib.types.path; + description = "A key to encrypt backups."; + default = null; + }; + }; - encryption = { - command = lib.mkOption { - type = lib.types.str; - description = "A bash command to encrypt backups. Defaults to a clear archive (simple cat)."; - default = "cat \"$SRC\" > \"$DST\""; - }; - key = lib.mkOption { - type = lib.types.nullOr lib.types.path; - description = "A key to encrypt backups."; - default = null; - }; - }; + salvation = { + command = lib.mkOption { + type = lib.types.str; + description = "A bash command to save the archive. Defaults to a copy from source to dest."; + default = "cp \"$SRC\" \"$DST\""; + }; + }; + }; - salvation = { - command = lib.mkOption { - type = lib.types.str; - description = "A bash command to save the archive. Defaults to a copy from source to dest."; - default = "cp \"$SRC\" \"$DST\""; - }; - }; + } + ) + ); }; - # Implementation config = let - script = pkgs.writeShellApplication { - name = "backup.sh"; - runtimeInputs = [ - pkgs.gnutar - pkgs.gzip - ]; - text = '' - #!/bin/bash + systemd_backups = map ( + attrset: + let + script = pkgs.writeShellApplication { + name = "backup-${baseNameOf attrset.name}.sh"; + runtimeInputs = [ + pkgs.gnutar + pkgs.gzip + ]; + text = '' + #!/bin/bash - # Archive creation and compression - SRC=${cfg.source} - DST=$(mktemp --suffix=.tar) - echo "Archiving and compressing $SRC to $DST." - ${cfg.compression.command} + # Archive creation and compression + SRC=${attrset.name} + DST=$(mktemp --suffix=.tar) + echo "Archiving and compressing $SRC to $DST." + ${attrset.value.compression.command} - # Archive encryption (optionnal) - SRC=$DST - DST=$(mktemp --suffix=.tar.encrypted) - echo "Encyphering $SRC to $DST." - ${cfg.encryption.command} + # Archive encryption (optionnal) + SRC=$DST + DST=$(mktemp --suffix=.tar.encrypted) + echo "Encyphering $SRC to $DST." + ${attrset.value.encryption.command} - # Archive salvation - SRC=$DST - DST="${cfg.dest}/${baseNameOf cfg.source}.tar.enc" - echo "Saving $SRC to $DST." - ${cfg.salvation.command} - ''; - }; + # Archive salvation + SRC=$DST + DST="${attrset.value.dest}/${baseNameOf attrset.name}.tar.enc" + echo "Saving $SRC to $DST." + ${attrset.value.salvation.command} + ''; + }; + in + { + timers."backup-${baseNameOf attrset.name}" = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = "5m"; + OnCalendar = "* *-*-* 00:00:00"; + Unit = "backup-${baseNameOf attrset.name}.service"; + Persistent = true; + }; + }; + services."backup-${baseNameOf attrset.name}" = { + enable = attrset.value.enable; + after = [ "network-online.target" ]; + wants = [ "network-online.target" ]; + description = "Saves a directory and its content."; + serviceConfig = { + Type = "simple"; + ExecStart = "${script}/bin/backup-${baseNameOf attrset.name}.sh"; + }; + }; + } + ) (lib.attrsets.attrsToList config.services.backup); in - lib.mkIf cfg.enable { - systemd.timers.backup = { - wantedBy = [ "timers.target" ]; - timerConfig = { - OnBootSec = "5m"; - OnCalendar = "* *-*-* 00:00:00"; - Unit = "backup.service"; - Persistent = true; - }; - }; - systemd.services.backup = { - enable = cfg.enable; - after = [ "network-online.target" ]; - wants = [ "network-online.target" ]; - description = "Saves a directory and its content."; - serviceConfig = { - Type = "simple"; - ExecStart = "${script}/bin/backup.sh"; - }; - }; + { + systemd = lib.attrsets.mergeAttrsList systemd_backups; }; + }