diff --git a/machines/niczos/default.nix b/machines/niczos/default.nix index 4843ece..59959c5 100644 --- a/machines/niczos/default.nix +++ b/machines/niczos/default.nix @@ -15,7 +15,7 @@ in /etc/nixos/modules/security.nix /etc/nixos/modules/ssl.nix /etc/nixos/modules/tailscale.nix - /etc/nixos/modules/backup.nix + /etc/nixos/modules/backup # machine-specific config ./bootloader.nix @@ -49,12 +49,12 @@ in services.backup = { "/home/zo/dev" = { enable = true; - dest = "/tmp/"; + dest = "/tmp/backups/"; }; "/home/zo/Documents" = { enable = true; - dest = "/tmp/"; + dest = "/tmp/backups/"; }; }; diff --git a/modules/backup.nix b/modules/backup.nix deleted file mode 100644 index 4a2d4ab..0000000 --- a/modules/backup.nix +++ /dev/null @@ -1,119 +0,0 @@ -{ - lib, - pkgs, - config, - ... -}: -{ - # Configuration - 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."; - - 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."; - }; - }; - - 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\""; - }; - }; - }; - - } - ) - ); - }; - - config = - let - 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=${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." - ${attrset.value.encryption.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 - { - systemd = lib.attrsets.mergeAttrsList systemd_backups; - }; - -} diff --git a/modules/backup/default.nix b/modules/backup/default.nix new file mode 100644 index 0000000..e0d07fb --- /dev/null +++ b/modules/backup/default.nix @@ -0,0 +1,85 @@ +{ + lib, + pkgs, + config, + ... +}: +{ + # Configuration + 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."; + + 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."; + }; + }; + + 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\""; + }; + }; + }; + } + ) + ); + }; + + config = + let + systemd_set = ( + lib.attrsets.mapAttrs ( + path: config: + (import ./systemd-utils.nix { + inherit path config; + script = import ./shell-utils.nix { + inherit path config pkgs; + }; + }) + ) config.services.backup + ); + + merge = + key: + lib.pipe systemd_set [ + builtins.attrValues + (map (cfg: cfg.${key})) + lib.mkMerge + ]; + in + { + systemd = { + timers = merge "timers"; + services = merge "services"; + }; + }; +} diff --git a/modules/backup/shell-utils.nix b/modules/backup/shell-utils.nix new file mode 100644 index 0000000..6dacd26 --- /dev/null +++ b/modules/backup/shell-utils.nix @@ -0,0 +1,33 @@ +{ + path, + config, + pkgs, +}: +pkgs.writeShellApplication { + name = "backup-${baseNameOf path}.sh"; + runtimeInputs = [ + pkgs.gnutar + pkgs.gzip + ]; + text = '' + #!/bin/bash + + # Archive creation and compression + SRC=${path} + DST=$(mktemp --suffix=.tar) + echo "Archiving and compressing $SRC to $DST." + ${config.compression.command} + + # Archive encryption (optionnal) + SRC=$DST + DST=$(mktemp --suffix=.tar.encrypted) + echo "Encyphering $SRC to $DST." + ${config.encryption.command} + + # Archive salvation + SRC=$DST + DST="${config.dest}/${baseNameOf path}.tar.enc" + echo "Saving $SRC to $DST." + ${config.salvation.command} + ''; +} diff --git a/modules/backup/systemd-utils.nix b/modules/backup/systemd-utils.nix new file mode 100644 index 0000000..20b4bfc --- /dev/null +++ b/modules/backup/systemd-utils.nix @@ -0,0 +1,24 @@ +{ + path, + config, + script, +}: +{ + timers."backup-${baseNameOf path}" = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = "5m"; + OnCalendar = "* *-*-* 00:00:00"; + Unit = "backup-${baseNameOf path}.service"; + Persistent = true; + }; + }; + services."backup-${baseNameOf path}" = { + enable = config.enable; + description = "Saves a directory and its content."; + serviceConfig = { + Type = "simple"; + ExecStart = "${script}/bin/backup-${baseNameOf path}.sh"; + }; + }; +}