Skip to content

Commit 2079cc7

Browse files
committed
Add PeerTube (v3.0.1) pkg and service
1 parent 5efbf24 commit 2079cc7

File tree

9 files changed

+372
-0
lines changed

9 files changed

+372
-0
lines changed

maintainers/maintainer-list.nix

+6
Original file line numberDiff line numberDiff line change
@@ -8767,6 +8767,12 @@
87678767
githubId = 1181362;
87688768
name = "Stefan Junker";
87698769
};
8770+
stevenroose = {
8771+
email = "github@stevenroose.org";
8772+
github = "stevenroose";
8773+
githubId = 853468;
8774+
name = "Steven Roose";
8775+
};
87708776
stianlagstad = {
87718777
email = "stianlagstad@gmail.com";
87728778
github = "stianlagstad";

nixos/modules/misc/ids.nix

+2
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ in
347347
#mailman = 316; # removed 2019-08-30
348348
zigbee2mqtt = 317;
349349
# shadow = 318; # unused
350+
peertube = 319;
350351

351352
# When adding a uid, make sure it doesn't match an existing gid. And don't use uids above 399!
352353

@@ -649,6 +650,7 @@ in
649650
#mailman = 316; # removed 2019-08-30
650651
zigbee2mqtt = 317;
651652
shadow = 318;
653+
peertube = 319;
652654

653655
# When adding a gid, make sure it doesn't match an existing
654656
# uid. Users and groups with the same name should have equal

nixos/modules/module-list.nix

+1
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@
890890
./services/web-apps/moodle.nix
891891
./services/web-apps/nextcloud.nix
892892
./services/web-apps/nexus.nix
893+
./services/web-apps/peertube.nix
893894
./services/web-apps/plantuml-server.nix
894895
./services/web-apps/pgpkeyserver-lite.nix
895896
./services/web-apps/matomo.nix
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+

pkgs/servers/peertube/client.nix

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{ yarnModulesConfig, mkYarnModules', server, sources, version, nodejs, stdenv }:
2+
rec {
3+
modules = mkYarnModules' rec {
4+
pname = "peertube-client-yarn-modules";
5+
inherit version;
6+
name = "${pname}-${version}";
7+
packageJSON = "${sources}/client/package.json";
8+
yarnLock = "${sources}/client/yarn.lock";
9+
pkgConfig = yarnModulesConfig;
10+
};
11+
dist = stdenv.mkDerivation {
12+
pname = "peertube-client";
13+
inherit version;
14+
src = sources;
15+
buildPhase = ''
16+
ln -s ${server.modules}/node_modules .
17+
cp -a ${modules}/node_modules client/
18+
chmod -R +w client/node_modules
19+
patchShebangs .
20+
npm run build:client
21+
'';
22+
installPhase = ''
23+
mkdir $out
24+
cp -a client/dist $out
25+
'';
26+
buildInputs = [ nodejs ];
27+
};
28+
}

pkgs/servers/peertube/default.nix

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{ light ? null, pkgs, stdenv, fetchurl, fetchFromGitHub, callPackage
2+
, nodejs ? pkgs.nodejs-12_x
3+
, jq, youtube-dl, nodePackages, yarn2nix-moretea }:
4+
5+
let
6+
version = "3.0.1";
7+
8+
source = fetchFromGitHub {
9+
owner = "Chocobozzz";
10+
repo = "PeerTube";
11+
rev = "v${version}";
12+
sha256 = "bIXt5bQiBBlNDFXYzcdQA8qp4nse5epUx/XQOguDOX8=";
13+
};
14+
15+
patchedSource = stdenv.mkDerivation {
16+
pname = "peertube";
17+
inherit version;
18+
src = source;
19+
phases = [ "unpackPhase" "patchPhase" "installPhase" ];
20+
patches = [ ./fix_yarn_lock.patch ];
21+
installPhase = ''
22+
mkdir $out
23+
cp -a . $out/
24+
'';
25+
};
26+
yarnModulesConfig = {
27+
bcrypt = {
28+
buildInputs = [ nodePackages.node-pre-gyp ];
29+
postInstall = let
30+
bcrypt_version = "5.0.0";
31+
bcrypt_lib = fetchurl {
32+
url = "https://github.com/kelektiv/node.bcrypt.js/releases/download/v${bcrypt_version}/bcrypt_lib-v${bcrypt_version}-napi-v3-linux-x64-glibc.tar.gz";
33+
sha256 = "0j3p2px1xb17sw3gpm8l4apljajxxfflal1yy552mhpzhi21wccn";
34+
};
35+
in
36+
''
37+
if [ "${bcrypt_version}" != "$(cat package.json | ${jq}/bin/jq -r .version)" ]; then
38+
echo "Mismatching version please update bcrypt in derivation"
39+
false
40+
fi
41+
mkdir -p lib/binding && tar -C lib/binding -xf ${bcrypt_lib}
42+
patchShebangs ../node-pre-gyp
43+
npm run install
44+
'';
45+
};
46+
utf-8-validate = {
47+
buildInputs = [ nodePackages.node-gyp-build ];
48+
};
49+
youtube-dl = {
50+
postInstall = ''
51+
mkdir bin
52+
ln -s ${youtube-dl}/bin/youtube-dl bin/youtube-dl
53+
cat > bin/details <<EOF
54+
{"version":"${youtube-dl.version}","path":null,"exec":"youtube-dl"}
55+
EOF
56+
'';
57+
};
58+
};
59+
mkYarnModules' = args: (yarn2nix-moretea.mkYarnModules args).overrideAttrs(old: {
60+
# This hack permits to workaround the fact that the yarn.lock
61+
# file doesn't respect the semver requirements
62+
buildPhase = builtins.replaceStrings [" ./package.json"] [" /dev/null; cp deps/*/package.json ."] old.buildPhase;
63+
});
64+
65+
server = callPackage ./server.nix {
66+
inherit version yarnModulesConfig mkYarnModules';
67+
sources = patchedSource;
68+
};
69+
client = callPackage ./client.nix {
70+
inherit server version yarnModulesConfig mkYarnModules';
71+
sources = patchedSource;
72+
};
73+
74+
in stdenv.mkDerivation rec {
75+
inherit version;
76+
pname = "peertube";
77+
src = patchedSource;
78+
buildPhase = ''
79+
ln -s ${server.modules}/node_modules .
80+
rm -rf dist && cp -a ${server.dist}/dist dist
81+
rm -rf client/dist && cp -a ${client.dist}/dist client/
82+
'';
83+
installPhase = ''
84+
mkdir $out
85+
cp -a * $out
86+
ln -s /tmp $out/.cache
87+
'';
88+
89+
meta = {
90+
description = "A free software to take back control of your videos";
91+
92+
longDescription = ''
93+
PeerTube aspires to be a decentralized and free/libre alternative to video
94+
broadcasting services.
95+
PeerTube is not meant to become a huge platform that would centralize
96+
videos from all around the world. Rather, it is a network of
97+
inter-connected small videos hosters.
98+
Anyone with a modicum of technical skills can host a PeerTube server, aka
99+
an instance. Each instance hosts its users and their videos. In this way,
100+
every instance is created, moderated and maintained independently by
101+
various administrators.
102+
You can still watch from your account videos hosted by other instances
103+
though if the administrator of your instance had previously connected it
104+
with other instances.
105+
'';
106+
107+
license = stdenv.lib.licenses.agpl3Plus;
108+
109+
homepage = "https://joinpeertube.org/";
110+
111+
platforms = stdenv.lib.platforms.unix;
112+
113+
maintainers = with stdenv.lib.maintainers; [ immae stevenroose ];
114+
};
115+
}
116+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
diff --git a/client/yarn.lock b/client/yarn.lock
2+
index d27cdaec8..26706a9fc 100644
3+
--- a/client/yarn.lock
4+
+++ b/client/yarn.lock
5+
@@ -5703,7 +5703,8 @@ http-errors@~1.7.2:
6+
7+
"http-node@github:feross/http-node#webtorrent":
8+
version "1.2.0"
9+
- resolved "https://codeload.github.com/feross/http-node/tar.gz/342ef8624495343ffd050bd0808b3750cf0e3974"
10+
+ resolved "https://codeload.github.com/feross/http-node/tar.gz/342ef8624495343ffd050bd0808b3750cf0e3974#33fa312d37f0000b17acdb1a5086565400419a13"
11+
+ integrity sha1-M/oxLTfwAAsXrNsaUIZWVABBmhM=
12+
dependencies:
13+
chrome-net "^3.3.3"
14+
freelist "^1.0.3"
15+
diff --git a/yarn.lock b/yarn.lock
16+
index 61a2ea05e..c742276c7 100644
17+
--- a/yarn.lock
18+
+++ b/yarn.lock
19+
@@ -3873,7 +3873,8 @@ http-errors@~1.7.2:
20+
21+
"http-node@github:feross/http-node#webtorrent":
22+
version "1.2.0"
23+
- resolved "https://codeload.github.com/feross/http-node/tar.gz/342ef8624495343ffd050bd0808b3750cf0e3974"
24+
+ resolved "https://codeload.github.com/feross/http-node/tar.gz/342ef8624495343ffd050bd0808b3750cf0e3974#33fa312d37f0000b17acdb1a5086565400419a13"
25+
+ integrity sha1-M/oxLTfwAAsXrNsaUIZWVABBmhM=
26+
dependencies:
27+
chrome-net "^3.3.3"
28+
freelist "^1.0.3"

0 commit comments

Comments
 (0)