From 0483ce0eeeb7c859e58a287d6f1a0d4e959efffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gaspard?= Date: Sat, 27 Oct 2018 18:33:43 +0900 Subject: [PATCH] rss2email module: init Also adding `system-sendmail` package for sharing the code with other modules or packages needing it. --- nixos/modules/misc/ids.nix | 2 + nixos/modules/module-list.nix | 1 + nixos/modules/services/mail/rss2email.nix | 136 ++++++++++++++++++ nixos/tests/all-tests.nix | 1 + nixos/tests/common/webroot/news-rss.xml | 15 ++ nixos/tests/rss2email.nix | 66 +++++++++ .../feedreaders/rss2email/default.nix | 9 +- pkgs/servers/mail/system-sendmail/default.nix | 36 +++++ pkgs/top-level/all-packages.nix | 2 + 9 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 nixos/modules/services/mail/rss2email.nix create mode 100644 nixos/tests/common/webroot/news-rss.xml create mode 100644 nixos/tests/rss2email.nix create mode 100644 pkgs/servers/mail/system-sendmail/default.nix diff --git a/nixos/modules/misc/ids.nix b/nixos/modules/misc/ids.nix index aff562c00eb1..9c6e9a8e3bb1 100644 --- a/nixos/modules/misc/ids.nix +++ b/nixos/modules/misc/ids.nix @@ -336,6 +336,7 @@ solr = 309; alerta = 310; minetest = 311; + rss2email = 312; # When adding a uid, make sure it doesn't match an existing gid. And don't use uids above 399! @@ -632,6 +633,7 @@ solr = 309; alerta = 310; minetest = 311; + rss2email = 312; # When adding a gid, make sure it doesn't match an existing # uid. Users and groups with the same name should have equal diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 5e50a105e1b2..17b683231224 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -329,6 +329,7 @@ ./services/mail/postgrey.nix ./services/mail/spamassassin.nix ./services/mail/rspamd.nix + ./services/mail/rss2email.nix ./services/mail/rmilter.nix ./services/mail/nullmailer.nix ./services/misc/airsonic.nix diff --git a/nixos/modules/services/mail/rss2email.nix b/nixos/modules/services/mail/rss2email.nix new file mode 100644 index 000000000000..5f3b2877008f --- /dev/null +++ b/nixos/modules/services/mail/rss2email.nix @@ -0,0 +1,136 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.rss2email; +in { + + ###### interface + + options = { + + services.rss2email = { + + enable = mkOption { + type = types.bool; + default = false; + description = "Whether to enable rss2email."; + }; + + to = mkOption { + type = types.str; + description = "Mail address to which to send emails"; + }; + + interval = mkOption { + type = types.str; + default = "12h"; + description = "How often to check the feeds, in systemd interval format"; + }; + + config = mkOption { + type = with types; attrsOf (either str (either int bool)); + default = {}; + description = '' + The configuration to give rss2email. + + Default will use system-wide sendmail to send the + email. This is rss2email's default when running + r2e new. + + This set contains key-value associations that will be set in the + [DEFAULT] block along with the + to parameter. + + See + https://github.com/rss2email/rss2email/blob/master/r2e.1 + for more information on which parameters are accepted. + ''; + }; + + feeds = mkOption { + description = "The feeds to watch."; + type = types.attrsOf (types.submodule { + options = { + url = mkOption { + type = types.str; + description = "The URL at which to fetch the feed."; + }; + + to = mkOption { + type = with types; nullOr str; + default = null; + description = '' + Email address to which to send feed items. + + If null, this will not be set in the + configuration file, and rss2email will make it default to + rss2email.to. + ''; + }; + }; + }); + }; + }; + + }; + + + ###### implementation + + config = mkIf cfg.enable { + users.groups = { + rss2email.gid = config.ids.gids.rss2email; + }; + + users.users = { + rss2email = { + description = "rss2email user"; + uid = config.ids.uids.rss2email; + group = "rss2email"; + }; + }; + + services.rss2email.config.to = cfg.to; + + systemd.services.rss2email = let + conf = pkgs.writeText "rss2email.cfg" (lib.generators.toINI {} ({ + DEFAULT = cfg.config; + } // lib.mapAttrs' (name: feed: nameValuePair "feed.${name}" ( + { inherit (feed) url; } // + lib.optionalAttrs (feed.to != null) { inherit (feed) to; } + )) cfg.feeds + )); + in + { + preStart = '' + mkdir -p /var/rss2email + chmod 700 /var/rss2email + + cp ${conf} /var/rss2email/conf.cfg + if [ ! -f /var/rss2email/db.json ]; then + echo '{"version":2,"feeds":[]}' > /var/rss2email/db.json + fi + + chown -R rss2email:rss2email /var/rss2email + ''; + path = [ pkgs.system-sendmail ]; + serviceConfig = { + ExecStart = + "${pkgs.rss2email}/bin/r2e -c /var/rss2email/conf.cfg -d /var/rss2email/db.json run"; + User = "rss2email"; + PermissionsStartOnly = "true"; + }; + }; + + systemd.timers.rss2email = { + partOf = [ "rss2email.service" ]; + wantedBy = [ "timers.target" ]; + timerConfig.OnBootSec = "0"; + timerConfig.OnUnitActiveSec = cfg.interval; + }; + }; + + meta.maintainers = with lib.maintainers; [ ekleog ]; +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 754a8ff20b26..e53e483e0406 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -179,6 +179,7 @@ in radicale = handleTest ./radicale.nix {}; redmine = handleTest ./redmine.nix {}; rspamd = handleTest ./rspamd.nix {}; + rss2email = handleTest ./rss2email.nix {}; rsyslogd = handleTest ./rsyslogd.nix {}; runInMachine = handleTest ./run-in-machine.nix {}; rxe = handleTest ./rxe.nix {}; diff --git a/nixos/tests/common/webroot/news-rss.xml b/nixos/tests/common/webroot/news-rss.xml new file mode 100644 index 000000000000..28e6fa7da1f3 --- /dev/null +++ b/nixos/tests/common/webroot/news-rss.xml @@ -0,0 +1,15 @@ + +NixOS Newshttps://nixos.orgNews for NixOS, the purely functional Linux distribution.NixOShttps://nixos.org/logo/nixos-logo-only-hires.pnghttps://nixos.org/ + NixOS 18.09 released + https://nixos.org/news.html + + 18.09 Jellyfish logo + + NixOS 18.09 “Jellyfish” has been released, the tenth stable release branch. + See the release notes + for details. You can get NixOS 18.09 ISOs and VirtualBox appliances + from the download page. + For information on how to upgrade from older release branches + to 18.09, check out the + manual section on upgrading. + Sat Oct 06 2018 00:00:00 GMT diff --git a/nixos/tests/rss2email.nix b/nixos/tests/rss2email.nix new file mode 100644 index 000000000000..492d47da9f56 --- /dev/null +++ b/nixos/tests/rss2email.nix @@ -0,0 +1,66 @@ +import ./make-test.nix { + name = "opensmtpd"; + + nodes = { + server = { pkgs, ... }: { + imports = [ common/user-account.nix ]; + services.nginx = { + enable = true; + virtualHosts."127.0.0.1".root = ./common/webroot; + }; + services.rss2email = { + enable = true; + to = "alice@localhost"; + interval = "1"; + config.from = "test@example.org"; + feeds = { + nixos = { url = "http://127.0.0.1/news-rss.xml"; }; + }; + }; + services.opensmtpd = { + enable = true; + extraServerArgs = [ "-v" ]; + serverConfiguration = '' + listen on 127.0.0.1 + action dovecot_deliver mda \ + "${pkgs.dovecot}/libexec/dovecot/deliver -d %{user.username}" + match from any for local action dovecot_deliver + ''; + }; + services.dovecot2 = { + enable = true; + enableImap = true; + mailLocation = "maildir:~/mail"; + protocols = [ "imap" ]; + }; + environment.systemPackages = let + checkMailLanded = pkgs.writeScriptBin "check-mail-landed" '' + #!${pkgs.python3.interpreter} + import imaplib + + with imaplib.IMAP4('127.0.0.1', 143) as imap: + imap.login('alice', 'foobar') + imap.select() + status, refs = imap.search(None, 'ALL') + print("=====> Result of search for all:", status, refs) + assert status == 'OK' + assert len(refs) > 0 + status, msg = imap.fetch(refs[0], 'BODY[TEXT]') + assert status == 'OK' + ''; + in [ pkgs.opensmtpd checkMailLanded ]; + }; + }; + + testScript = '' + startAll; + + $server->waitForUnit("network-online.target"); + $server->waitForUnit("opensmtpd"); + $server->waitForUnit("dovecot2"); + $server->waitForUnit("nginx"); + $server->waitForUnit("rss2email"); + + $server->waitUntilSucceeds('check-mail-landed >&2'); + ''; +} diff --git a/pkgs/applications/networking/feedreaders/rss2email/default.nix b/pkgs/applications/networking/feedreaders/rss2email/default.nix index 4152c2772e10..4d17cc8bddd1 100644 --- a/pkgs/applications/networking/feedreaders/rss2email/default.nix +++ b/pkgs/applications/networking/feedreaders/rss2email/default.nix @@ -1,11 +1,13 @@ -{ pythonPackages, fetchurl, lib }: +{ pythonPackages, fetchurl, lib, nixosTests }: with pythonPackages; buildPythonApplication rec { name = "${pname}-${version}"; pname = "rss2email"; - version = "3.9"; + version = "3.9"; # TODO: on next bump, the manpage will be updated. + # Update nixos/modules/services/mail/rss2email.nix to point to it instead of + # to the online r2e.1 propagatedBuildInputs = [ feedparser beautifulsoup4 html2text ]; @@ -44,4 +46,7 @@ buildPythonApplication rec { license = licenses.gpl2; maintainers = with maintainers; [ jb55 Profpatsch ]; }; + passthru.tests = { + smoke-test = nixosTests.rss2email; + }; } diff --git a/pkgs/servers/mail/system-sendmail/default.nix b/pkgs/servers/mail/system-sendmail/default.nix new file mode 100644 index 000000000000..0e290f135181 --- /dev/null +++ b/pkgs/servers/mail/system-sendmail/default.nix @@ -0,0 +1,36 @@ +{ stdenv, writeText }: + +let script = writeText "script" '' + #!/bin/sh + + if command -v sendmail > /dev/null 2>&1 && [ "$(command -v sendmail)" != "{{MYPATH}}" ]; then + exec sendmail "$@" + elif [ -x /run/wrappers/bin/sendmail ]; then + exec /run/wrappers/bin/sendmail "$@" + elif [ -x /run/current-system/sw/bin/sendmail ]; then + exec /run/current-system/sw/bin/sendmail "$@" + else + echo "Unable to find system sendmail." >&2 + exit 1 + fi +''; in +stdenv.mkDerivation { + name = "system-sendmail-1.0"; + + src = script; + + phases = [ "buildPhase" ]; + buildPhase = '' + mkdir -p $out/bin + < $src sed "s#{{MYPATH}}#$out/bin/sendmail#" > $out/bin/sendmail + chmod +x $out/bin/sendmail + ''; + + meta = with stdenv.lib; { + description = '' + A sendmail wrapper that calls the system sendmail. Do not install as system-wide sendmail! + ''; + platforms = platforms.unix; + maintainers = with maintainers; [ ekleog ]; + }; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index a73bc7f77c94..7c6db1806f40 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -13522,6 +13522,8 @@ with pkgs; pshs = callPackage ../servers/http/pshs { }; + system-sendmail = lowPrio (callPackage ../servers/mail/system-sendmail { }); + # PulseAudio daemons pulseaudio = callPackage ../servers/pulseaudio {