workstation virtually...done!

This commit is contained in:
2024-05-11 22:57:41 -06:00
parent bdfbf790ec
commit 58878b21ed
10 changed files with 159 additions and 93 deletions

View File

@@ -1,51 +1,59 @@
{ config, lib, pkgs, ... }: {
options.my.script = {
install = lib.mkEnableOption "Whether to install the script package";
service = lib.mkEnableOption "Whether to enable the script service";
name = lib.mkOption {
type = lib.types.str;
default = "my-script";
description = "Name of the script.";
};
timer = lib.mkOption {
type = lib.types.str;
default = "*:0/10";
description = "Systemd timer schedule.";
};
description = lib.mkOption {
type = lib.types.str;
default = "A service that runs a script.";
description = "Description of the service.";
};
package = lib.mkOption {
type = lib.types.package;
default = pkgs.writeScriptBin "my-script" "echo Hello World";
description = "Package containing the executable script.";
};
};
config = {
users.users.jawz = lib.mkIf config.my.script.install {
packages = [ config.my.script.package ];
};
systemd.user = with config.my.script;
lib.mkIf config.my.script.service {
services."${name}" = {
restartIfChanged = true;
description = description;
wantedBy = [ "default.target" ];
path = [ pkgs.nix package ];
serviceConfig = {
Restart = "on-failure";
RestartSec = 30;
ExecStart = "${package}/bin/${name}";
};
options.my.scripts = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule ({
options = {
enable = lib.mkEnableOption "Whether to enable this script";
install = lib.mkEnableOption "Whether to install the script package";
service = lib.mkEnableOption "Whether to enable the script service";
name = lib.mkOption {
type = lib.types.str;
description = "Name of the script.";
};
timers."${name}" = {
enable = true;
description = description;
wantedBy = [ "timers.target" ];
timerConfig = { OnCalendar = timer; };
timer = lib.mkOption {
type = lib.types.str;
default = "*:0";
description = "Systemd timer schedule.";
};
description = lib.mkOption {
type = lib.types.str;
description = "Description of the service.";
};
package = lib.mkOption {
type = lib.types.package;
description = "Package containing the executable script.";
};
};
}));
default = { };
description = "Configuration for multiple scripts.";
};
config = lib.mkIf (lib.any (s: s.enable) (lib.attrValues config.my.scripts)) {
users.users.jawz.packages = lib.flatten (lib.mapAttrsToList (name: script:
lib.optional (script.enable && script.install) script.package)
config.my.scripts);
systemd.user.services = lib.mapAttrs' (name: script:
lib.nameValuePair "${script.name}"
(lib.mkIf (script.enable && script.service) {
restartIfChanged = true;
description = script.description;
wantedBy = [ "default.target" ];
path = [ pkgs.nix script.package ];
serviceConfig = {
Restart = "on-failure";
RestartSec = 30;
ExecStart = "${script.package}/bin/${script.name}";
};
})) config.my.scripts;
systemd.user.timers = lib.mapAttrs' (name: script:
lib.nameValuePair "${script.name}"
(lib.mkIf (script.enable && script.service) {
enable = true;
description = script.description;
wantedBy = [ "timers.target" ];
timerConfig = { OnCalendar = script.timer; };
})) config.my.scripts;
};
}