Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / servers / mastodon / default.nix
blob7b20fe7038f9417438a8519016eb27495b7c9be1
1 { lib, stdenv, nodejs-slim, bundlerEnv, nixosTests
2 , yarn, callPackage, imagemagick, ffmpeg, file, ruby, writeShellScript
3 , fetchYarnDeps, fixup_yarn_lock
4 , brotli
6   # Allow building a fork or custom version of Mastodon:
7 , pname ? "mastodon"
8 , version ? srcOverride.version
9   # src is a package
10 , srcOverride ? callPackage ./source.nix {}
11 , gemset ? ./. + "/gemset.nix"
12 , yarnHash ? srcOverride.yarnHash
15 stdenv.mkDerivation rec {
16   inherit pname version;
18   src = srcOverride;
20   mastodonGems = bundlerEnv {
21     name = "${pname}-gems-${version}";
22     inherit version gemset ruby;
23     gemdir = src;
24     # This fix (copied from https://github.com/NixOS/nixpkgs/pull/76765) replaces the gem
25     # symlinks with directories, resolving this error when running rake:
26     #   /nix/store/451rhxkggw53h7253izpbq55nrhs7iv0-mastodon-gems-3.0.1/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/settings.rb:6:in `<module:Bundler>': uninitialized constant Bundler::Settings (NameError)
27     postBuild = ''
28       for gem in "$out"/lib/ruby/gems/*/gems/*; do
29         cp -a "$gem/" "$gem.new"
30         rm "$gem"
31         # needed on macOS, otherwise the mv yields permission denied
32         chmod +w "$gem.new"
33         mv "$gem.new" "$gem"
34       done
35     '';
36   };
38   mastodonModules = stdenv.mkDerivation {
39     pname = "${pname}-modules";
40     inherit src version;
42     yarnOfflineCache = fetchYarnDeps {
43       yarnLock = "${src}/yarn.lock";
44       hash = yarnHash;
45     };
47     nativeBuildInputs = [ fixup_yarn_lock nodejs-slim yarn mastodonGems mastodonGems.wrappedRuby brotli ];
49     RAILS_ENV = "production";
50     NODE_ENV = "production";
52     buildPhase = ''
53       runHook preBuild
55       export HOME=$PWD
56       # This option is needed for openssl-3 compatibility
57       # Otherwise we encounter this upstream issue: https://github.com/mastodon/mastodon/issues/17924
58       export NODE_OPTIONS=--openssl-legacy-provider
59       fixup_yarn_lock ~/yarn.lock
60       yarn config --offline set yarn-offline-mirror $yarnOfflineCache
61       yarn install --offline --frozen-lockfile --ignore-engines --ignore-scripts --no-progress
63       patchShebangs ~/bin
64       patchShebangs ~/node_modules
66       # skip running yarn install
67       rm -rf ~/bin/yarn
69       OTP_SECRET=precompile_placeholder SECRET_KEY_BASE=precompile_placeholder \
70         rails assets:precompile
71       yarn cache clean --offline
72       rm -rf ~/node_modules/.cache
74       # Create missing static gzip and brotli files
75       gzip --best --keep ~/public/assets/500.html
76       gzip --best --keep ~/public/packs/report.html
77       find ~/public/assets -maxdepth 1 -type f -name '.*.json' \
78         -exec gzip --best --keep --force {} ';'
79       brotli --best --keep ~/public/packs/report.html
80       find ~/public/assets -type f -regextype posix-extended -iregex '.*\.(css|js|json|html)' \
81         -exec brotli --best --keep {} ';'
83       runHook postBuild
84     '';
86     installPhase = ''
87       runHook preInstall
89       mkdir -p $out/public
90       cp -r node_modules $out/node_modules
91       cp -r public/assets $out/public
92       cp -r public/packs $out/public
94       runHook postInstall
95     '';
96   };
98   propagatedBuildInputs = [ imagemagick ffmpeg file mastodonGems.wrappedRuby ];
99   buildInputs = [ mastodonGems nodejs-slim ];
101   buildPhase = ''
102     runHook preBuild
104     ln -s $mastodonModules/node_modules node_modules
105     ln -s $mastodonModules/public/assets public/assets
106     ln -s $mastodonModules/public/packs public/packs
108     patchShebangs bin/
109     for b in $(ls $mastodonGems/bin/)
110     do
111       if [ ! -f bin/$b ]; then
112         ln -s $mastodonGems/bin/$b bin/$b
113       fi
114     done
116     # Remove execute permissions
117     chmod 0444 public/emoji/*.svg
119     # Create missing static gzip and brotli files
120     find public -maxdepth 1 -type f -regextype posix-extended -iregex '.*\.(css|js|svg|txt|xml)' \
121       -exec gzip --best --keep --force {} ';' \
122       -exec brotli --best --keep {} ';'
123     find public/emoji -type f -name '.*.svg' \
124       -exec gzip --best --keep --force {} ';' \
125       -exec brotli --best --keep {} ';'
126     ln -s assets/500.html.gz public/500.html.gz
127     ln -s assets/500.html.br public/500.html.br
128     ln -s packs/sw.js.gz public/sw.js.gz
129     ln -s packs/sw.js.br public/sw.js.br
130     ln -s packs/sw.js.map.gz public/sw.js.map.gz
131     ln -s packs/sw.js.map.br public/sw.js.map.br
133     rm -rf log
134     ln -s /var/log/mastodon log
135     ln -s /tmp tmp
137     runHook postBuild
138   '';
140   installPhase = let
141     run-streaming = writeShellScript "run-streaming.sh" ''
142       # NixOS helper script to consistently use the same NodeJS version the package was built with.
143       ${nodejs-slim}/bin/node ./streaming
144     '';
145   in ''
146     runHook preInstall
148     mkdir -p $out
149     cp -r * $out/
150     ln -s ${run-streaming} $out/run-streaming.sh
152     runHook postInstall
153   '';
155   passthru = {
156     tests.mastodon = nixosTests.mastodon;
157     # run with: nix-shell ./maintainers/scripts/update.nix --argstr package mastodon
158     updateScript = ./update.sh;
159   };
161   meta = with lib; {
162     description = "Self-hosted, globally interconnected microblogging software based on ActivityPub";
163     homepage = "https://joinmastodon.org";
164     license = licenses.agpl3Plus;
165     platforms = [ "x86_64-linux" "i686-linux" "aarch64-linux" ];
166     maintainers = with maintainers; [ happy-river erictapen izorkin ghuntley ];
167   };