NixOS/modules/scripts/base.nix

52 lines
1.6 KiB
Nix

{ 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}";
};
};
timers."${name}" = {
enable = true;
description = description;
wantedBy = [ "timers.target" ];
timerConfig = { OnCalendar = timer; };
};
};
};
}