2 description = "A free software to take back control of your videos";
4 url = "https://git.immae.eu/perso/Immae/Config/Nix.git";
8 inputs.flake-utils.url = "github:numtide/flake-utils";
9 inputs.nixpkgs.url = "github:NixOS/nixpkgs";
11 url = "github:Chocobozzz/PeerTube/v3.0.1";
15 outputs = { self, myuids, nixpkgs, peertube, flake-utils }: flake-utils.lib.eachSystem ["x86_64-linux"] (system:
17 pkgs = import nixpkgs { inherit system; overlays = [
18 (self: super: { nodejs = self.nodejs-12_x; })
20 inherit (pkgs) callPackage stdenv jq youtube-dl fetchurl nodePackages yarn2nix-moretea;
22 patchedSource = stdenv.mkDerivation {
24 version = peertube.rev;
26 phases = [ "unpackPhase" "patchPhase" "installPhase" ];
27 patches = [ ./fix_yarn_lock.patch ];
35 buildInputs = [ nodePackages.node-pre-gyp ];
37 bcrypt_version = "5.0.0";
38 bcrypt_lib = fetchurl {
39 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";
40 sha256 = "0j3p2px1xb17sw3gpm8l4apljajxxfflal1yy552mhpzhi21wccn";
44 if [ "${bcrypt_version}" != "$(cat package.json | ${jq}/bin/jq -r .version)" ]; then
45 echo "Mismatching version please update bcrypt in derivation"
48 mkdir -p lib/binding && tar -C lib/binding -xf ${bcrypt_lib}
49 patchShebangs ../node-pre-gyp
54 buildInputs = [ nodePackages.node-gyp-build ];
59 ln -s ${youtube-dl}/bin/youtube-dl bin/youtube-dl
60 cat > bin/details <<EOF
61 {"version":"${youtube-dl.version}","path":null,"exec":"youtube-dl"}
66 mkYarnModules' = args: (yarn2nix-moretea.mkYarnModules args).overrideAttrs(old: {
67 # This hack permits to workaround the fact that the yarn.lock
68 # file doesn't respect the semver requirements
69 buildPhase = builtins.replaceStrings [" ./package.json"] [" /dev/null; cp deps/*/package.json ."] old.buildPhase;
72 server = callPackage ./server.nix {
73 inherit yarnModulesConfig mkYarnModules';
74 sources = patchedSource;
76 client = callPackage ./client.nix {
77 inherit server yarnModulesConfig mkYarnModules';
78 sources = patchedSource;
82 packages.peertube = callPackage ./. { inherit server client; src = patchedSource; };
83 defaultPackage = packages.peertube;
84 legacyPackages.peertube = packages.peertube;
86 build = defaultPackage;
87 } // pkgs.lib.optionalAttrs (builtins.elem system pkgs.lib.systems.doubles.linux) {
89 let testing = import (nixpkgs + "/nixos/lib/testing-python.nix") { inherit system; };
92 client = { pkgs, ... }: {};
94 server = { pkgs, ... }: {
95 imports = [ self.nixosModule ];
96 config.networking.firewall.allowedTCPPorts = [ 8888 ];
97 config.services.redis.enable = true;
98 config.services.postgresql = {
100 initialScript = pkgs.writeText "peertube.sql" ''
101 CREATE ROLE "peertube" WITH LOGIN PASSWORD 'peertube';
102 CREATE DATABASE "peertube_prod" WITH OWNER "peertube"
107 CREATE EXTENSION unaccent;
108 CREATE EXTENSION pg_trgm;
111 config.services.peertube.enable = true;
112 config.services.peertube.configFile = pkgs.writeText "peertube.conf" ''
118 hostname: 'localhost.tld'
121 hostname: 'localhost'
133 tmp: '/var/lib/peertube/storage/tmp/'
134 avatars: '/var/lib/peertube/storage/avatars/'
135 videos: '/var/lib/peertube/storage/videos/'
136 streaming_playlists: '/var/lib/peertube/storage/streaming-playlists/'
137 redundancy: '/var/lib/peertube/storage/videos/'
138 logs: '/var/lib/peertube/storage/logs/'
139 previews: '/var/lib/peertube/storage/previews/'
140 thumbnails: '/var/lib/peertube/storage/thumbnails/'
141 torrents: '/var/lib/peertube/storage/torrents/'
142 captions: '/var/lib/peertube/storage/captions/'
143 cache: '/var/lib/peertube/storage/cache/'
144 plugins: '/var/lib/peertube/storage/plugins/'
145 client_overrides: '/var/lib/peertube/storage/client-overrides/'
151 server.wait_for_unit("peertube.service")
152 server.wait_for_open_port(8888)
153 client.succeed("curl http://server:8888")
154 client.succeed("curl http://server:8888/client/fr-FR/index.html")
161 peertube = final: prev: {
162 peertube = self.defaultPackage."${final.system}";
165 overlay = overlays.peertube;
166 nixosModule = { lib, pkgs, config, ... }:
169 cfg = config.services.peertube;
172 options.services.peertube = {
173 enable = lib.mkEnableOption "Enable Peertube’s service";
174 user = lib.mkOption {
175 type = lib.types.str;
177 description = "User account under which Peertube runs";
179 group = lib.mkOption {
180 type = lib.types.str;
182 description = "Group under which Peertube runs";
184 dataDir = lib.mkOption {
185 type = lib.types.path;
186 default = "/var/lib/${name}";
188 The directory where Peertube stores its data.
191 configFile = lib.mkOption {
192 type = lib.types.path;
194 The configuration file path for Peertube.
197 package = lib.mkOption {
198 type = lib.types.package;
199 default = pkgs.peertube;
201 Peertube package to use.
205 systemdStateDirectory = lib.mkOption {
206 type = lib.types.str;
207 # Use ReadWritePaths= instead if varDir is outside of /var/lib
208 default = assert lib.strings.hasPrefix "/var/lib/" cfg.dataDir;
209 lib.strings.removePrefix "/var/lib/" cfg.dataDir;
211 Adjusted Peertube data directory for systemd
217 config = lib.mkIf cfg.enable {
218 nixpkgs.overlays = [ self.overlay ];
219 users.users = lib.optionalAttrs (cfg.user == name) {
221 uid = myuids.lib.uids.peertube;
223 description = "Peertube user";
225 useDefaultShell = true;
228 users.groups = lib.optionalAttrs (cfg.group == name) {
230 gid = myuids.lib.gids.peertube;
234 systemd.services.peertube = {
235 description = "Peertube";
236 wantedBy = [ "multi-user.target" ];
237 after = [ "network.target" "postgresql.service" ];
238 wants = [ "postgresql.service" ];
240 environment.NODE_CONFIG_DIR = "${cfg.dataDir}/config";
241 environment.NODE_ENV = "production";
242 environment.HOME = cfg.package;
244 path = [ pkgs.nodejs pkgs.yarn pkgs.bashInteractive pkgs.ffmpeg pkgs.openssl ];
247 install -m 0750 -d ${cfg.dataDir}/config
248 ln -sf ${cfg.configFile} ${cfg.dataDir}/config/production.yaml
249 ln -sf ${cfg.package}/config/default.yaml ${cfg.dataDir}/config/default.yaml
256 WorkingDirectory = cfg.package;
257 StateDirectory = cfg.systemdStateDirectory;
258 StateDirectoryMode = 0750;
261 ProtectControlGroups = true;
267 unitConfig.RequiresMountsFor = cfg.dataDir;