diff --git a/machines/niczos/default.nix b/machines/niczos/default.nix index 5368e01..081784d 100644 --- a/machines/niczos/default.nix +++ b/machines/niczos/default.nix @@ -15,9 +15,7 @@ in /etc/nixos/modules/security.nix /etc/nixos/modules/ssl.nix /etc/nixos/modules/tailscale.nix - - # temporary headscale - /etc/nixos/modules/headscale + /etc/nixos/modules/backup.nix # machine-specific config ./bootloader.nix @@ -48,6 +46,10 @@ in }; }; + services.backup.enable = true; + services.backup.source = "/home/zo/dev"; + services.backup.dest = "/tmp/"; + programs = { steam.enable = true; fuse.userAllowOther = true; diff --git a/modules/backup.nix b/modules/backup.nix index 98d3429..121eeac 100644 --- a/modules/backup.nix +++ b/modules/backup.nix @@ -1,8 +1,14 @@ -{ lib, config, ... }: +{ + lib, + config, + pkgs, + ... +}: let cfg = config.services.backup; in { + # Configuration options.services.backup = { enable = lib.mkEnableOption "Whether to enable backups or not."; @@ -18,17 +24,98 @@ in 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.path; + 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 = lib.mkIf cfg.enable { - systemd.services.backup = { - wantedBy = [ "multi-user.target" ]; + # Implementation + config = + let + script = pkgs.writeShellApplication { + name = "backup.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} + '' + + ( + if cfg.encryption.command != null then + /* bash */ '' + + # Archive encryption (optionnal) + SRC=$DST + DST=$(mktemp --suffix=.tar.encrypted) + echo "Encyphering $SRC to $DST." + '' + + cfg.encryption.command + + " + " + else + "" + ) + + /* bash */ '' + + # Archive salvation + SRC=$DST + DST="${cfg.dest}/${baseNameOf cfg.source}.tar.enc" + echo "Saving $SRC to $DST." + ${cfg.salvation.command} + ''; + }; + 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"; + }; + }; }; - }; }