From 777e80e720d2940412708212e335bc4c6c8d848c Mon Sep 17 00:00:00 2001 From: stubbfel Date: Sat, 23 Dec 2017 16:38:23 +0100 Subject: [PATCH] add news2kintle module and service --- configuration.nix | 1 + module/news2kindle/news2kindle.nix | 185 +++++++++++++++++++++++++++++ services/enabled.nix | 1 + services/news2kindle.nix | 40 +++++++ users.nix | 1 - 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 module/news2kindle/news2kindle.nix create mode 100644 services/news2kindle.nix diff --git a/configuration.nix b/configuration.nix index d5c5323..ce1c768 100644 --- a/configuration.nix +++ b/configuration.nix @@ -14,6 +14,7 @@ ./programs/installed.nix ./services/enabled.nix ./users.nix + ./module/news2kindle/news2kindle.nix ]; nixpkgs.config.allowUnfree = true; diff --git a/module/news2kindle/news2kindle.nix b/module/news2kindle/news2kindle.nix new file mode 100644 index 0000000..2eac222 --- /dev/null +++ b/module/news2kindle/news2kindle.nix @@ -0,0 +1,185 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.services.news2kindle; + +portString = toString cfg.smtp.port; + + convertScript = pkgs.writeText "convertScript.sh" ''#!/bin/bash +NAME=$1 +RECIPEPATH=$2 +MOBIPATH=$3 +FROMADDR=$4 +TOADDR=$5 +if [ ! -f mobi/$NAME.mobi ]; then + ebook-convert $RECIPEPATH/$NAME.recipe $MOBIPATH/$NAME.mobi --output-profile kindle -v +fi + +mailsend -smtp ${cfg.smtp.address} -port ${portString} -from $FROMADDR -to $TOADDR -sub "New news from $NAME" -attach $MOBIPATH/$NAME.mobi +sleep 60 +''; + +callConvertScripts = + let + makeCallLine = recipient: + map (makeCallLineRecipe recipient.mail) recipient.recipes; + makeCallLineRecipe = recipientMail: recipe: + if isNull recipe.content then + "bash ${convertScript} ${recipe.name} ${calibreWithRecipes}/var/news2kindle/recipes ${cfg.mobiPath} ${cfg.fromMail} ${recipientMail}\n" + else + '' + mkdir -p /tmp/news2kindle/recipes + echo "${recipe.content}" > /tmp/news2kindle/recipes/${recipe.name}.recipe + bash ${convertScript} ${recipe.name} /tmp/news2kindle/recipes/ ${cfg.mobiPath} ${cfg.fromMail} ${recipientMail} + ''; + in + concatMap makeCallLine cfg.recipients; + +dailyUpdateScript = pkgs.writeText "dailyUpdates.sh" ''#!/bin/bash + rm -f ${cfg.mobiPath}/*.mobi + mkdir -p ${cfg.mobiPath} + ${concatStrings callConvertScripts} + ''; + +calibreWithRecipes = pkgs.calibre.overrideAttrs (oldAttrs: rec { + installPhase = '' + mkdir -p $out/var/news2kindle/recipes + cp -ravf recipes $out/var/news2kindle + ''+ oldAttrs.installPhase ; +}); + + +in + +{ + ###### interface + + options = { + + services.news2kindle = { + + enable = mkOption { + type = types.bool; + default = false; + description = '' + Whether to enable news2kindle service. + ''; + }; + + recipePath = mkOption { + example = "/var/news2kindle/recipes"; + default = "/var/news2kindle/recipes"; + type = types.str; + description = '' + Path where the recipes should be saved + ''; + }; + + mobiPath = mkOption { + example = "/tmp/news2kindle/mobi"; + default = "/tmp/news2kindle/mobi"; + type = types.str; + description = '' + Path where the mobi files should be saved + ''; + }; + + fromMail = mkOption { + example = "news@stubbe.rocks"; + default = "news@stubbe.rocks"; + type = types.str; + description = '' + Adrees of the sender, please allow this mail in your kindle doc service + ''; + }; + + + recipients = mkOption { + type = with types; listOf (submodule { + options = { + mail = mkOption { + type = types.str; + default = null; + description = '' + name of recipe + ''; + }; + recipes = mkOption { + type = with types; listOf (submodule { + options = { + name = mkOption { + type = types.str; + default = null; + description = '' + name of recipe + ''; + }; + content = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + content of the recipe file, when its null the use the builtins recipes + ''; + }; + }; + }); + default = []; + example = [ { name = "heise";} { name = "Postillon"; port = '' + class BasicUserRecipe1414943033(AutomaticNewsRecipe): + title = u'Postillon' + oldest_article = 7 + max_articles_per_feed = 100 + auto_cleanup = True + feeds= [(u'Postilion', u'http://feeds.feedburner.com/blogspot/rkEL')] ''; } ]; + description = '' + List of recipes wich the user wants fetch. If recipe content is not specified the fetcher uses builtins recipes + ''; + }; + }; + }); + default = []; + }; + + smtp = { + + address = mkOption { + type = types.str; + default = "localhost"; + description = "Address of the SMTP server for news2kindle."; + }; + + port = mkOption { + type = types.int; + default = 25; + description = "Port of the SMTP server for news2kindle."; + }; + + username = mkOption { + type = types.nullOr types.str; + default = null; + description = "Username of the SMTP server for news2kindle."; + }; + + password = mkOption { + type = types.nullOr types.str; + default = null; + description = "Password of the SMTP server for news2kindle."; + }; + }; + + }; + + }; + + + ###### implementation + + config = mkIf cfg.enable{ + environment.systemPackages = [pkgs.python27Packages.pyqt5 pkgs.python36Packages.pyqt5 pkgs.mailsend calibreWithRecipes]; + services.cron.enable = true; + services.cron.systemCronJobs = [ "0 5 * * * root bash ${dailyUpdateScript}"]; + }; +} diff --git a/services/enabled.nix b/services/enabled.nix index 9da81d1..d32c467 100644 --- a/services/enabled.nix +++ b/services/enabled.nix @@ -8,5 +8,6 @@ ./nginx.nix ./gitlab.nix ./nextcloud.nix + ./news2kindle.nix ]; } diff --git a/services/news2kindle.nix b/services/news2kindle.nix new file mode 100644 index 0000000..5d3a31d --- /dev/null +++ b/services/news2kindle.nix @@ -0,0 +1,40 @@ +{ config, pkgs, ... }: + +{ + services.postfix.enable = true; + services.postfix.extraConfig = "message_size_limit=52428800"; + services.news2kindle = { + enable = true; + recipients = [ + { + mail = "stubbfel@kindle.com"; + recipes = [ + { name = "heise"; } + { name = "netzpolitik"; } + { name = "zeitde"; } + { name = "spiegelde"; } + { name = "Postillon"; + content = '' +class BasicUserRecipe1414943033(AutomaticNewsRecipe): + title = u'Der Postillon' + oldest_article = 7 + max_articles_per_feed = 100 + auto_cleanup = True + feeds = [(u'Postilion', +u'http://feeds.feedburner.com/blogspot/rkEL')] + '';} + { name = "Störungsmelder"; + content = '' +class BasicUserRecipe1397125629(AutomaticNewsRecipe): + title = u'st\xf6rungsmelder' + oldest_article = 7 + max_articles_per_feed = 100 + auto_cleanup = True + feeds = [(u'st\xf6rungsmelder', +u'http://blog.zeit.de/stoerungsmelder/feed')] + '';} + ]; + } + ]; + }; +} diff --git a/users.nix b/users.nix index aca51d5..8520004 100644 --- a/users.nix +++ b/users.nix @@ -12,7 +12,6 @@ in users = { defaultUserShell = pkgs.zsh; extraUsers.sshuser = myPublicSshKeys // { isNormalUser = true; home = "/home/sshuser";}; - extraUsers.calibre = myPublicSshKeys // { isNormalUser = true; home = "/home/calibre"; packages = [pkgs.xvfb_run pkgs.calibre]}; extraUsers.nextcloud = myPublicSshKeys // { isNormalUser = true; home = "/home/nextcloud"; group = "nextcloud";}; extraGroups.nexdcloud.name = "nextcloud"; };