@@ -18,7 +18,6 @@ let
1818
1919 poolName = "tt-rss";
2020 phpfpmSocketName = "/var/run/phpfpm/${poolName}.sock";
21− virtualHostName = "tt-rss";
2221
2322 tt-rss-config = pkgs.writeText "config.php" ''
2423 <?php
@@ -34,10 +33,10 @@ let
3433 define('MYSQL_CHARSET', 'UTF8');
3534
3635 define('DB_TYPE', '${cfg.database.type}');
37− define('DB_HOST', '${cfg.database.host}');
36+ define('DB_HOST', '${optionalString (cfg.database.host != null) cfg.database.host}');
3837 define('DB_USER', '${cfg.database.user}');
3938 define('DB_NAME', '${cfg.database.name}');
40− define('DB_PASS', '${escape ["'" "\\"] cfg.database.password}');
39+ define('DB_PASS', '${optionalString (cfg.database.password != null) (escape ["'" "\\"] cfg.database.password)}');
4140 define('DB_PORT', '${toString dbPort}');
4241
4342 define('AUTH_AUTO_CREATE', ${boolToString cfg.auth.autoCreate});
@@ -91,12 +90,21 @@ let
9190
9291 enable = mkEnableOption "tt-rss";
9392
93+ root = mkOption {
94+ type = types.path;
95+ default = "/var/lib/tt-rss";
96+ example = "/var/lib/tt-rss";
97+ description = ''
98+ Root of the application.
99+ '';
100+ };
101+
94102 user = mkOption {
95103 type = types.str;
96104 default = "nginx";
97105 example = "nginx";
98106 description = ''
99− User account under which both the service and the web-application run.
107+ User account under which both the update daemon and the web-application run.
100108 '';
101109 };
102110
@@ -110,17 +118,13 @@ let
110118 '';
111119 };
112120
113− # TODO: Re-enable after https://github.com/NixOS/nixpkgs/pull/15862 is merged
114−
115− # virtualHost = mkOption {
116− # type = types.str;
117− # default = "${virtualHostName}";
118− # description = ''
119− # Name of existing nginx virtual host that is used to run web-application.
120− # If not specified a host will be created automatically with
121− # default values.
122− # '';
123− # };
121+ virtualHost = mkOption {
122+ type = types.nullOr types.str;
123+ default = "tt-rss";
124+ description = ''
125+ Name of the nginx virtualhost to use and setup. If null, do not setup any virtualhost.
126+ '';
127+ };
124128
125129 database = {
126130 type = mkOption {
@@ -132,10 +136,10 @@ let
132136 };
133137
134138 host = mkOption {
135− type = types.str;
136− default = "localhost";
139+ type = types.nullOr types.str;
140+ default = null;
137141 description = ''
138− Host of the database.
142+ Host of the database. Leave null to use Unix domain socket.
139143 '';
140144 };
141145
@@ -362,7 +366,7 @@ let
362366
363367 singleUserMode = mkOption {
364368 type = types.bool;
365− default = true;
369+ default = false;
366370
367371 description = ''
368372 Operate in single user mode, disables all functionality related to
@@ -445,17 +449,15 @@ let
445449
446450 ###### implementation
447451
448− config = let
449− root = "/var/lib/tt-rss";
450− in mkIf cfg.enable {
452+ config = mkIf cfg.enable {
451453
452− services.phpfpm.poolConfigs = if cfg.pool == "${poolName}" then {
454+ services.phpfpm.poolConfigs = mkIf (cfg.pool == "${poolName}") {
453455 "${poolName}" = ''
454456 listen = "${phpfpmSocketName}";
455457 listen.owner = nginx
456458 listen.group = nginx
457459 listen.mode = 0600
458− user = nginx
460+ user = ${cfg.user}
459461 pm = dynamic
460462 pm.max_children = 75
461463 pm.start_servers = 10
@@ -464,36 +466,26 @@ let
464466 pm.max_requests = 500
465467 catch_workers_output = 1
466468 '';
467− } else {};
468−
469− # TODO: Re-enable after https://github.com/NixOS/nixpkgs/pull/15862 is merged
470−
471− # services.nginx.virtualHosts = if cfg.virtualHost == "${virtualHostName}" then {
472− # "${virtualHostName}" = {
473− # root = "${root}";
474− # extraConfig = ''
475− # access_log /var/log/nginx-${virtualHostName}-access.log;
476− # error_log /var/log/nginx-${virtualHostName}-error.log;
477− # '';
478−
479− # locations."/" = {
480− # extraConfig = ''
481− # index index.php;
482− # '';
483− # };
484−
485− # locations."~ \.php$" = {
486− # extraConfig = ''
487− # fastcgi_split_path_info ^(.+\.php)(/.+)$;
488− # fastcgi_pass unix:${phpfpmSocketName};
489− # fastcgi_index index.php;
490− # fastcgi_param SCRIPT_FILENAME ${root}/$fastcgi_script_name;
491−
492− # include ${pkgs.nginx}/conf/fastcgi_params;
493− # '';
494− # };
495− # };
496− # } else {};
469+ };
470+
471+ services.nginx.virtualHosts = mkIf (cfg.virtualHost != null) {
472+ "${cfg.virtualHost}" = {
473+ root = "${cfg.root}";
474+
475+ locations."/" = {
476+ index = "index.php";
477+ };
478+
479+ locations."~ \.php$" = {
480+ extraConfig = ''
481+ fastcgi_split_path_info ^(.+\.php)(/.+)$;
482+ fastcgi_pass unix:${phpfpmSocketName};
483+ fastcgi_index index.php;
484+ fastcgi_param SCRIPT_FILENAME ${cfg.root}/$fastcgi_script_name;
485+ '';
486+ };
487+ };
488+ };
497489
498490
499491 systemd.services.tt-rss = let
@@ -503,35 +495,34 @@ let
503495 description = "Tiny Tiny RSS feeds update daemon";
504496
505497 preStart = let
506− callSql = if cfg.database.type == "pgsql" then (e: ''
507− ${optionalString (cfg.database.password != null)
508− "PGPASSWORD=${cfg.database.password}"} ${pkgs.postgresql95}/bin/psql \
509− -U ${cfg.database.user} \
510− -h ${cfg.database.host} \
511− --port ${toString dbPort} \
512− -c '${e}' \
513− ${cfg.database.name}'')
514−
515− else if cfg.database.type == "mysql" then (e: ''
516− echo '${e}' | ${pkgs.mysql}/bin/mysql \
517− ${optionalString (cfg.database.password != null)
518− "-p${cfg.database.password}"} \
519− -u ${cfg.database.user} \
520− -h ${cfg.database.host} \
521− -P ${toString dbPort} \
522− ${cfg.database.name}'')
523−
524− else "";
498+ callSql = e:
499+ if cfg.database.type == "pgsql" then ''
500+ ${optionalString (cfg.database.password != null) "PGPASSWORD=${cfg.database.password}"} \
501+ ${pkgs.postgresql95}/bin/psql \
502+ -U ${cfg.database.user} \
503+ ${optionalString (cfg.database.host != null) "-h ${cfg.database.host} --port ${toString dbPort}"} \
504+ -c '${e}' \
505+ ${cfg.database.name}''
506+
507+ else if cfg.database.type == "mysql" then ''
508+ echo '${e}' | ${pkgs.mysql}/bin/mysql \
509+ -u ${cfg.database.user} \
510+ ${optionalString (cfg.database.password != null) "-p${cfg.database.password}"} \
511+ ${optionalString (cfg.database.host != null) "-h ${cfg.database.host} -P ${toString dbPort}"} \
512+ ${cfg.database.name}''
513+
514+ else "";
525515
526516 in ''
527− rm -rf "${root}/*"
528− mkdir -m 755 -p "${root}"
529− cp -r "${pkgs.tt-rss}/"* "${root}"
530− ln -sf "${tt-rss-config}" "${root}/config.php"
531− chown -R "${cfg.user}" "${root}"
532− chmod -R 755 "${root}"
533− '' + (optionalString (cfg.database.type == "pgsql") ''
534−
517+ rm -rf "${cfg.root}/*"
518+ mkdir -m 755 -p "${cfg.root}"
519+ cp -r "${pkgs.tt-rss}/"* "${cfg.root}"
520+ ln -sf "${tt-rss-config}" "${cfg.root}/config.php"
521+ chown -R "${cfg.user}" "${cfg.root}"
522+ chmod -R 755 "${cfg.root}"
523+ ''
524+
525+ + (optionalString (cfg.database.type == "pgsql") ''
535526 exists=$(${callSql "select count(*) > 0 from pg_tables where tableowner = user"} \
536527 | tail -n+3 | head -n-2 | sed -e 's/[ \n\t]*//')
537528
@@ -540,8 +531,9 @@ let
540531 else
541532 echo 'The database contains some data. Leaving it as it is.'
542533 fi;
543− '') + (optionalString (cfg.database.type == "mysql") ''
534+ '')
544535
536+ + (optionalString (cfg.database.type == "mysql") ''
545537 exists=$(${callSql "select count(*) > 0 from information_schema.tables where table_schema = schema()"} \
546538 | tail -n+2 | sed -e 's/[ \n\t]*//')
547539
@@ -554,7 +546,7 @@ let
554546
555547 serviceConfig = {
556548 User = "${cfg.user}";
557− ExecStart = "${pkgs.php}/bin/php /var/lib/tt-rss/update.php --daemon";
549+ ExecStart = "${pkgs.php}/bin/php ${cfg.root}/update.php --daemon";
558550 StandardOutput = "syslog";
559551 StandardError = "syslog";
560552 PermissionsStartOnly = true;