120 lines
3.3 KiB
Nix
120 lines
3.3 KiB
Nix
_: {
|
|
_class = "clan.service";
|
|
|
|
manifest = {
|
|
name = "sleep";
|
|
description = "Let your machines sleep while you sleep. Give them a break too :0";
|
|
categories = [ "System" ];
|
|
readme = ''
|
|
Leverages the `rtcwake` command within systemd services to let the machines sleep within a timespan.
|
|
|
|
# Usage
|
|
|
|
Can have multiple instances.
|
|
|
|
```nix
|
|
# clan.nix
|
|
inventory.instances = [
|
|
week-night-sleep = {
|
|
module.name = "@schallerclan/sleep";
|
|
module.input = "schallerclan";
|
|
|
|
roles.default.tags = [ "night-sleep" ];
|
|
roles.default.settings = {
|
|
mode = "poweroff";
|
|
sleepAt = "Sun..Thu 23:00";
|
|
wakeAt = "tomorrow 06:00";
|
|
};
|
|
};
|
|
week-night-sleep-2 = {
|
|
module.name = "@schallerclan/sleep";
|
|
module.input = "schallerclan";
|
|
|
|
roles.default.tags = [ "night-sleep" ];
|
|
roles.default.settings = {
|
|
mode = "poweroff";
|
|
sleepAt = "Mon..Fri 00..05:00";
|
|
wakeAt = "06:00";
|
|
};
|
|
};
|
|
weekend-night-sleep = {
|
|
module.name = "@schallerclan/sleep";
|
|
module.input = "schallerclan";
|
|
|
|
roles.default.tags = [ "night-sleep" ];
|
|
roles.default.settings = {
|
|
mode = "poweroff";
|
|
sleepAt = "Sat,Sun 01..05:00";
|
|
wakeAt = "07:00";
|
|
};
|
|
};
|
|
];
|
|
```
|
|
'';
|
|
};
|
|
|
|
roles.default = {
|
|
description = "Machines to let sleep.";
|
|
interface =
|
|
{ lib, ... }:
|
|
{
|
|
options = with lib.types; {
|
|
mode = lib.mkOption {
|
|
type = nullOr (enum [
|
|
"halt"
|
|
"poweroff"
|
|
"sleep"
|
|
"suspend"
|
|
"hibernate"
|
|
"hybrid-sleep"
|
|
"suspend-then-hibernate"
|
|
]);
|
|
default = "sleep";
|
|
description = "Which `systemctl` standby state.";
|
|
};
|
|
sleepAt = lib.mkOption {
|
|
type = str;
|
|
description = "When to sleep. Takes a `systemd.time(7)` calendar event.";
|
|
example = "Mon..Fri 23:00";
|
|
};
|
|
wakeAt = lib.mkOption {
|
|
type = str;
|
|
description = "When to wake. Takes a `date --date` timestamp.";
|
|
example = "tomorrow 07:00";
|
|
};
|
|
};
|
|
};
|
|
perInstance =
|
|
{ instanceName, settings, ... }:
|
|
{
|
|
nixosModule =
|
|
{ lib, pkgs, ... }:
|
|
{
|
|
systemd.timers.${instanceName} = {
|
|
description = "entering ${instanceName}";
|
|
wantedBy = [ "timers.target" ];
|
|
timerConfig = {
|
|
OnCalendar = settings.sleepAt;
|
|
WakeSystem = true;
|
|
};
|
|
};
|
|
systemd.services.${instanceName} = {
|
|
description = "entering ${instanceName}";
|
|
serviceConfig.Type = "oneshot";
|
|
path = [
|
|
pkgs.util-linux
|
|
pkgs.coreutils
|
|
pkgs.systemdMinimal
|
|
];
|
|
script = ''
|
|
WAKE_AT="$(date --date '${settings.wakeAt}' +%s)"
|
|
rtcwake --time "$WAKE_AT" --mode no ${
|
|
lib.optionalString (settings.mode != null) "&& systemctl ${settings.mode}"
|
|
}
|
|
'';
|
|
};
|
|
};
|
|
};
|
|
};
|
|
}
|