|
| 1 | +{ lib, pkgs, config, ... }: |
| 2 | + |
| 3 | +let |
| 4 | + name = "peertube"; |
| 5 | + cfg = config.services.peertube; |
| 6 | + |
| 7 | + uid = config.ids.uids.peertube; |
| 8 | + gid = config.ids.gids.peertube; |
| 9 | +in |
| 10 | +{ |
| 11 | + options.services.peertube = { |
| 12 | + enable = lib.mkEnableOption "Enable Peertube’s service"; |
| 13 | + |
| 14 | + user = lib.mkOption { |
| 15 | + type = lib.types.str; |
| 16 | + default = name; |
| 17 | + description = "User account under which Peertube runs"; |
| 18 | + }; |
| 19 | + |
| 20 | + group = lib.mkOption { |
| 21 | + type = lib.types.str; |
| 22 | + default = name; |
| 23 | + description = "Group under which Peertube runs"; |
| 24 | + }; |
| 25 | + |
| 26 | + configFile = lib.mkOption { |
| 27 | + type = lib.types.path; |
| 28 | + description = '' |
| 29 | + The configuration file path for Peertube. |
| 30 | + ''; |
| 31 | + }; |
| 32 | + |
| 33 | + database = { |
| 34 | + createLocally = lib.mkOption { |
| 35 | + description = "Configure local PostgreSQL database server for PeerTube."; |
| 36 | + type = lib.types.bool; |
| 37 | + default = true; |
| 38 | + }; |
| 39 | + |
| 40 | + name = lib.mkOption { |
| 41 | + type = lib.types.str; |
| 42 | + default = "peertube_prod"; |
| 43 | + description = "Database name."; |
| 44 | + }; |
| 45 | + |
| 46 | + user = lib.mkOption { |
| 47 | + type = lib.types.str; |
| 48 | + default = "peertube"; |
| 49 | + description = "Database user."; |
| 50 | + }; |
| 51 | + }; |
| 52 | + |
| 53 | + smtp = { |
| 54 | + createLocally = lib.mkOption { |
| 55 | + description = "Configure local Postfix SMTP server for PeerTube."; |
| 56 | + type = lib.types.bool; |
| 57 | + default = true; |
| 58 | + }; |
| 59 | + }; |
| 60 | + |
| 61 | + redis = { |
| 62 | + createLocally = lib.mkOption { |
| 63 | + description = "Configure local Redis server for PeerTube."; |
| 64 | + type = lib.types.bool; |
| 65 | + default = true; |
| 66 | + }; |
| 67 | + }; |
| 68 | + |
| 69 | + runtimeDir = lib.mkOption { |
| 70 | + type = lib.types.path; |
| 71 | + default = "/var/lib/${name}"; |
| 72 | + description = "The directory where Peertube stores its runtime data."; |
| 73 | + }; |
| 74 | + |
| 75 | + package = lib.mkOption { |
| 76 | + type = lib.types.package; |
| 77 | + default = pkgs.peertube; |
| 78 | + description = '' |
| 79 | + Peertube package to use. |
| 80 | + ''; |
| 81 | + }; |
| 82 | + }; |
| 83 | + |
| 84 | + config = lib.mkIf cfg.enable { |
| 85 | + users.users = lib.optionalAttrs (cfg.user == name) { |
| 86 | + "${name}" = { |
| 87 | + inherit uid; |
| 88 | + group = cfg.group; |
| 89 | + description = "Peertube user"; |
| 90 | + home = cfg.runtimeDir; |
| 91 | + useDefaultShell = true; |
| 92 | + # todo: fix this. needed for postgres authentication |
| 93 | + password = "peertube"; |
| 94 | + }; |
| 95 | + }; |
| 96 | + users.groups = lib.optionalAttrs (cfg.group == name) { |
| 97 | + "${name}" = { |
| 98 | + inherit gid; |
| 99 | + }; |
| 100 | + }; |
| 101 | + |
| 102 | + services.postgresql = lib.mkIf cfg.database.createLocally { |
| 103 | + enable = true; |
| 104 | + ensureUsers = [ { name = cfg.database.user; }]; |
| 105 | + # The database is created as a startup script of the peertube service. |
| 106 | + authentication = '' |
| 107 | + host ${cfg.database.name} ${cfg.database.user} 127.0.0.1/32 trust |
| 108 | + host ${cfg.database.name} ${cfg.database.user} 127.0.0.1/32 md5 |
| 109 | + ''; |
| 110 | + }; |
| 111 | + |
| 112 | + services.postfix = lib.mkIf cfg.smtp.createLocally { |
| 113 | + enable = true; |
| 114 | + }; |
| 115 | + |
| 116 | + services.redis = lib.mkIf cfg.redis.createLocally { |
| 117 | + enable = true; |
| 118 | + }; |
| 119 | + |
| 120 | + # Make sure the runtimeDir exists with the desired permissions. |
| 121 | + systemd.tmpfiles.rules = [ |
| 122 | + "d \"${cfg.runtimeDir}\" - ${cfg.user} ${cfg.group} - -" |
| 123 | + ]; |
| 124 | + |
| 125 | + systemd.services.peertube = { |
| 126 | + description = "Peertube"; |
| 127 | + wantedBy = [ "multi-user.target" ]; |
| 128 | + after = [ "network.target" "postgresql.service" "redis.service" ]; |
| 129 | + wants = [ "postgresql.service" "redis.service" ]; |
| 130 | + |
| 131 | + environment.NODE_CONFIG_DIR = "${cfg.runtimeDir}/config"; |
| 132 | + environment.NODE_ENV = "production"; |
| 133 | + environment.HOME = cfg.package; |
| 134 | + |
| 135 | + path = [ pkgs.nodejs pkgs.bashInteractive pkgs.ffmpeg pkgs.openssl pkgs.sudo pkgs.youtube-dl ]; |
| 136 | + |
| 137 | + script = '' |
| 138 | + install -m 0750 -d ${cfg.runtimeDir}/config |
| 139 | + ln -sf ${cfg.configFile} ${cfg.runtimeDir}/config/production.yaml |
| 140 | + exec npm start |
| 141 | + ''; |
| 142 | + |
| 143 | + serviceConfig = { |
| 144 | + User = cfg.user; |
| 145 | + Group = cfg.group; |
| 146 | + WorkingDirectory = cfg.package; |
| 147 | + StateDirectory = "peertube"; |
| 148 | + StateDirectoryMode = "0750"; |
| 149 | + PrivateTmp = true; |
| 150 | + ProtectHome = true; |
| 151 | + ProtectControlGroups = true; |
| 152 | + ProtectSystem = "full"; |
| 153 | + Restart = "always"; |
| 154 | + Type = "simple"; |
| 155 | + TimeoutSec = 60; |
| 156 | + CapabilityBoundingSet = "~CAP_SYS_ADMIN"; |
| 157 | + ExecStartPre = let script = pkgs.writeScript "peertube-pre-start.sh" '' |
| 158 | + #!/bin/sh |
| 159 | + set -e |
| 160 | +
|
| 161 | + if ! [ -e "${cfg.runtimeDir}/.first_run" ]; then |
| 162 | + set -v |
| 163 | + if [ -e "${cfg.runtimeDir}/.first_run_partial" ]; then |
| 164 | + echo "Warn: first run was interrupted" |
| 165 | + fi |
| 166 | + touch "${cfg.runtimeDir}/.first_run_partial" |
| 167 | +
|
| 168 | + echo "Running PeerTube's PostgreSQL initialization..." |
| 169 | + echo "PeerTube is known to work with PostgreSQL v12, if any error occurs, please check your version." |
| 170 | +
|
| 171 | + sudo -u postgres "${config.services.postgresql.package}/bin/createdb" -O ${cfg.database.user} -E UTF8 -T template0 ${cfg.database.name} |
| 172 | + sudo -u postgres "${config.services.postgresql.package}/bin/psql" -c "CREATE EXTENSION pg_trgm;" ${cfg.database.name} |
| 173 | + sudo -u postgres "${config.services.postgresql.package}/bin/psql" -c "CREATE EXTENSION unaccent;" ${cfg.database.name} |
| 174 | +
|
| 175 | + touch "${cfg.runtimeDir}/.first_run" |
| 176 | + rm "${cfg.runtimeDir}/.first_run_partial" |
| 177 | + fi |
| 178 | + ''; |
| 179 | + in "+${script}"; |
| 180 | + }; |
| 181 | + |
| 182 | + unitConfig.RequiresMountsFor = cfg.runtimeDir; |
| 183 | + }; |
| 184 | + }; |
| 185 | +} |
| 186 | + |
0 commit comments