about summary refs log tree commit diff
path: root/pkgs/applications/video/mpv/scripts/buildLua.nix
blob: 2e25ee55b28a0dc57f4ad702c21dc1d02e48e4e5 (plain) (blame)
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
{ lib, stdenvNoCC }:

let
  # Escape strings for embedding in shell scripts
  escaped = s: "'${lib.escape [ "'" ] s}'";
  escapedList = lib.concatMapStringsSep " " escaped;

  fileName = pathStr: lib.last (lib.splitString "/" pathStr);
  scriptsDir = "$out/share/mpv/scripts";

  # similar to `lib.extends`, but with inverted precedence and recursive update
  extendedBy =
    args: orig: self:
    let
      super = args self;
    in
    lib.recursiveUpdate (orig super) super;
in

lib.makeOverridable (
  args:
  stdenvNoCC.mkDerivation (
    extendedBy (if lib.isFunction args then args else (_: args)) (
      {
        pname,
        extraScripts ? [ ],
        ...
      }@args:
      let
        strippedName =
          with builtins;
          let
            groups = match "mpv[-_](.*)" pname;
          in
          if groups != null then head groups else pname;
        # either passthru.scriptName, inferred from scriptPath, or from pname
        scriptName =
          (args.passthru or { }).scriptName
            or (if args ? scriptPath then fileName args.scriptPath else "${strippedName}.lua");
        scriptPath = args.scriptPath or "./${scriptName}";
      in
      {
        dontBuild = true;
        preferLocalBuild = true;

        # Prevent `patch` from emitting `.orig` files (that end up in the output)
        patchFlags = [
          "--no-backup-if-mismatch"
          "-p1"
        ];

        outputHashMode = "recursive";
        installPhase = ''
          runHook preInstall

          if [ -d "${scriptPath}" ]; then
            [ -f "${scriptPath}/main.lua" ] || {
              echo "Script directory '${scriptPath}' does not contain 'main.lua'" >&2
              exit 1
            }
            [ ${with builtins; toString (length extraScripts)} -eq 0 ] || {
              echo "mpvScripts.buildLua does not support 'extraScripts'" \
                   "when 'scriptPath' is a directory" >&2
              exit 1
            }
            mkdir -p "${scriptsDir}"
            cp -a "${scriptPath}" "${scriptsDir}/${scriptName}"
          else
            install -m644 -Dt "${scriptsDir}" ${escaped scriptPath}
            ${
              lib.optionalString (extraScripts != [ ]) ''cp -at "${scriptsDir}/" ${escapedList extraScripts}''
            }
          fi

          runHook postInstall
        '';

        passthru = {
          inherit scriptName;
        };
        meta =
          {
            platforms = lib.platforms.all;
          }
          // (
            let
              pos =
                if (args.meta or { }) ? description then
                  builtins.unsafeGetAttrPos "description" args.meta
                else
                  builtins.unsafeGetAttrPos "pname" args;
            in
            lib.optionalAttrs (pos != null) { position = "${pos.file}:${toString pos.line}"; }
          );
      }
    )
  )
)