lol

Merge pull request #18047 from Nadrieril/ttrss

tt-rss service: Use nginx virtualhosts; improve config options

authored by

Joachim F and committed by
GitHub
fbcb9385 c571a7f2

+69 -77
+69 -77
nixos/modules/services/web-apps/tt-rss.nix
··· 18 18 19 19 poolName = "tt-rss"; 20 20 phpfpmSocketName = "/var/run/phpfpm/${poolName}.sock"; 21 - virtualHostName = "tt-rss"; 22 21 23 22 tt-rss-config = pkgs.writeText "config.php" '' 24 23 <?php ··· 34 33 define('MYSQL_CHARSET', 'UTF8'); 35 34 36 35 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}'); 38 37 define('DB_USER', '${cfg.database.user}'); 39 38 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)}'); 41 40 define('DB_PORT', '${toString dbPort}'); 42 41 43 42 define('AUTH_AUTO_CREATE', ${boolToString cfg.auth.autoCreate}); ··· 91 90 92 91 enable = mkEnableOption "tt-rss"; 93 92 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 + 94 102 user = mkOption { 95 103 type = types.str; 96 104 default = "nginx"; 97 105 example = "nginx"; 98 106 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. 100 108 ''; 101 109 }; 102 110 ··· 110 118 ''; 111 119 }; 112 120 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 + }; 124 128 125 129 database = { 126 130 type = mkOption { ··· 132 136 }; 133 137 134 138 host = mkOption { 135 - type = types.str; 136 - default = "localhost"; 139 + type = types.nullOr types.str; 140 + default = null; 137 141 description = '' 138 - Host of the database. 142 + Host of the database. Leave null to use Unix domain socket. 139 143 ''; 140 144 }; 141 145 ··· 362 366 363 367 singleUserMode = mkOption { 364 368 type = types.bool; 365 - default = true; 369 + default = false; 366 370 367 371 description = '' 368 372 Operate in single user mode, disables all functionality related to ··· 445 449 446 450 ###### implementation 447 451 448 - config = let 449 - root = "/var/lib/tt-rss"; 450 - in mkIf cfg.enable { 452 + config = mkIf cfg.enable { 451 453 452 - services.phpfpm.poolConfigs = if cfg.pool == "${poolName}" then { 454 + services.phpfpm.poolConfigs = mkIf (cfg.pool == "${poolName}") { 453 455 "${poolName}" = '' 454 456 listen = "${phpfpmSocketName}"; 455 457 listen.owner = nginx 456 458 listen.group = nginx 457 459 listen.mode = 0600 458 - user = nginx 460 + user = ${cfg.user} 459 461 pm = dynamic 460 462 pm.max_children = 75 461 463 pm.start_servers = 10 ··· 464 466 pm.max_requests = 500 465 467 catch_workers_output = 1 466 468 ''; 467 - } else {}; 469 + }; 468 470 469 - # TODO: Re-enable after https://github.com/NixOS/nixpkgs/pull/15862 is merged 471 + services.nginx.virtualHosts = mkIf (cfg.virtualHost != null) { 472 + "${cfg.virtualHost}" = { 473 + root = "${cfg.root}"; 470 474 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; 475 + locations."/" = { 476 + index = "index.php"; 477 + }; 491 478 492 - # include ${pkgs.nginx}/conf/fastcgi_params; 493 - # ''; 494 - # }; 495 - # }; 496 - # } else {}; 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 + }; 497 489 498 490 499 491 systemd.services.tt-rss = let ··· 503 495 description = "Tiny Tiny RSS feeds update daemon"; 504 496 505 497 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}'') 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}'' 514 506 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}'') 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}'' 523 513 524 - else ""; 514 + else ""; 525 515 526 516 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") '' 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 + '' 534 524 525 + + (optionalString (cfg.database.type == "pgsql") '' 535 526 exists=$(${callSql "select count(*) > 0 from pg_tables where tableowner = user"} \ 536 527 | tail -n+3 | head -n-2 | sed -e 's/[ \n\t]*//') 537 528 ··· 540 531 else 541 532 echo 'The database contains some data. Leaving it as it is.' 542 533 fi; 543 - '') + (optionalString (cfg.database.type == "mysql") '' 534 + '') 544 535 536 + + (optionalString (cfg.database.type == "mysql") '' 545 537 exists=$(${callSql "select count(*) > 0 from information_schema.tables where table_schema = schema()"} \ 546 538 | tail -n+2 | sed -e 's/[ \n\t]*//') 547 539 ··· 554 546 555 547 serviceConfig = { 556 548 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"; 558 550 StandardOutput = "syslog"; 559 551 StandardError = "syslog"; 560 552 PermissionsStartOnly = true;