nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 nodejs-slim,
5 bundlerEnv,
6 nixosTests,
7 yarn-berry,
8 callPackage,
9 ruby,
10 writeShellScript,
11 brotli,
12 python3,
13
14 # Allow building a fork or custom version of Mastodon:
15 pname ? "mastodon",
16 version ? srcOverride.version,
17 patches ? [ ],
18 # src is a package
19 srcOverride ? callPackage ./source.nix { inherit patches; },
20 gemset ? ./. + "/gemset.nix",
21 yarnHash ? srcOverride.yarnHash,
22 yarnMissingHashes ? srcOverride.yarnMissingHashes,
23}:
24
25stdenv.mkDerivation rec {
26 inherit pname version;
27
28 src = srcOverride;
29
30 mastodonGems = bundlerEnv {
31 name = "${pname}-gems-${version}";
32 inherit version gemset ruby;
33 gemdir = src;
34 };
35
36 mastodonModules = stdenv.mkDerivation (finalAttrs: {
37 pname = "${pname}-modules";
38 inherit src version;
39
40 missingHashes = yarnMissingHashes;
41 yarnOfflineCache = yarn-berry.fetchYarnBerryDeps {
42 inherit src;
43 hash = yarnHash;
44 missingHashes = yarnMissingHashes;
45 };
46
47 nativeBuildInputs = [
48 nodejs-slim
49 yarn-berry
50 yarn-berry.yarnBerryConfigHook
51 mastodonGems
52 mastodonGems.wrappedRuby
53 brotli
54 python3
55 ];
56
57 RAILS_ENV = "production";
58 NODE_ENV = "production";
59
60 buildPhase = ''
61 runHook preBuild
62
63 export SECRET_KEY_BASE_DUMMY=1
64
65 patchShebangs bin
66
67 bundle exec rails assets:precompile
68
69 # Install packages for streaming server while remove others
70 rm -rf node_modules/*
71 yarn workspaces focus --production @mastodon/streaming
72 rm -rf node_modules/.cache
73
74 # Remove workspace "package" as it contains broken symlinks
75 # See https://github.com/NixOS/nixpkgs/issues/380366
76 rm -rf node_modules/@mastodon
77
78 # Remove execute permissions
79 find public/assets -type f ! -perm 0555 \
80 -exec chmod 0444 {} ';'
81
82 # Create missing static gzip and brotli files
83 find public/assets -type f -regextype posix-extended -iregex '.*\.(css|html|js|json|svg)' \
84 -exec gzip --best --keep --force {} ';' \
85 -exec brotli --best --keep {} ';'
86 find public/packs -type f -regextype posix-extended -iregex '.*\.(css|js|json|svg)' \
87 -exec brotli --best --keep {} ';'
88 gzip --best --keep public/packs/sw.js
89
90 runHook postBuild
91 '';
92
93 installPhase = ''
94 runHook preInstall
95
96 mkdir -p $out/public
97 cp -r node_modules $out/node_modules
98 cp -r public/assets $out/public
99 cp -r public/packs $out/public
100
101 runHook postInstall
102 '';
103 });
104
105 propagatedBuildInputs = [ mastodonGems.wrappedRuby ];
106 nativeBuildInputs = [ brotli ];
107 buildInputs = [
108 mastodonGems
109 nodejs-slim
110 ];
111
112 buildPhase = ''
113 runHook preBuild
114
115 ln -s $mastodonModules/node_modules node_modules
116 ln -s $mastodonModules/public/assets public/assets
117 ln -s $mastodonModules/public/packs public/packs
118
119 patchShebangs bin/
120 for b in $(ls $mastodonGems/bin/)
121 do
122 if [ ! -f bin/$b ]; then
123 ln -s $mastodonGems/bin/$b bin/$b
124 fi
125 done
126
127 # Remove execute permissions
128 find public/emoji -type f ! -perm 0555 \
129 -exec chmod 0444 {} ';'
130 find public -maxdepth 1 -type f ! -perm 0555 \
131 -exec chmod 0444 {} ';'
132
133 # Create missing static gzip and brotli files
134 find public -maxdepth 1 -type f -regextype posix-extended -iregex '.*\.(js|txt)' \
135 -exec gzip --best --keep --force {} ';' \
136 -exec brotli --best --keep {} ';'
137 find public/emoji -type f -name '*.svg' \
138 -exec gzip --best --keep --force {} ';' \
139 -exec brotli --best --keep {} ';'
140 ln -s assets/500.html.gz public/500.html.gz
141 ln -s assets/500.html.br public/500.html.br
142 ln -s packs/sw.js.gz public/sw.js.gz
143 ln -s packs/sw.js.br public/sw.js.br
144
145 rm -rf log
146 ln -s /var/log/mastodon log
147 ln -s /tmp tmp
148
149 runHook postBuild
150 '';
151
152 installPhase =
153 let
154 run-streaming = writeShellScript "run-streaming.sh" ''
155 # NixOS helper script to consistently use the same NodeJS version the package was built with.
156 ${nodejs-slim}/bin/node ./streaming
157 '';
158 in
159 ''
160 runHook preInstall
161
162 mkdir -p $out
163 mv .{env*,ruby*} $out/
164 mv * $out/
165 ln -s ${run-streaming} $out/run-streaming.sh
166
167 runHook postInstall
168 '';
169
170 passthru = {
171 tests.mastodon = nixosTests.mastodon;
172 # run with: nix-shell ./maintainers/scripts/update.nix --argstr package mastodon
173 updateScript = ./update.sh;
174 };
175
176 meta = {
177 description = "Self-hosted, globally interconnected microblogging software based on ActivityPub";
178 homepage = "https://joinmastodon.org";
179 license = lib.licenses.agpl3Plus;
180 platforms = [
181 "x86_64-linux"
182 "i686-linux"
183 "aarch64-linux"
184 ];
185 maintainers = with lib.maintainers; [
186 happy-river
187 erictapen
188 izorkin
189 ghuntley
190 ];
191 };
192}