Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add peertube #84189

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions maintainers/maintainer-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3541,6 +3541,12 @@
githubId = 4085046;
name = "Imuli";
};
immae = {
email = "ismael@bouya.org";
github = "immae";
githubId = 510202;
name = "Immae";
};
infinisil = {
email = "contact@infinisil.com";
github = "infinisil";
Expand Down
2 changes: 2 additions & 0 deletions nixos/modules/misc/ids.nix
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ in
paperless = 315;
#mailman = 316; # removed 2019-08-30
zigbee2mqtt = 317;
peertube = 318;

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

Expand Down Expand Up @@ -647,6 +648,7 @@ in
paperless = 315;
#mailman = 316; # removed 2019-08-30
zigbee2mqtt = 317;
peertube = 318;

# When adding a gid, make sure it doesn't match an existing
# uid. Users and groups with the same name should have equal
Expand Down
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@
./services/web-apps/moodle.nix
./services/web-apps/nextcloud.nix
./services/web-apps/nexus.nix
./services/web-apps/peertube/default.nix
./services/web-apps/pgpkeyserver-lite.nix
./services/web-apps/matomo.nix
./services/web-apps/moinmoin.nix
Expand Down
177 changes: 177 additions & 0 deletions nixos/modules/services/web-apps/peertube/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{ lib, pkgs, config, ... }:

let
name = "peertube";
cfg = config.services.peertube;

uid = config.ids.uids.peertube;
gid = config.ids.gids.peertube;
in
{
options.services.peertube = {
enable = lib.mkEnableOption "Enable Peertube’s service";

user = lib.mkOption {
type = lib.types.str;
default = name;
description = "User account under which Peertube runs";
};

group = lib.mkOption {
type = lib.types.str;
default = name;
description = "Group under which Peertube runs";
};

dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/${name}";
description = ''
The directory where Peertube stores its data.
'';
};

database = lib.mkOption {
type = lib.types.str;
default = "peertube_prod";
description = ''
The Postgres database where Peertube stores its data.
'';
};

configFile = lib.mkOption {
type = lib.types.path;
description = ''
The configuration file path for Peertube.
'';
};

package = lib.mkOption {
type = lib.types.package;
default = pkgs.peertube;
description = ''
Peertube package to use.
'';
};

# Output variables
systemdStateDirectory = lib.mkOption {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for? I'm no Nix expert but the assert down seems to make this break if the dataDir is set to something outside of /var/lib.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this not be hardcoded to /var/run/${name}?

type = lib.types.str;

# Use ReadWritePaths= instead if varDir is outside of /var/lib
default = assert lib.strings.hasPrefix "/var/lib/" cfg.dataDir;
lib.strings.removePrefix "/var/lib/" cfg.dataDir;

description = ''
Adjusted Peertube data directory for systemd
'';

readOnly = true;
};
};

config = lib.mkIf cfg.enable {
users.users = lib.optionalAttrs (cfg.user == name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm is this a common pattern? To only create the user if it's the same name as the package?

"${name}" = {
inherit uid;
group = cfg.group;
description = "Peertube user";
home = cfg.dataDir;
useDefaultShell = true;
# todo: fix this. needed for postgres authentication
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea no how to fix this?

password = "peertube";
};
};
users.groups = lib.optionalAttrs (cfg.group == name) {
"${name}" = {
inherit gid;
};
};

services.redis = {
enable = true;
};

services.postgresql = {
enable = true;
package = pkgs.postgresql_12;
# requires sudo -u postgres createdb -O peertube -E UTF8 -T template0 ${cfg.database}
# so this may not suffice
# ensureDatabases = [ "${cfg.database}" ];
ensureUsers = [
{
name = "${cfg.user}";
# we create database with `peertube` as owner in `preStart`
# ensurePermissions = {
# "DATABASE ${cfg.database}" = "ALL PRIVILEGES";
# };
}
];
authentication = ''
host ${cfg.database} ${cfg.user} 127.0.0.1/32 trust
host ${cfg.database} ${cfg.user} 127.0.0.1/32 md5
'';

};

systemd.tmpfiles.rules = [
"d \"${cfg.dataDir}\" - ${cfg.user} ${cfg.group} - -"
];

systemd.services.peertube = {
description = "Peertube";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "postgresql.service" "redis.service" ];
wants = [ "postgresql.service" "redis.service" ];

environment.NODE_CONFIG_DIR = "${cfg.dataDir}/config";
environment.NODE_ENV = "production";
environment.HOME = cfg.package;

path = [ pkgs.nodejs pkgs.bashInteractive pkgs.ffmpeg pkgs.openssl pkgs.sudo ];

script = ''
install -m 0750 -d ${cfg.dataDir}/config
ln -sf ${cfg.configFile} ${cfg.dataDir}/config/production.yaml
exec npm run start
'';

serviceConfig = {
User = cfg.user;
Group = cfg.group;
WorkingDirectory = cfg.package;
StateDirectory = cfg.systemdStateDirectory;
StateDirectoryMode = 0750;
PrivateTmp = true;
ProtectHome = true;
ProtectControlGroups = true;
Restart = "always";
Type = "simple";
TimeoutSec = 60;
ExecStartPre = let script = pkgs.writeScript "peertube-pre-start.sh" ''
#!/bin/sh
set -e

if ! [ -e "${cfg.dataDir}/.first_run" ]; then
set -v
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you indent those lines inside the if block?

if [ -e "${cfg.dataDir}/.first_run_partial" ]; then
echo "Warn: first run was interrupted"
fi
touch "${cfg.dataDir}/.first_run_partial"

sudo -u postgres "${config.services.postgresql.package}/bin/createdb" -O ${cfg.user} -E UTF8 -T template0 ${cfg.database}
sudo -u postgres "${config.services.postgresql.package}/bin/psql" -c "CREATE EXTENSION pg_trgm;" ${cfg.database}
sudo -u postgres "${config.services.postgresql.package}/bin/psql" -c "CREATE EXTENSION unaccent;" ${cfg.database}

touch "${cfg.dataDir}/.first_run"
rm "${cfg.dataDir}/.first_run_partial"
fi
'';
in "+${script}";
};

unitConfig.RequiresMountsFor = cfg.dataDir;
};
};
}

Loading