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

let
  escapedList = with lib; concatMapStringsSep " " (s: "'${escape [ "'" ] s}'");
  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"
          exit 1
        }
        mkdir -p "${scriptsDir}"
        cp -a "${scriptPath}" "${scriptsDir}/${scriptName}"
      else
        install -m644 -Dt "${scriptsDir}" \
          ${escapedList ([ scriptPath ] ++ 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}"; }
    );
  })
))