From 58879676ef879d18cfcec3cfaf12dd2c0d7bb376 Mon Sep 17 00:00:00 2001 From: Evan Danaher Date: Tue, 24 Oct 2017 13:13:07 -0400 Subject: [PATCH] Abstract out utils.simple-timer and use it. --- chileh-backups.nix | 31 ++++++------------------------- host/hosts.nix | 9 +-------- host/utils.nix | 3 ++- services/angell-classes.nix | 10 ++-------- services/disk-mon.nix | 10 ++-------- utils.nix | 10 ++++++++++ 6 files changed, 23 insertions(+), 50 deletions(-) create mode 100644 utils.nix diff --git a/chileh-backups.nix b/chileh-backups.nix index 8e36267..9993647 100644 --- a/chileh-backups.nix +++ b/chileh-backups.nix @@ -1,6 +1,8 @@ { config, lib, pkgs, ... }: -let two-days = 60 * 60 * 24 * 2 - 60; in +let two-days = 60 * 60 * 24 * 2 - 60; + utils = import ./utils.nix; +in { config = lib.mkIf config.host.chileh-backups.enable { services.periodimail.intervals = [ two-days ]; @@ -20,14 +22,7 @@ let two-days = 60 * 60 * 24 * 2 - 60; in }; }; - systemd.timers.backup-deretheni = { - description = "Backup deretheni daily"; - wantedBy = [ "timers.target" ]; - timerConfig = { - OnCalendar = "daily"; - Persistent = true; - }; - }; + systemd.timers.backup-deretheni = utils.simple-timer "daily" "Backup deretheni daily"; systemd.services.snapshot-chileh-edanaher = { description = "Snapshot chileh homedir"; @@ -39,14 +34,7 @@ let two-days = 60 * 60 * 24 * 2 - 60; in }; }; - systemd.timers.snapshot-chileh-edanaher = { - description = "Snapshot chileh homedir hourly"; - wantedBy = [ "timers.target" ]; - timerConfig = { - OnCalendar = "hourly"; - Persistent = true; - }; - }; + systemd.timers.snapshot-chileh-edanaher = utils.simple-timer "hourly" "Snapshot chileh homedir hourly"; systemd.services.snapshot-chileh-edanaher-to-borg = { description = "Copy chileh homedir snapshots to borg"; @@ -57,14 +45,7 @@ let two-days = 60 * 60 * 24 * 2 - 60; in }; }; - systemd.timers.snapshot-chileh-edanaher-to-borg = { - description = "Copy chileh homedir snapshots to borg daily"; - wantedBy = [ "timers.target" ]; - timerConfig = { - OnCalendar = "daily"; - Persistent = true; - }; - }; + systemd.timers.snapshot-chileh-edanaher-to-borg = utils.simple-timer "daily" "Copy chileh homedir snapshots to borg daily"; }; options = { diff --git a/host/hosts.nix b/host/hosts.nix index e405f2a..3de324b 100644 --- a/host/hosts.nix +++ b/host/hosts.nix @@ -33,14 +33,7 @@ let }; }; - systemd.timers.check-edanaher-mail = { - description = "Check mail for edanaher every half hour"; - wantedBy = [ "timers.target" ]; - timerConfig = { - OnCalendar = "*-*-* *:00,30:00"; - Persistent = true; - }; - }; + systemd.timers.check-edanaher-mail = utils.simple-timer "*-*-* *:00,30:00" "Check mail for edanaher every half hour"; host.monitor-disks = { "/" = 90; diff --git a/host/utils.nix b/host/utils.nix index 9f9330c..d8a3e65 100644 --- a/host/utils.nix +++ b/host/utils.nix @@ -1,6 +1,7 @@ { lib }: -{ +let parent = import ../utils.nix; in +parent // { select = v: set: let all = lib.mapAttrs (name: data: lib.mkIf (name == v) data) set; in lib.mkMerge (builtins.attrValues all); } diff --git a/services/angell-classes.nix b/services/angell-classes.nix index f279c56..7d6e699 100644 --- a/services/angell-classes.nix +++ b/services/angell-classes.nix @@ -8,6 +8,7 @@ let angell-path = "/var/run/angell-classes"; ${update-script} > ${angell-path}/index.html.tmp mv ${angell-path}/index.html.tmp ${angell-path}/index.html ''; + utils = import ../utils.nix; in { config = lib.mkIf config.host.angell-classes.enable { @@ -33,14 +34,7 @@ in }; }; - systemd.timers.update-angell-classes= { - description = "Scrape updates for Angell classes daily"; - wantedBy = [ "timers.target" ]; - timerConfig = { - OnCalendar = "daily"; - Persistent = true; - }; - }; + systemd.timers.update-angell-classes = utils.simple-timer "daily" "Scrape updates for Angell classes daily"; }; options = { host.angell-classes.enable = lib.mkOption { diff --git a/services/disk-mon.nix b/services/disk-mon.nix index ac308aa..e1e84bb 100644 --- a/services/disk-mon.nix +++ b/services/disk-mon.nix @@ -1,6 +1,7 @@ {config, lib, pkgs, ...}: let + utils = import ../utils.nix; escapeSlash = builtins.replaceStrings [ "/" ] [ "_" ]; mon-script-template = builtins.readFile ./disk-mon.sh; mon-script-text = disk: usage: builtins.replaceStrings [ "$DISK" "$USAGE" ] [ disk usage ] mon-script-template; @@ -17,14 +18,7 @@ let }; }; - mon-timer-for = disk: usage: { - description = "Monitor usage for ${disk} at ${toString usage} daily"; - wantedBy = [ "timers.target" ]; - timerConfig = { - OnCalendar = "daily"; - Persistent = true; - }; - }; + mon-timer-for = disk: usage: utils.simple-timer "daily" "Monitor usage for ${disk} at ${toString usage} daily"; services = lib.mapAttrs' (disk: usage: lib.nameValuePair ("monitor-disk-" + escapeSlash disk) (mon-service-for disk usage)) config.host.monitor-disks; timers = lib.mapAttrs' (disk: usage: lib.nameValuePair ("monitor-disk-" + escapeSlash disk) (mon-timer-for disk usage)) config.host.monitor-disks; diff --git a/utils.nix b/utils.nix new file mode 100644 index 0000000..87c6c97 --- /dev/null +++ b/utils.nix @@ -0,0 +1,10 @@ +{ + simple-timer = interval: description: { + description = description; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = interval; + Persistent = true; + }; + }; +}