From e0034971f9def16bbc32124147787bc0f09f0e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Fri, 14 Apr 2023 23:41:51 +0200 Subject: [PATCH] comodoro: add module --- modules/misc/news.nix | 10 +++ modules/modules.nix | 2 + modules/programs/comodoro.nix | 31 ++++++++ modules/services/comodoro.nix | 70 +++++++++++++++++++ tests/default.nix | 2 + tests/modules/programs/comodoro/comodoro.nix | 35 ++++++++++ tests/modules/programs/comodoro/default.nix | 1 + tests/modules/programs/comodoro/expected.toml | 13 ++++ tests/modules/services/comodoro/comodoro.nix | 16 +++++ tests/modules/services/comodoro/default.nix | 1 + .../services/comodoro/expected.service | 12 ++++ 11 files changed, 193 insertions(+) create mode 100644 modules/programs/comodoro.nix create mode 100644 modules/services/comodoro.nix create mode 100644 tests/modules/programs/comodoro/comodoro.nix create mode 100644 tests/modules/programs/comodoro/default.nix create mode 100644 tests/modules/programs/comodoro/expected.toml create mode 100644 tests/modules/services/comodoro/comodoro.nix create mode 100644 tests/modules/services/comodoro/default.nix create mode 100644 tests/modules/services/comodoro/expected.service diff --git a/modules/misc/news.nix b/modules/misc/news.nix index cec6a508d..06a4fbcc1 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1101,6 +1101,16 @@ in A new module is available: 'programs.git-credential-oauth'. ''; } + + { + time = "2023-06-14T21:41:22+00:00"; + message = '' + Two new modules are available: + + - 'programs.comodoro' and + - 'services.comodoro' + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index f9c2be546..bb80c618d 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -68,6 +68,7 @@ let ./programs/btop.nix ./programs/chromium.nix ./programs/command-not-found/command-not-found.nix + ./programs/comodoro.nix ./programs/dircolors.nix ./programs/direnv.nix ./programs/discocss.nix @@ -234,6 +235,7 @@ let ./services/cbatticon.nix ./services/clipman.nix ./services/clipmenu.nix + ./services/comodoro.nix ./services/copyq.nix ./services/devilspie2.nix ./services/dropbox.nix diff --git a/modules/programs/comodoro.nix b/modules/programs/comodoro.nix new file mode 100644 index 000000000..5c9fa358f --- /dev/null +++ b/modules/programs/comodoro.nix @@ -0,0 +1,31 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.programs.comodoro; + tomlFormat = pkgs.formats.toml { }; + +in { + meta.maintainers = with lib.hm.maintainers; [ soywod ]; + + options.programs.comodoro = { + enable = lib.mkEnableOption "Comodoro, a CLI to manage your time"; + + package = lib.mkPackageOption pkgs "comodoro" { }; + + settings = lib.mkOption { + type = lib.types.submodule { freeformType = tomlFormat.type; }; + default = { }; + description = '' + Comodoro configuration. + See for supported values. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + xdg.configFile."comodoro/config.toml".source = + tomlFormat.generate "comodoro-config.toml" cfg.settings; + }; +} diff --git a/modules/services/comodoro.nix b/modules/services/comodoro.nix new file mode 100644 index 000000000..cf298ce82 --- /dev/null +++ b/modules/services/comodoro.nix @@ -0,0 +1,70 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.services.comodoro; + + args = with cfg; { + inherit preset; + protocols = if lib.isList protocols then + lib.concatStringsSep " " protocols + else + protocols; + }; + +in { + meta.maintainers = with lib.hm.maintainers; [ soywod ]; + + options.services.comodoro = { + enable = lib.mkEnableOption "Comodoro server"; + + package = lib.mkPackageOption pkgs "comodoro" { }; + + environment = lib.mkOption { + type = with lib.types; attrsOf str; + default = { }; + example = lib.literalExpression '' + { + "PASSWORD_STORE_DIR" = "~/.password-store"; + } + ''; + description = '' + Extra environment variables to be exported in the service. + ''; + }; + + preset = lib.mkOption { + type = lib.types.nonEmptyStr; + description = '' + Use configuration from the given preset as defined in the configuration file. + ''; + }; + + protocols = lib.mkOption { + type = with lib.types; nonEmptyListOf nonEmptyStr; + description = '' + Define protocols the server should use to accept requests. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + systemd.user.services.comodoro = { + Unit = { + Description = "Comodoro server"; + After = [ "network.target" ]; + }; + Install = { WantedBy = [ "default.target" ]; }; + Service = { + ExecStart = with args; + "${cfg.package}/bin/comodoro server start ${preset} ${protocols}"; + ExecSearchPath = "/bin"; + Restart = "always"; + RestartSec = 10; + Environment = + lib.mapAttrsToList (key: val: "${key}=${val}") cfg.environment; + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index fa202ff23..0f2443ec0 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -66,6 +66,7 @@ import nmt { ./modules/programs/broot ./modules/programs/browserpass ./modules/programs/btop + ./modules/programs/comodoro ./modules/programs/dircolors ./modules/programs/direnv ./modules/programs/emacs @@ -186,6 +187,7 @@ import nmt { ./modules/services/borgmatic ./modules/services/cachix-agent ./modules/services/clipman + ./modules/services/comodoro ./modules/services/devilspie2 ./modules/services/dropbox ./modules/services/emacs diff --git a/tests/modules/programs/comodoro/comodoro.nix b/tests/modules/programs/comodoro/comodoro.nix new file mode 100644 index 000000000..37f43b448 --- /dev/null +++ b/tests/modules/programs/comodoro/comodoro.nix @@ -0,0 +1,35 @@ +{ ... }: + +{ + programs.comodoro = { + enable = true; + settings = { + test-preset = { + cycles = [ + { + name = "Work"; + duration = 1500; + } + { + name = "Rest"; + duration = 500; + } + ]; + + tcp-host = "localhost"; + tcp-port = 8080; + + on-server-start = "echo server started"; + on-timer-stop = "echo timer stopped"; + on-work-begin = "echo work cycle began"; + }; + }; + }; + + test.stubs.comodoro = { }; + + nmt.script = '' + assertFileExists home-files/.config/comodoro/config.toml + assertFileContent home-files/.config/comodoro/config.toml ${./expected.toml} + ''; +} diff --git a/tests/modules/programs/comodoro/default.nix b/tests/modules/programs/comodoro/default.nix new file mode 100644 index 000000000..23f4b1750 --- /dev/null +++ b/tests/modules/programs/comodoro/default.nix @@ -0,0 +1 @@ +{ comodoro-program = ./comodoro.nix; } diff --git a/tests/modules/programs/comodoro/expected.toml b/tests/modules/programs/comodoro/expected.toml new file mode 100644 index 000000000..9d0d4c564 --- /dev/null +++ b/tests/modules/programs/comodoro/expected.toml @@ -0,0 +1,13 @@ +[test-preset] +on-server-start = "echo server started" +on-timer-stop = "echo timer stopped" +on-work-begin = "echo work cycle began" +tcp-host = "localhost" +tcp-port = 8080 +[[test-preset.cycles]] +duration = 1500 +name = "Work" + +[[test-preset.cycles]] +duration = 500 +name = "Rest" diff --git a/tests/modules/services/comodoro/comodoro.nix b/tests/modules/services/comodoro/comodoro.nix new file mode 100644 index 000000000..35974ecd7 --- /dev/null +++ b/tests/modules/services/comodoro/comodoro.nix @@ -0,0 +1,16 @@ +{ ... }: + +{ + services.comodoro = { + enable = true; + preset = "preset"; + protocols = [ "tcp" ]; + }; + + test.stubs.comodoro = { }; + + nmt.script = '' + serviceFile=$(normalizeStorePaths home-files/.config/systemd/user/comodoro.service) + assertFileContent "$serviceFile" ${./expected.service} + ''; +} diff --git a/tests/modules/services/comodoro/default.nix b/tests/modules/services/comodoro/default.nix new file mode 100644 index 000000000..84281f3a4 --- /dev/null +++ b/tests/modules/services/comodoro/default.nix @@ -0,0 +1 @@ +{ comodoro-service = ./comodoro.nix; } diff --git a/tests/modules/services/comodoro/expected.service b/tests/modules/services/comodoro/expected.service new file mode 100644 index 000000000..346619d52 --- /dev/null +++ b/tests/modules/services/comodoro/expected.service @@ -0,0 +1,12 @@ +[Install] +WantedBy=default.target + +[Service] +ExecSearchPath=/bin +ExecStart=@comodoro@/bin/comodoro server start preset tcp +Restart=always +RestartSec=10 + +[Unit] +After=network.target +Description=Comodoro server