Last active
January 15, 2025 14:21
-
-
Save dcode/7e6a25609535aa81aef7446c78317e72 to your computer and use it in GitHub Desktop.
A Nix home-manager module to setup a user systemd service to enable/disable the caffeine extension at start and end of my workday.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ config, lib, pkgs, ... }: | |
let | |
cfg = config.services.caffeinated-workday; | |
# Check if gnome-shell package is installed | |
hasGnomeShell = pkgs.runCommand "check-gnome-shell" { } '' | |
if ${pkgs.coreutils}/bin/which ${pkgs.gnome.gnome-shell}/bin/gnome-shell; then | |
touch $out | |
fi | |
'' != null; | |
# TODO add test that the extension is currently enabled, or maybe install it? | |
# _enabledGSExts = dconf.get "org/gnome/shell/enabled-extensions"; | |
# caffeineExtEnabled = builtins.elem "[email protected]" _enabledGSExts; | |
in { | |
options.services.caffeinated-workday = { | |
enable = lib.mkEnableOption "Caffeinated Workday service"; | |
workdayStart = lib.mkOption { | |
type = lib.types.str; | |
default = "Mon..Fri 07:00:00"; | |
example = "Mon..Fri 07:00:00 US/Central"; | |
description = '' | |
The calendar event format pattern to indicate the start of your workday | |
to enable the caffeine extension. | |
Takes a systemd calendar event string, see {manpage}`systemd.time(7)`. | |
Will default to M-F at 7am in the system's local time, if omitted. | |
''; | |
}; | |
workdayEnd = lib.mkOption { | |
type = lib.types.str; | |
default = "Mon..Fri 17:00:00"; | |
example = "Mon..Fri 17:00:00 US/Central"; | |
description = '' | |
The calendar event format pattern to indicate the end of your workday | |
to disable the caffeine extension. | |
Takes a systemd calendar event string, see {manpage}`systemd.time(7)`. | |
Will default to M-F at 5pm in the system's local time, if omitted. | |
''; | |
}; | |
}; | |
config = lib.mkIf cfg.enable { | |
assertions = [ | |
(lib.hm.assertions.assertPlatform "services.caffeinated-workday" pkgs | |
lib.platforms.linux) | |
{ | |
assertion = hasGnomeShell; | |
message = '' | |
This service only works on systems that have Gnome-Shell installed. | |
''; | |
} | |
]; | |
systemd.user = { | |
services = let | |
schemaDir = | |
"\${XDG_DATA_HOME}/gnome-shell/extensions/[email protected]/schemas/"; | |
gExtension = "org.gnome.shell.extensions.caffeine"; | |
in { | |
enable-caffeine = { | |
Unit = { | |
Description = | |
"Enable Caffeine extension to prevent desktop from sleeping."; | |
After = "multi-user.target"; | |
Conflicts = "disable-caffeine.service"; | |
}; | |
Service = { | |
Type = "oneshot"; | |
RemainAfterExit = "yes"; | |
ExecStart = '' | |
gsettings --schemadir "${schemaDir}" set ${gExtension} toggle-state true''; | |
Environment = [ "XDG_DATA_HOME=%h/.local/share" ]; | |
}; | |
Install = { WantedBy = [ "default.target" ]; }; | |
}; | |
disable-caffeine = { | |
Unit = { | |
Description = | |
"Disable Caffeine extension to allow desktop to sleep."; | |
After = "multi-user.target"; | |
Conflicts = "enable-caffeine.service"; | |
}; | |
Service = { | |
Type = "oneshot"; | |
RemainAfterExit = "yes"; | |
ExecStart = '' | |
gsettings --schemadir "${schemaDir}" set ${gExtension} toggle-state false''; | |
Environment = [ "XDG_DATA_HOME=%h/.local/share" ]; | |
}; | |
Install = { WantedBy = [ "default.target" ]; }; | |
}; | |
}; | |
timers = { | |
enable-caffeine = { | |
Unit = { | |
Description = "Timer to enable caffeine at start of workday"; | |
PartOf = [ "enable-caffeine.service" ]; | |
}; | |
Timer = { | |
Persistent = "true"; | |
OnCalendar = cfg.workdayStart; | |
}; | |
Install = { WantedBy = ["default.target"];}; | |
}; | |
disable-caffeine = { | |
Unit = { | |
Description = "Timer to disable caffeine at end of workday"; | |
PartOf = [ "disable-caffeine.service" ]; | |
}; | |
Timer = { OnCalendar = cfg.workdayEnd; }; | |
Install = { WantedBy = ["default.target"];}; | |
}; | |
}; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment