From 98e419c0e27dd21e9dee0b915876ea603b833057 Mon Sep 17 00:00:00 2001 From: Al Zohali Date: Sun, 5 Jun 2016 21:55:46 +0300 Subject: tt-rss service: init at 16.3 --- nixos/modules/services/web-apps/tt-rss.nix | 567 +++++++++++++++++++++++++++++ 1 file changed, 567 insertions(+) create mode 100644 nixos/modules/services/web-apps/tt-rss.nix (limited to 'nixos/modules/services/web-apps/tt-rss.nix') diff --git a/nixos/modules/services/web-apps/tt-rss.nix b/nixos/modules/services/web-apps/tt-rss.nix new file mode 100644 index 000000000000..8ab51c006f27 --- /dev/null +++ b/nixos/modules/services/web-apps/tt-rss.nix @@ -0,0 +1,567 @@ +{ config, lib, pkgs, ... }: + +with lib; +let + cfg = config.services.tt-rss; + + configVersion = 26; + + boolToString = b: if b then "true" else "false"; + + cacheDir = "cache"; + lockDir = "lock"; + feedIconsDir = "feed-icons"; + + dbPort = if cfg.database.port == null + then (if cfg.database.type == "pgsql" then 5432 else 3306) + else cfg.database.port; + + poolName = "tt-rss"; + virtualHostName = "tt-rss"; + + tt-rss-config = pkgs.writeText "config.php" '' + System), syslog - logs to system log. + Setting this to blank uses PHP logging (usually to http server + error.log). + ''; + }; + }; + }; + + + ###### implementation + + config = let + root = "/var/lib/tt-rss"; + in mkIf cfg.enable { + + services.phpfpm.pools = if cfg.pool == "${poolName}" then { + "${poolName}" = { + listen = "/var/run/phpfpm/${poolName}.sock"; + extraConfig = '' + listen.owner = nginx + listen.group = nginx + listen.mode = 0600 + user = nginx + pm = dynamic + pm.max_children = 75 + pm.start_servers = 10 + pm.min_spare_servers = 5 + pm.max_spare_servers = 20 + pm.max_requests = 500 + catch_workers_output = 1 + ''; + }; + } else {}; + + + services.nginx.virtualHosts = if cfg.virtualHost == "${virtualHostName}" then { + "${virtualHostName}" = { + root = "${root}"; + extraConfig = '' + access_log /var/log/nginx-${virtualHostName}-access.log; + error_log /var/log/nginx-${virtualHostName}-error.log; + ''; + + locations."/" = { + extraConfig = '' + index index.php; + ''; + }; + + locations."~ \.php$" = { + extraConfig = '' + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass unix:${config.services.phpfpm.pools."${cfg.pool}".listen}; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME ${root}/$fastcgi_script_name; + + include ${pkgs.nginx}/conf/fastcgi_params; + ''; + }; + }; + } else {}; + + + systemd.services.tt-rss = let + dbService = if cfg.database.type == "pgsql" then "postgresql.service" else "mysql.service"; + in { + + description = "Tiny Tiny RSS feeds update daemon"; + + preStart = let + callSql = if cfg.database.type == "pgsql" then (e: '' + ${optionalString (cfg.database.password != null) + "PGPASSWORD=${cfg.database.password}"} ${pkgs.postgresql95}/bin/psql \ + -U ${cfg.database.user} \ + -h ${cfg.database.host} \ + --port ${toString dbPort} \ + -c '${e}' \ + ${cfg.database.name}'') + + else if cfg.database.type == "mysql" then (e: '' + echo '${e}' | ${pkgs.mysql}/bin/mysql \ + ${optionalString (cfg.database.password != null) + "-p${cfg.database.password}"} \ + -u ${cfg.database.user} \ + -h ${cfg.database.host} \ + -P ${toString dbPort} \ + ${cfg.database.name}'') + + else ""; + + in '' + rm -rf "${root}/*" + mkdir -m 755 -p "${root}" + cp -r "${pkgs.tt-rss}/"* "${root}" + ln -sf "${tt-rss-config}" "${root}/config.php" + chown -R "${cfg.user}" "${root}" + chmod -R 755 "${root}" + '' + (optionalString (cfg.database.type == "pgsql") '' + + exists=$(${callSql "select count(*) > 0 from pg_tables where tableowner = user"} \ + | tail -n+3 | head -n-2 | sed -e 's/[ \n\t]*//') + + if [ "$exists" == 'f' ]; then + ${callSql "\\i ${pkgs.tt-rss}/schema/ttrss_schema_${cfg.database.type}.sql"} + else + echo 'The database contains some data. Leaving it as it is.' + fi; + '') + (optionalString (cfg.database.type == "mysql") '' + + exists=$(${callSql "select count(*) > 0 from information_schema.tables where table_schema = schema()"} \ + | tail -n+2 | sed -e 's/[ \n\t]*//') + + if [ "$exists" == '0' ]; then + ${callSql "\\. ${pkgs.tt-rss}/schema/ttrss_schema_${cfg.database.type}.sql"} + else + echo 'The database contains some data. Leaving it as it is.' + fi; + ''); + + serviceConfig = { + User = "${cfg.user}"; + ExecStart = "${pkgs.php}/bin/php /var/lib/tt-rss/update.php --daemon"; + StandardOutput = "syslog"; + StandardError = "syslog"; + PermissionsStartOnly = true; + }; + + wantedBy = [ "multi-user.target" ]; + requires = ["${dbService}"]; + after = ["network.target" "${dbService}"]; + }; + }; +} + -- cgit 1.4.1