summaryrefslogtreecommitdiff
path: root/pkgs/servers/web-apps/wordpress/packages/default.nix
blob: d874bf3b4db2279dd0a0853e5ef845e8772f6029 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Source: https://git.helsinki.tools/helsinki-systems/wp4nix/-/blob/master/default.nix
# Licensed under: MIT
# Slightly modified

{
  lib,
  newScope,
  plugins,
  themes,
  languages,
  callPackage,
}:

let
  packages =
    self:
    let
      generatedJson = {
        inherit plugins themes languages;
      };
      sourceJson = {
        plugins = builtins.fromJSON (builtins.readFile ./wordpress-plugins.json);
        themes = builtins.fromJSON (builtins.readFile ./wordpress-themes.json);
        languages = builtins.fromJSON (builtins.readFile ./wordpress-languages.json);
      };

    in
    {
      # Create a generic WordPress package. Most arguments are just passed
      # to `mkDerivation`. The version is automatically filtered for weird characters.
      mkWordpressDerivation = self.callPackage (
        {
          stdenvNoCC,
          lib,
          filterWPString,
          gettext,
          wp-cli,
        }:
        {
          type,
          pname,
          version,
          license,
          ...
        }@args:
        assert lib.any (x: x == type) [
          "plugin"
          "theme"
          "language"
        ];
        stdenvNoCC.mkDerivation (
          {
            pname = "wordpress-${type}-${pname}";
            version = filterWPString version;

            dontConfigure = true;
            dontBuild = true;

            installPhase = ''
              runHook preInstall
              cp -R ./. $out
              runHook postInstall
            '';

            passthru = {
              wpName = pname;
            };

            meta = {
              license = lib.licenses.${license};
            } // (args.passthru or { });
          }
          // lib.optionalAttrs (type == "language") {
            nativeBuildInputs = [
              gettext
              wp-cli
            ];
            dontBuild = false;
            buildPhase = ''
              runHook preBuild

              find -name '*.po' -print0 | while IFS= read -d "" -r po; do
                msgfmt -o $(basename "$po" .po).mo "$po"
              done
              wp i18n make-json .
              rm *.po

              runHook postBuild
            '';
          }
          // removeAttrs args [
            "type"
            "pname"
            "version"
            "passthru"
          ]
        )
      ) { };

      # Create a derivation from the official wordpress.org packages.
      # This takes the type, the pname and the data generated from the go tool.
      mkOfficialWordpressDerivation = self.callPackage (
        { mkWordpressDerivation, fetchWordpress }:
        {
          type,
          pname,
          data,
          license,
        }:
        mkWordpressDerivation {
          inherit type pname license;
          version = data.version;

          src = fetchWordpress type data;
        }
      ) { };

      # Filter out all characters that might occur in a version string but that that are not allowed
      # in store paths.
      filterWPString =
        builtins.replaceStrings
          [
            " "
            ","
            "/"
            "&"
            ";"
            ''"''
            "'"
            "$"
            ":"
            "("
            ")"
            "["
            "]"
            "{"
            "}"
            "|"
            "*"
            "\t"
          ]
          [
            "_"
            "."
            "."
            ""
            ""
            ""
            ""
            ""
            ""
            ""
            ""
            ""
            ""
            ""
            ""
            "-"
            ""
            ""
          ];

      # Fetch a package from the official wordpress.org SVN.
      # The data supplied is the data straight from the go tool.
      fetchWordpress = self.callPackage (
        { fetchsvn }:
        type: data:
        fetchsvn {
          inherit (data) rev sha256;
          url =
            if type == "plugin" || type == "theme" then
              "https://" + type + "s.svn.wordpress.org/" + data.path
            else if type == "language" then
              "https://i18n.svn.wordpress.org/core/" + data.version + "/" + data.path
            else if type == "pluginLanguage" then
              "https://i18n.svn.wordpress.org/plugins/" + data.path
            else if type == "themeLanguage" then
              "https://i18n.svn.wordpress.org/themes/" + data.path
            else
              throw "fetchWordpress: invalid package type ${type}";
        }
      ) { };

    }
    // lib.mapAttrs (
      type: pkgs:
      lib.makeExtensible (
        _:
        lib.mapAttrs (
          pname: data:
          self.mkOfficialWordpressDerivation {
            type = lib.removeSuffix "s" type;
            inherit pname data;
            license = sourceJson.${type}.${pname};
          }
        ) pkgs
      )
    ) generatedJson;

in
# This creates an extensible scope.
lib.recursiveUpdate ((lib.makeExtensible (_: (lib.makeScope newScope packages))).extend (
  selfWP: superWP: { }
)) (callPackage ./thirdparty.nix { })