1{
2 lib,
3 stdenv,
4 fetchurl,
5 formats,
6 installShellFiles,
7 makeWrapper,
8 versionCheckHook,
9 php,
10 writeScript,
11 nix-update,
12 common-updater-scripts,
13 phpIniFile ? null,
14}:
15
16let
17 version = "2.12.0";
18
19 completion = fetchurl {
20 url = "https://raw.githubusercontent.com/wp-cli/wp-cli/v${version}/utils/wp-completion.bash";
21 hash = "sha256-RDygYQzK6NLWrOug7EqnkpuH7Wz1T2Zq/tGNZjoYo5U=";
22 };
23
24 ini =
25 if phpIniFile == null then
26 (formats.ini { }).generate "php.ini" {
27 PHP.memory_limit = -1; # no limit as composer uses a lot of memory
28 Phar."phar.readonly" = "Off";
29 }
30 else
31 phpIniFile;
32
33in
34stdenv.mkDerivation (finalAttrs: {
35 pname = "wp-cli";
36 inherit version;
37
38 src = fetchurl {
39 url = "https://github.com/wp-cli/wp-cli/releases/download/v${version}/wp-cli-${version}.phar";
40 hash = "sha256-zjTd2Dj3NR1nWQaNCXk/JnVUY7SkYQpaXAqXtoIg2Fw=";
41 };
42
43 dontUnpack = true;
44 dontConfigure = true;
45 dontBuild = true;
46
47 nativeBuildInputs = [
48 installShellFiles
49 makeWrapper
50 ];
51
52 # Usually --set-default is used to set default environment variables.
53 # But the value is wrapped in single-quotes so our variables would be used literally instead
54 # of the expanded version. Thus we use --run instead.
55 installPhase = ''
56 runHook preInstall
57
58 install -Dm444 ${finalAttrs.src} $out/share/wp-cli/wp-cli.phar
59 install -Dm444 ${ini} $out/etc/${ini.name}
60 installShellCompletion --bash --name wp ${completion}
61
62 makeWrapper ${lib.getExe php} $out/bin/${finalAttrs.meta.mainProgram} \
63 --run 'export XDG_CACHE_HOME=''${XDG_CACHE_HOME-"$HOME/.cache"}' \
64 --run 'export XDG_CONFIG_HOME=''${XDG_CONFIG_HOME-"$HOME/.config"}' \
65 --run 'export XDG_DATA_HOME=''${XDG_DATA_HOME-"$HOME/.local/share"}' \
66 --run 'export WP_CLI_CONFIG_PATH=''${WP_CLI_CONFIG_PATH-"$XDG_CONFIG_HOME/wp-cli"}' \
67 --run 'export WP_CLI_PACKAGES_DIR=''${WP_CLI_PACKAGES_DIR-"$XDG_DATA_HOME/wp-cli"}' \
68 --run 'export WP_CLI_CACHE_DIR=''${WP_CLI_CACHE_DIR-"$XDG_CACHE_HOME/wp-cli"}' \
69 --add-flags "-c $out/etc/${ini.name}" \
70 --add-flags "-f $out/share/wp-cli/wp-cli.phar" \
71 --add-flags "--"
72
73 runHook postInstall
74 '';
75
76 doInstallCheck = true;
77
78 nativeInstallCheckInputs = [ versionCheckHook ];
79
80 versionCheckProgram = "${placeholder "out"}/bin/wp";
81
82 versionCheckProgramArg = "--info";
83
84 passthru = {
85 inherit completion;
86 updateScript = writeScript "update-wp-cli" ''
87 ${lib.getExe nix-update}
88 version=$(nix-instantiate --eval -E "with import ./. {}; wp-cli.version or (lib.getVersion wp-cli)" | tr -d '"')
89 ${lib.getExe' common-updater-scripts "update-source-version"} wp-cli $version --source-key=completion --ignore-same-version --ignore-same-hash
90 '';
91 };
92
93 meta = {
94 description = "Command line interface for WordPress";
95 homepage = "https://wp-cli.org";
96 changelog = "https://github.com/wp-cli/wp-cli/releases/tag/v${finalAttrs.version}";
97 license = lib.licenses.mit;
98 maintainers = with lib.maintainers; [ peterhoeg ];
99 platforms = lib.platforms.all;
100 mainProgram = "wp";
101 };
102})