1{ lib, stdenvNoCC }:
2
3let
4 # Escape strings for embedding in shell scripts
5 escaped = s: "'${lib.escape [ "'" ] s}'";
6 escapedList = lib.concatMapStringsSep " " escaped;
7
8 fileName = pathStr: lib.last (lib.splitString "/" pathStr);
9 scriptsDir = "$out/share/mpv/scripts";
10
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;
18in
19
20lib.makeOverridable (
21 args:
22 stdenvNoCC.mkDerivation (
23 extendedBy (if lib.isFunction args then args else (_: args)) (
24 {
25 pname,
26 extraScripts ? [ ],
27 runtime-dependencies ? [ ],
28 ...
29 }@args:
30 let
31 strippedName =
32 with builtins;
33 let
34 groups = match "mpv[-_](.*)" pname;
35 in
36 if groups != null then head groups else pname;
37 # either passthru.scriptName, inferred from scriptPath, or from pname
38 scriptName =
39 (args.passthru or { }).scriptName
40 or (if args ? scriptPath then fileName args.scriptPath else "${strippedName}.lua");
41 scriptPath = args.scriptPath or "./${scriptName}";
42 in
43 {
44 dontBuild = true;
45 preferLocalBuild = true;
46
47 # Prevent `patch` from emitting `.orig` files (that end up in the output)
48 patchFlags = [
49 "--no-backup-if-mismatch"
50 "-p1"
51 ];
52
53 outputHashMode = "recursive";
54 installPhase = ''
55 runHook preInstall
56
57 if [ -d "${scriptPath}" ]; then
58 [ -f "${scriptPath}/main.lua" ] || {
59 echo "Script directory '${scriptPath}' does not contain 'main.lua'" >&2
60 exit 1
61 }
62 [ ${with builtins; toString (length extraScripts)} -eq 0 ] || {
63 echo "mpvScripts.buildLua does not support 'extraScripts'" \
64 "when 'scriptPath' is a directory" >&2
65 exit 1
66 }
67 mkdir -p "${scriptsDir}"
68 cp -a "${scriptPath}" "${scriptsDir}/${scriptName}"
69 else
70 install -m644 -Dt "${scriptsDir}" ${escaped scriptPath}
71 ${lib.optionalString (
72 extraScripts != [ ]
73 ) ''cp -at "${scriptsDir}/" ${escapedList extraScripts}''}
74 fi
75
76 runHook postInstall
77 '';
78
79 passthru = {
80 inherit scriptName;
81 }
82 // lib.optionalAttrs (runtime-dependencies != [ ]) {
83 extraWrapperArgs = [
84 "--prefix"
85 "PATH"
86 ":"
87 (lib.makeBinPath runtime-dependencies)
88 ]
89 ++ args.passthru.extraWrapperArgs or [ ];
90 };
91 meta = {
92 platforms = lib.platforms.all;
93 }
94 // (
95 let
96 pos =
97 if (args.meta or { }) ? description then
98 builtins.unsafeGetAttrPos "description" args.meta
99 else
100 builtins.unsafeGetAttrPos "pname" args;
101 in
102 lib.optionalAttrs (pos != null) { position = "${pos.file}:${toString pos.line}"; }
103 );
104 }
105 )
106 )
107)