From 9f46d516fa13df70768a3211827d14f456fa6e85 Mon Sep 17 00:00:00 2001 From: Tad Fisher Date: Thu, 20 Feb 2020 23:30:59 -0800 Subject: [PATCH] services.lieer: add module Add 'services.lieer', which generates systemd timer and service units to synchronize a Gmail account with lieer. Per-account configuration lives in 'accounts.email.accounts..lieer.sync'. --- modules/accounts/email.nix | 6 +- modules/misc/news.nix | 8 +++ modules/modules.nix | 1 + modules/services/lieer-accounts.nix | 25 ++++++++ modules/services/lieer.nix | 62 +++++++++++++++++++ tests/default.nix | 1 + tests/modules/services/lieer/default.nix | 1 + .../lieer/lieer-service-expected.service | 8 +++ .../lieer/lieer-service-expected.timer | 9 +++ .../modules/services/lieer/lieer-service.nix | 35 +++++++++++ 10 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 modules/services/lieer-accounts.nix create mode 100644 modules/services/lieer.nix create mode 100644 tests/modules/services/lieer/default.nix create mode 100644 tests/modules/services/lieer/lieer-service-expected.service create mode 100644 tests/modules/services/lieer/lieer-service-expected.timer create mode 100644 tests/modules/services/lieer/lieer-service.nix diff --git a/modules/accounts/email.nix b/modules/accounts/email.nix index f45e57472af5..b347e5bb1a2f 100644 --- a/modules/accounts/email.nix +++ b/modules/accounts/email.nix @@ -384,7 +384,7 @@ in }; accounts = mkOption { - type = types.attrsOf (types.submodule [ + type = types.attrsOf (types.submodule ([ mailAccountOpts (import ../programs/alot-accounts.nix pkgs) (import ../programs/astroid-accounts.nix) @@ -395,7 +395,9 @@ in (import ../programs/neomutt-accounts.nix) (import ../programs/notmuch-accounts.nix) (import ../programs/offlineimap-accounts.nix) - ]); + ] ++ optionals pkgs.stdenv.hostPlatform.isLinux [ + (import ../services/lieer-accounts.nix) + ])); default = {}; description = "List of email accounts."; }; diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 048f9cbe5582..6a5cce7e085a 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1373,6 +1373,14 @@ in A new module is available: 'programs.lieer'. ''; } + + { + time = "2020-03-07T14:12:50+00:00"; + condition = hostPlatform.isLinux; + message = '' + A new module is available: 'services.lieer'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 3d1283e380c4..bca1e8ed9ca4 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -128,6 +128,7 @@ let (loadModule ./services/kdeconnect.nix { }) (loadModule ./services/keepassx.nix { }) (loadModule ./services/keybase.nix { }) + (loadModule ./services/lieer.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/lorri.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/mbsync.nix { }) (loadModule ./services/mpd.nix { }) diff --git a/modules/services/lieer-accounts.nix b/modules/services/lieer-accounts.nix new file mode 100644 index 000000000000..187f7dff9805 --- /dev/null +++ b/modules/services/lieer-accounts.nix @@ -0,0 +1,25 @@ +{ lib, ... }: + +with lib; + +{ + options.lieer.sync = { + enable = mkEnableOption "lieer synchronization service"; + + frequency = mkOption { + type = types.str; + default = "*:0/5"; + description = '' + How often to synchronize the account. + + This value is passed to the systemd timer configuration as the + onCalendar option. See + + systemd.time + 7 + + for more information about the format. + ''; + }; + }; +} diff --git a/modules/services/lieer.nix b/modules/services/lieer.nix new file mode 100644 index 000000000000..35ac7d643cca --- /dev/null +++ b/modules/services/lieer.nix @@ -0,0 +1,62 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.lieer; + + syncAccounts = filter (a: a.lieer.enable && a.lieer.sync.enable) + (attrValues config.accounts.email.accounts); + + escapeUnitName = name: + let + good = upperChars ++ lowerChars ++ stringToCharacters "0123456789-_"; + subst = c: if any (x: x == c) good then c else "-"; + in stringAsChars subst name; + + serviceUnit = account: { + name = escapeUnitName "lieer-${account.name}"; + value = { + Unit = { + Description = "lieer Gmail synchronization for ${account.name}"; + ConditionPathExists = "${account.maildir.absPath}/.gmailieer.json"; + }; + + Service = { + Type = "oneshot"; + ExecStart = "${pkgs.gmailieer}/bin/gmi sync"; + WorkingDirectory = account.maildir.absPath; + }; + }; + }; + + timerUnit = account: { + name = escapeUnitName "lieer-${account.name}"; + value = { + Unit = { + Description = "lieer Gmail synchronization for ${account.name}"; + }; + + Timer = { + OnCalendar = account.lieer.sync.frequency; + RandomizedDelaySec = 30; + }; + + Install = { WantedBy = [ "timers.target" ]; }; + }; + }; + +in { + meta.maintainers = [ maintainers.tadfisher ]; + + options = { + services.lieer.enable = + mkEnableOption "lieer Gmail synchronization service"; + }; + + config = mkIf cfg.enable { + programs.lieer.enable = true; + systemd.user.services = listToAttrs (map serviceUnit syncAccounts); + systemd.user.timers = listToAttrs (map timerUnit syncAccounts); + }; +} diff --git a/tests/default.nix b/tests/default.nix index cc6d7dfa215c..1d8a5132225a 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -51,6 +51,7 @@ import nmt { ./modules/programs/abook ./modules/programs/firefox ./modules/programs/getmail + ./modules/services/lieer ./modules/programs/rofi ./modules/services/polybar ./modules/services/sxhkd diff --git a/tests/modules/services/lieer/default.nix b/tests/modules/services/lieer/default.nix new file mode 100644 index 000000000000..1d6b435a1cac --- /dev/null +++ b/tests/modules/services/lieer/default.nix @@ -0,0 +1 @@ +{ lieer-service = ./lieer-service.nix; } diff --git a/tests/modules/services/lieer/lieer-service-expected.service b/tests/modules/services/lieer/lieer-service-expected.service new file mode 100644 index 000000000000..1110e85c4756 --- /dev/null +++ b/tests/modules/services/lieer/lieer-service-expected.service @@ -0,0 +1,8 @@ +[Service] +ExecStart=@lieer@/bin/gmi sync +Type=oneshot +WorkingDirectory=/home/hm-user/Mail/hm@example.com + +[Unit] +ConditionPathExists=/home/hm-user/Mail/hm@example.com/.gmailieer.json +Description=lieer Gmail synchronization for hm@example.com diff --git a/tests/modules/services/lieer/lieer-service-expected.timer b/tests/modules/services/lieer/lieer-service-expected.timer new file mode 100644 index 000000000000..cb059ea47c4f --- /dev/null +++ b/tests/modules/services/lieer/lieer-service-expected.timer @@ -0,0 +1,9 @@ +[Install] +WantedBy=timers.target + +[Timer] +OnCalendar=*:0/5 +RandomizedDelaySec=30 + +[Unit] +Description=lieer Gmail synchronization for hm@example.com diff --git a/tests/modules/services/lieer/lieer-service.nix b/tests/modules/services/lieer/lieer-service.nix new file mode 100644 index 000000000000..3c9e66397c51 --- /dev/null +++ b/tests/modules/services/lieer/lieer-service.nix @@ -0,0 +1,35 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + imports = [ ../../accounts/email-test-accounts.nix ]; + + config = { + home.username = "hm-user"; + home.homeDirectory = "/home/hm-user"; + + services.lieer.enable = true; + + accounts.email.accounts = { + "hm@example.com".lieer.enable = true; + "hm@example.com".lieer.sync.enable = true; + }; + + nixpkgs.overlays = [ + (self: super: { + gmailieer = super.gmailieer // { outPath = "@lieer@"; }; + }) + ]; + + nmt.script = '' + assertFileExists home-files/.config/systemd/user/lieer-hm-example-com.service + assertFileExists home-files/.config/systemd/user/lieer-hm-example-com.timer + + assertFileContent home-files/.config/systemd/user/lieer-hm-example-com.service \ + ${./lieer-service-expected.service} + assertFileContent home-files/.config/systemd/user/lieer-hm-example-com.timer \ + ${./lieer-service-expected.timer} + ''; + }; +}