electron_32: fix log spam when building on aarch64-linux (#378988)
[NixPkgs.git] / pkgs / applications / video / mpv / scripts / buildLua.nix
blob6d6831c1621bbaed99c5d6d9645be396168d2641
1 { lib, stdenvNoCC }:
3 let
4   # Escape strings for embedding in shell scripts
5   escaped = s: "'${lib.escape [ "'" ] s}'";
6   escapedList = lib.concatMapStringsSep " " escaped;
8   fileName = pathStr: lib.last (lib.splitString "/" pathStr);
9   scriptsDir = "$out/share/mpv/scripts";
11   # similar to `lib.extends`, but with inverted precedence and recursive update
12   extendedBy =
13     args: orig: self:
14     let
15       super = args self;
16     in
17     lib.recursiveUpdate (orig super) super;
20 lib.makeOverridable (
21   args:
22   stdenvNoCC.mkDerivation (
23     extendedBy (if lib.isFunction args then args else (_: args)) (
24       {
25         pname,
26         extraScripts ? [ ],
27         ...
28       }@args:
29       let
30         strippedName =
31           with builtins;
32           let
33             groups = match "mpv[-_](.*)" pname;
34           in
35           if groups != null then head groups else pname;
36         # either passthru.scriptName, inferred from scriptPath, or from pname
37         scriptName =
38           (args.passthru or { }).scriptName
39             or (if args ? scriptPath then fileName args.scriptPath else "${strippedName}.lua");
40         scriptPath = args.scriptPath or "./${scriptName}";
41       in
42       {
43         dontBuild = true;
44         preferLocalBuild = true;
46         # Prevent `patch` from emitting `.orig` files (that end up in the output)
47         patchFlags = [
48           "--no-backup-if-mismatch"
49           "-p1"
50         ];
52         outputHashMode = "recursive";
53         installPhase = ''
54           runHook preInstall
56           if [ -d "${scriptPath}" ]; then
57             [ -f "${scriptPath}/main.lua" ] || {
58               echo "Script directory '${scriptPath}' does not contain 'main.lua'" >&2
59               exit 1
60             }
61             [ ${with builtins; toString (length extraScripts)} -eq 0 ] || {
62               echo "mpvScripts.buildLua does not support 'extraScripts'" \
63                    "when 'scriptPath' is a directory" >&2
64               exit 1
65             }
66             mkdir -p "${scriptsDir}"
67             cp -a "${scriptPath}" "${scriptsDir}/${scriptName}"
68           else
69             install -m644 -Dt "${scriptsDir}" ${escaped scriptPath}
70             ${lib.optionalString (
71               extraScripts != [ ]
72             ) ''cp -at "${scriptsDir}/" ${escapedList extraScripts}''}
73           fi
75           runHook postInstall
76         '';
78         passthru = {
79           inherit scriptName;
80         };
81         meta =
82           {
83             platforms = lib.platforms.all;
84           }
85           // (
86             let
87               pos =
88                 if (args.meta or { }) ? description then
89                   builtins.unsafeGetAttrPos "description" args.meta
90                 else
91                   builtins.unsafeGetAttrPos "pname" args;
92             in
93             lib.optionalAttrs (pos != null) { position = "${pos.file}:${toString pos.line}"; }
94           );
95       }
96     )
97   )