Free and Open-source video streaming suite. https://miracle-tv.live
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.7KB

  1. { lib, pkgs, config, ... }:
  2. with lib;
  3. let
  4. cfg = config.services.miracle-tv;
  5. # Manually pin static versions of frontend and backend packages
  6. miracleSrc = pkgs.fetchgit {
  7. url = "https://code.gensokyo.social/Gensokyo.social/miracle-tv.git";
  8. rev = "493c80142637b6fc47c864dfb51a1cba2f391150";
  9. sha256 = "1a5ih7jbdkkapmih11jk4y95nk6ppvns5lg0aqv76kd9y9c8v26s";
  10. };
  11. miracleFeSrc = pkgs.fetchgit {
  12. url = "https://code.gensokyo.social/Gensokyo.social/miracle-tv-frontend.git";
  13. rev = "71f32ef097bc056aa7d7286e4d5d4de11bb43830";
  14. sha256 = "1ig4bhx31mwd6771q1fx33ajfyxs2rpdcancybd0gj5bin2wccsl";
  15. };
  16. miracle-tv = (import "${miracleSrc}/default.nix");
  17. miracle-tv-frontend = (import "${miracleFeSrc}/default.nix");
  18. in {
  19. options.services.miracle-tv = {
  20. enable = mkEnableOption "Miracle TV server";
  21. nginx = {
  22. manage = mkOption {
  23. type = types.bool;
  24. default = true;
  25. description = "Whether to manage nginx config for you or not.";
  26. };
  27. enableACME = mkOption {
  28. type = types.bool;
  29. default = false;
  30. description = "Whether to use Letsencrypt with this domain or not. Also sets forceSSL if true";
  31. };
  32. domain = mkOption {
  33. type = types.str;
  34. description = "Domain name to use with nginx";
  35. };
  36. };
  37. settings = {
  38. database = {
  39. client = mkOption {
  40. type = types.str;
  41. default = "pg";
  42. description = "Client to use. Currently only `pg` is available.";
  43. };
  44. connection = {
  45. hostname = mkOption {
  46. type = types.str;
  47. default = "localhost";
  48. description = "Database host.";
  49. };
  50. database = mkOption {
  51. type = types.str;
  52. default = "";
  53. description = "Database name.";
  54. };
  55. user = mkOption {
  56. type = types.str;
  57. default = "";
  58. description = "Database user.";
  59. };
  60. password = mkOption {
  61. type = types.str;
  62. default = "";
  63. description = "Database user's password.";
  64. };
  65. };
  66. migrations = {
  67. directory = mkOption {
  68. type = types.str;
  69. default = "${miracle-tv}/migrations";
  70. description = ''
  71. Folder with application's migrations. Defaults to bundled original migrations.
  72. Can be set to any folder that contains knex migrtaions
  73. '';
  74. };
  75. };
  76. };
  77. server = {
  78. port = mkOption {
  79. type = types.int;
  80. default = 8080;
  81. description = "Governs port number that server will listen on";
  82. };
  83. };
  84. };
  85. };
  86. config = let
  87. configFile = pkgs.writeText "config.json" (builtins.toJSON cfg.settings);
  88. in lib.mkIf cfg.enable {
  89. # If user delegated nginx config to us, make necessary config for them
  90. services.nginx.virtualHosts.${cfg.nginx.domain} = mkIf cfg.nginx.manage {
  91. # If user opted to use Letsencrypt, enable it for module and enforce https
  92. enableACME = mkIf cfg.nginx.enableACME true;
  93. forceSSL = mkIf cfg.nginx.enableACME true;
  94. # Serve dist of miracle-tv-frontend package automatically
  95. locations."/" = {
  96. root = "${miracle-tv-frontend}/dist";
  97. tryFiles = "$uri $uri/index.html /index.html";
  98. };
  99. # Proxy api to the miracle-tv-frontend server
  100. locations."/api" = {
  101. proxyPass = "http://localhost:${builtins.toString cfg.settings.server.port}";
  102. };
  103. };
  104. # systemd unit for miracle-tv-server
  105. systemd.services.miracle-tv = {
  106. wantedBy = [ "multi-user.target" "postgresql.service" ];
  107. serviceConfig.ExecStart = "${miracle-tv}/bin/miracle-tv-server --configPath=${configFile}";
  108. };
  109. };
  110. }