{ 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; tt-rss-config = pkgs.stdenv.mkDerivation { name = "tt-rss-config"; buildCommand = let cfgFile = 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 = mkIf cfg.enable { 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 root = "/var/lib/tt-rss"; 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}"]; }; }; }