|
| 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 | + dataDir = lib.mkOption { |
| 27 | + type = lib.types.path; |
| 28 | + default = "/var/lib/${name}"; |
| 29 | + description = '' |
| 30 | + The directory where Peertube stores its data. |
| 31 | + ''; |
| 32 | + }; |
| 33 | + |
| 34 | + database = lib.mkOption { |
| 35 | + type = lib.types.str; |
| 36 | + default = "peertube_prod"; |
| 37 | + description = '' |
| 38 | + The Postgres database where Peertube stores its data. |
| 39 | + ''; |
| 40 | + }; |
| 41 | + |
| 42 | + configFile = lib.mkOption { |
| 43 | + type = lib.types.path; |
| 44 | + description = '' |
| 45 | + The configuration file path for Peertube. |
| 46 | + ''; |
| 47 | + }; |
| 48 | + |
| 49 | + package = lib.mkOption { |
| 50 | + type = lib.types.package; |
| 51 | + default = pkgs.peertube; |
| 52 | + description = '' |
| 53 | + Peertube package to use. |
| 54 | + ''; |
| 55 | + }; |
| 56 | + }; |
| 57 | + |
| 58 | + config = lib.mkIf cfg.enable { |
| 59 | + users.users = lib.optionalAttrs (cfg.user == name) { |
| 60 | + "${name}" = { |
| 61 | + inherit uid; |
| 62 | + group = cfg.group; |
| 63 | + description = "Peertube user"; |
| 64 | + home = cfg.dataDir; |
| 65 | + useDefaultShell = true; |
| 66 | + # todo: fix this. needed for postgres authentication |
| 67 | + password = "peertube"; |
| 68 | + }; |
| 69 | + }; |
| 70 | + users.groups = lib.optionalAttrs (cfg.group == name) { |
| 71 | + "${name}" = { |
| 72 | + inherit gid; |
| 73 | + }; |
| 74 | + }; |
| 75 | + |
| 76 | + services.redis = { |
| 77 | + enable = true; |
| 78 | + }; |
| 79 | + |
| 80 | + services.postgresql = { |
| 81 | + enable = true; |
| 82 | + package = pkgs.postgresql_12; |
| 83 | + # requires sudo -u postgres createdb -O peertube -E UTF8 -T template0 ${cfg.database} |
| 84 | + # so this may not suffice |
| 85 | + # ensureDatabases = [ "${cfg.database}" ]; |
| 86 | + ensureUsers = [ |
| 87 | + { |
| 88 | + name = "${cfg.user}"; |
| 89 | + # we create database with `peertube` as owner in `preStart` |
| 90 | + } |
| 91 | + ]; |
| 92 | + authentication = '' |
| 93 | + host ${cfg.database} ${cfg.user} 127.0.0.1/32 trust |
| 94 | + host ${cfg.database} ${cfg.user} 127.0.0.1/32 md5 |
| 95 | + ''; |
| 96 | + }; |
| 97 | + |
| 98 | + # Make sure the datadir exists with the desired permissions. |
| 99 | + systemd.tmpfiles.rules = [ |
| 100 | + "d \"${cfg.dataDir}\" - ${cfg.user} ${cfg.group} - -" |
| 101 | + ]; |
| 102 | + |
| 103 | + systemd.services.peertube = { |
| 104 | + description = "Peertube"; |
| 105 | + wantedBy = [ "multi-user.target" ]; |
| 106 | + after = [ "network.target" "postgresql.service" "redis.service" ]; |
| 107 | + wants = [ "postgresql.service" "redis.service" ]; |
| 108 | + |
| 109 | + environment.NODE_CONFIG_DIR = "${cfg.dataDir}/config"; |
| 110 | + environment.NODE_ENV = "production"; |
| 111 | + environment.HOME = cfg.package; |
| 112 | + |
| 113 | + path = [ pkgs.nodejs pkgs.bashInteractive pkgs.ffmpeg pkgs.openssl pkgs.sudo pkgs.youtube-dl ]; |
| 114 | + |
| 115 | + script = '' |
| 116 | + install -m 0750 -d ${cfg.dataDir}/config |
| 117 | + ln -sf ${cfg.configFile} ${cfg.dataDir}/config/production.yaml |
| 118 | + exec npm start |
| 119 | + ''; |
| 120 | + |
| 121 | + serviceConfig = { |
| 122 | + User = cfg.user; |
| 123 | + Group = cfg.group; |
| 124 | + WorkingDirectory = cfg.package; |
| 125 | + StateDirectory = "peertube"; |
| 126 | + StateDirectoryMode = "0750"; |
| 127 | + PrivateTmp = true; |
| 128 | + ProtectHome = true; |
| 129 | + ProtectControlGroups = true; |
| 130 | + ProtectSystem = "full"; |
| 131 | + Restart = "always"; |
| 132 | + Type = "simple"; |
| 133 | + TimeoutSec = 60; |
| 134 | + CapabilityBoundingSet = "~CAP_SYS_ADMIN"; |
| 135 | + ExecStartPre = let script = pkgs.writeScript "peertube-pre-start.sh" '' |
| 136 | + #!/bin/sh |
| 137 | + set -e |
| 138 | +
|
| 139 | + if ! [ -e "${cfg.dataDir}/.first_run" ]; then |
| 140 | + set -v |
| 141 | + if [ -e "${cfg.dataDir}/.first_run_partial" ]; then |
| 142 | + echo "Warn: first run was interrupted" |
| 143 | + fi |
| 144 | + touch "${cfg.dataDir}/.first_run_partial" |
| 145 | +
|
| 146 | + sudo -u postgres "${config.services.postgresql.package}/bin/createdb" -O ${cfg.user} -E UTF8 -T template0 ${cfg.database} |
| 147 | + sudo -u postgres "${config.services.postgresql.package}/bin/psql" -c "CREATE EXTENSION pg_trgm;" ${cfg.database} |
| 148 | + sudo -u postgres "${config.services.postgresql.package}/bin/psql" -c "CREATE EXTENSION unaccent;" ${cfg.database} |
| 149 | +
|
| 150 | + touch "${cfg.dataDir}/.first_run" |
| 151 | + rm "${cfg.dataDir}/.first_run_partial" |
| 152 | + fi |
| 153 | + ''; |
| 154 | + in "+${script}"; |
| 155 | + }; |
| 156 | + |
| 157 | + unitConfig.RequiresMountsFor = cfg.dataDir; |
| 158 | + }; |
| 159 | + }; |
| 160 | +} |
| 161 | + |
0 commit comments