Files
stubbfelnix/module/news2kindle/news2kindle.nix
2018-02-13 21:43:59 +01:00

196 lines
5.7 KiB
Nix

{ config, lib, pkgs, fetchurl, ... }:
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}
'';
odfpyNoTest = pkgs.python2Packages.odfpy.overrideAttrs (oldAttrs: rec {
doInstallCheck = false;
});
calibreWithRecipes = pkgs.calibre.overrideAttrs (oldAttrs: rec {
installPhase = ''
mkdir -p $out/var/news2kindle/recipes
cp -ravf recipes $out/var/news2kindle
''+ oldAttrs.installPhase ;
buildInputs = (remove pkgs.python2Packages.odfpy oldAttrs.buildInputs) ++ [odfpyNoTest];
# patches = oldAttrs.patches ++ [./calibre-disable_plugins.patch];
patches = [./calibre-disable_plugins.patch];
});
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}"];
};
}