From 90a24c97be0126eae79a15b75e1939455197d896 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sat, 5 Aug 2023 22:23:36 +0100 Subject: [PATCH] feat: up[dated the config for the timers and services --- flake.nix | 132 +++++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 67 deletions(-) diff --git a/flake.nix b/flake.nix index 491bea3..dceccf3 100644 --- a/flake.nix +++ b/flake.nix @@ -67,6 +67,44 @@ USERS_LIFETIME = lib.strings.concatStringsSep "," cfg.users.lifetime; USERS_BANNED = lib.strings.concatStringsSep "," cfg.users.banned; }; + + service_name = script: lib.strings.sanitizeDerivationName("${cfg.prefix}${cfg.user}@${script}"); + + # oneshot scripts to run + serviceGenerator = builtins.mapAttrs (script: time: nameValuePair (service_name script) { + description = "Service for ${desc} ${script}"; + wantedBy = [ ]; + after = [ "network-online.target" ]; + environment = environment_config; + + serviceConfig = { + Type = "oneshot"; + DynamicUser = true; + ExecStart = "${self.defaultPackage."${system}"}/bin/${script}"; + EnvironmentFile = "${cfg.envFile}"; + }; + }) scripts; + + # each timer will run the above service + timerGenerator = builtins.mapAttrs (script: time: nameValuePair (service_name script) { + description = "Timer for ${desc} ${script}"; + + wantedBy = [ "timers.target" ]; + partOf = [ "${service_name script}.service" ]; + timerConfig = { + OnCalendar = time; + Unit = "${service_name script}.service"; + Persistent = true; + }; + }) scripts; + + # modify these + scripts = { + "update_groups" = "*-*-* *:00:00"; + "new_users" = "*:0/15"; + }; + + in { options.services."${package_name}" = { enable = mkEnableOption "enable ${package_name}"; @@ -144,6 +182,13 @@ default = "/etc/skynet_${package_name}"; description = "The home for the user"; }; + + prefix = mkOption rec { + type = types.str; + default = "skynet_"; + example = default; + description = "The prefix used to name service/folders"; + }; }; @@ -158,75 +203,28 @@ group = "${cfg.user}"; }; - systemd.services."${cfg.user}" = { - description = desc; - wantedBy = [ "multi-user.target" ]; - after = [ "network-online.target" ]; - wants = [ ]; - environment = environment_config; - - serviceConfig = { - # because we are storing data we need a home for it - User = "${cfg.user}"; - Group = "${cfg.user}"; - Restart = "always"; - ExecStart = "${self.defaultPackage."${system}"}/bin/${package_name}"; - EnvironmentFile = "${cfg.envFile}"; + systemd.services = { + # main service + "${cfg.user}" = { + description = desc; + wantedBy = [ "multi-user.target" ]; + after = [ "network-online.target" ]; + wants = [ ]; + environment = environment_config; + + serviceConfig = { + # because we are storing data we need a home for it + User = "${cfg.user}"; + Group = "${cfg.user}"; + Restart = "always"; + ExecStart = "${self.defaultPackage."${system}"}/bin/${package_name}"; + EnvironmentFile = "${cfg.envFile}"; + }; }; - }; + } // serviceGenerator; - # for updating the data - systemd.services."${cfg.user}_update" = { - description = "${desc} Update groups"; - wantedBy = [ ]; - after = [ "network-online.target" ]; - environment = environment_config; - - serviceConfig = { - Type = "oneshot"; - DynamicUser = true; - ExecStart = "${self.defaultPackage."${system}"}/bin/update_groups"; - EnvironmentFile = "${cfg.envFile}"; - }; - }; - - systemd.timers."${cfg.user}_update" = { - description = "Run the update script for ${desc}"; - - wantedBy = [ "timers.target" ]; - partOf = [ "${cfg.user}_update.service" ]; - timerConfig = { - # every hour - OnCalendar = "*-*-* *:00:00"; - Unit = "${cfg.user}_update.service"; - }; - }; - - # for new users - systemd.services."${cfg.user}_new_users" = { - description = "${desc} Get new users"; - wantedBy = [ ]; - after = [ "network-online.target" ]; - environment = environment_config; - - serviceConfig = { - Type = "oneshot"; - DynamicUser = true; - ExecStart = "${self.defaultPackage."${system}"}/bin/new_users"; - EnvironmentFile = "${cfg.envFile}"; - }; - }; - - systemd.timers."${cfg.user}_new_users" = { - description = "Run the new users script for ${desc}"; - wantedBy = [ "timers.target" ]; - partOf = [ "${cfg.user}_new_users.service" ]; - timerConfig = { - # every 15 min - OnCalendar = "*:0/15"; - Unit = "${cfg.user}_new_users.service"; - }; - }; + # timers to run the above services + systemd.timers = timerGenerator; };