Tue Jun 4 12:53:01 AM CEST 2024

This commit is contained in:
Thomas Avé 2024-06-04 00:53:01 +02:00
parent d7b9755f91
commit e0e75470e8
1 changed files with 86 additions and 42 deletions

View File

@ -1,50 +1,89 @@
{ inputs, config, pkgs, ... }: { inputs, config, pkgs, ... }:
let let
mkTimer = { name, interval, serviceScript }: mkTimer = name: cfg:
let {
timerName = "${name}-timer"; Install.WantedBy = [ "timers.target" ];
serviceName = "${name}-service"; Timer = {
in Persistent = true;
{ OnCalendar = cfg.when;
systemd.timers.${timerName} = { Unit = "${name}.service";
wantedBy = [ "timers.target" ]; };
timerConfig = {
OnBootSec = interval;
OnUnitActiveSec = interval;
Unit = "${serviceName}.service";
};
}; };
mkService = name: cfg:
{
Unit.Description = name;
Install = {
WantedBy = [ "default.target" ];
};
Service = {
ExecStart = cfg.script;
};
};
scripts = {
disk_check = {
when = "*-*-* *:00:00";
script = toString (pkgs.writeShellScript "script" ''
REPORT_EMAIL=email@thomasave.be
ZPOOL_STATUS=$(zpool status -x)
if [ "$ZPOOL_STATUS" = "all pools are healthy" ] || [ "$ZPOOL_STATUS" = "no pools available" ]
then
printf 0 > /var/db/zpool.status
else
if [ "$(cat /var/db/zpool.status)" -eq 0 ]
then
zpool status | mail -s "ZPOOL NOT HEALTHY" $REPORT_EMAIL
printf 1 > /var/db/zpool.status
fi
fi
'');
};
vdirsyncer = {
when = "*:0/15";
script = toString (pkgs.writeShellScript "script" ''
${pkgs.vdirsyncer}/bin/vdirsyncer sync imec/pair
'');
};
mbsync = {
when = "*-*-* 00:00:00";
script = toString (pkgs.writeShellScript "script" ''
BASEDIR=/home/server/Containers/mbsync
# python3 $BASEDIR/config/oauth2/mutt_oauth2.py $BASEDIR/data/oauth2/credentials.json
docker exec --user "1000:1000" mbsync-container /home/user/.config/mbsync/run.sh -a
${pkgs.notmuch}/bin/notmuch new
'');
};
sync_vault = {
when = "*-*-* 02:00:00";
script = toString (pkgs.writeShellScript "script" ''
source $HOME/.secrets/Backup/env.sh
for _ in {1..2}; do
OUTPUT=$(echo "$ZFS_PASSPHRASE" | ssh 10.4.0.1 zfs load-key Vault/Thomas/Encrypted 2>&1);
if [ "$OUTPUT" == "Key load error: Key already loaded for 'Vault/Thomas/Encrypted'." ]; then
echo "Key successfully loaded, starting syncoid"
syncoid --no-privilege-elevation --no-sync-snap tank/Storage/Thomas/Workspace 10.4.0.1:Vault/Thomas/Encrypted/Storage/Workspace
syncoid --no-privilege-elevation --no-sync-snap tank/Storage/Thomas 10.4.0.1:Vault/Thomas/Encrypted/Storage/T
syncoid --no-privilege-elevation --no-sync-snap tank/Storage/Niels 10.4.0.1:Vault/Thomas/Encrypted/Storage/N
syncoid --no-privilege-elevation --no-sync-snap tank/Storage/Yolande 10.4.0.1:Vault/Thomas/Encrypted/Storage/Y
syncoid --no-privilege-elevation --no-sync-snap tank/Containers 10.4.0.1:Vault/Thomas/Encrypted/Containers
ssh 10.4.0.1 zfs unload-key Vault/Thomas/Encrypted
${pkgs.curl}/bin/curl https://uptime.thomasave.be/api/push/s39pIIrB0R
exit 0
fi
done
systemd.services.${serviceName} = { echo "Found output instead: $OUTPUT"
script = '' echo "Failed to load key: $OUTPUT" | sendmail
set -eu exit 1
${serviceScript} '');
''; };
serviceConfig = { backup = {
Type = "oneshot"; when = "*-*-* 04:00:00";
User = "root"; script = toString (pkgs.writeShellScript "script" ''
${./scripts/files/backup.sh}
${pkgs.curl}/bin/curl https://uptime.thomasave.be/api/push/R6iJcWqGp0
'');
}; };
};
}; };
disk_timer = mkTimer {
name = "disk_check";
interval = "1h";
serviceScript = toString (pkgs.writeShellScript "disk_check" ''
REPORT_EMAIL=email@thomasave.be
ZPOOL_STATUS=$(zpool status -x)
if [ "$ZPOOL_STATUS" = "all pools are healthy" ] || [ "$ZPOOL_STATUS" = "no pools available" ]
then
printf 0 > /var/db/zpool.status
else
if [ "$(cat /var/db/zpool.status)" -eq 0 ]
then
zpool status | mail -s "ZPOOL NOT HEALTHY" $REPORT_EMAIL
printf 1 > /var/db/zpool.status
fi
fi
'');
};
in in
{ {
home.username = "server"; home.username = "server";
@ -54,6 +93,7 @@ in
settings.use-xdg-base-directories = true; settings.use-xdg-base-directories = true;
}; };
xdg.enable = true;
# home.profileDirectory = "${config.xdg.stateHome}/nix/profile"; # home.profileDirectory = "${config.xdg.stateHome}/nix/profile";
imports = [ imports = [
(import ./common.nix { inherit inputs config pkgs; }) (import ./common.nix { inherit inputs config pkgs; })
@ -62,5 +102,9 @@ in
home.sessionVariables = { home.sessionVariables = {
NIX_PATH = "${config.xdg.stateHome}/nix/profiles/channels/"; NIX_PATH = "${config.xdg.stateHome}/nix/profiles/channels/";
LANG = "en_US.UTF-8"; LANG = "en_US.UTF-8";
XDG_RUNTIME_DIR="/run/user/$(id -u)";
}; };
systemd.user.services = pkgs.lib.mapAttrs mkService scripts;
systemd.user.timers = pkgs.lib.mapAttrs mkTimer scripts;
} }