summaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/pr/prl-tools/package.nix
blob: e1b6e9bf4abece7533913a1251a5205135fd560f (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
{
  autoPatchelfHook,
  bash,
  bbe,
  coreutils,
  cups,
  dbus,
  fetchurl,
  fuse,
  gawk,
  glib,
  lib,
  makeWrapper,
  netcat,
  p7zip,
  perl,
  stdenv,
  timetrap,
  undmg,
  util-linux,
  wayland,
  libxrandr,
  libxi,
  libxinerama,
  libxext,
  libxcomposite,
  libx11,
}:

let
  libPath = lib.concatStringsSep ":" [
    "${glib.out}/lib"
    "${libxrandr}/lib"
    "${wayland.out}/lib"
  ];
  scriptPath = lib.concatStringsSep ":" [
    "${bash}/bin"
    "${coreutils}/bin"
    "${cups}/sbin"
    "${gawk}/bin"
    "${netcat}/bin"
    "${timetrap}/bin"
    "${util-linux}/bin"
  ];
in
stdenv.mkDerivation (finalAttrs: {
  pname = "prl-tools";
  version = "26.4.2-57518";

  # We download the full distribution to extract prl-tools-lin.iso from
  # => ${dmg}/Parallels\ Desktop.app/Contents/Resources/Tools/prl-tools-lin.iso
  src = fetchurl {
    url = "https://download.parallels.com/desktop/v${lib.versions.major finalAttrs.version}/${finalAttrs.version}/ParallelsDesktop-${finalAttrs.version}.dmg";
    hash = "sha256-7VDr4mfHnDfsjf77wem8zcyHIpgV1RvvIOpp2C+SZTk=";
  };

  hardeningDisable = [
    "pic"
    "format"
  ];

  nativeBuildInputs = [
    autoPatchelfHook
    bbe
    makeWrapper
    p7zip
    perl
    undmg
  ];

  buildInputs = [
    fuse
    glib
    libx11
    libxcomposite
    libxext
    libxrandr
    libxi
    libxinerama
  ];

  runtimeDependencies = [
    dbus
    glib
    libxrandr
  ];

  unpackPhase = ''
    runHook preUnpack

    undmg $src
    export sourceRoot=prl-tools-build
    7z x "Parallels Desktop.app/Contents/Resources/Tools/prl-tools-lin${lib.optionalString stdenv.hostPlatform.isAarch64 "-arm"}.iso" -o$sourceRoot
    runHook postUnpack
  '';

  dontBuild = true;

  installPhase = ''
    runHook preInstall

    ( # tools
      cd tools/tools${
        if stdenv.hostPlatform.isAarch64 then
          "-arm64"
        else if stdenv.hostPlatform.isx86_64 then
          "64"
        else
          "32"
      }
      mkdir -p $out/lib

      # prltoolsd contains hardcoded /bin/bash path
      # we're lucky because it uses only -c command
      # => replace to /bin/sh
      bbe -e "s:/bin/bash:/bin/sh\x00\x00:" -o bin/prltoolsd.tmp bin/prltoolsd
      rm -f bin/prltoolsd
      mv bin/prltoolsd.tmp bin/prltoolsd

      # replace hardcoded /usr/bin/prl_fsd
      substituteInPlace ../mount.fuse.prl_fsd \
        --replace-fail "/usr/bin/prl_fsd" "$out/bin/prl_fsd"

      # install binaries
      for i in bin/* sbin/prl_nettool sbin/prl_snapshot; do
        # also patch binaries to replace /usr/bin/XXX to XXX
        # here a two possible cases:
        # 1. it is uses as null terminated string and should be truncated by null;
        # 2. it is uses inside shell script and should be truncated by space.
        for p in bin/* sbin/prl_nettool sbin/prl_snapshot sbin/prlfsmountd; do
          p=$(basename $p)
          bbe -e "s:/usr/bin/$p\x00:./$p\x00\x00\x00\x00\x00\x00\x00\x00:" -o $i.tmp $i
          bbe -e "s:/usr/sbin/$p\x00:./$p\x00\x00\x00\x00\x00\x00\x00\x00 :" -o $i $i.tmp
          bbe -e "s:/usr/bin/$p:$p         :" -o $i.tmp $i
          bbe -e "s:/usr/sbin/$p:$p          :" -o $i $i.tmp
        done

        install -Dm755 $i $out/$i
      done

      install -Dm755 ../../tools/mount.fuse.prl_fsd $out/sbin/mount.fuse.prl_fsd
      install -Dm755 ../../tools/prlfsmountd.sh $out/sbin/prlfsmountd
      install -Dm755 ../../tools/prlbinfmtconfig.sh $out/sbin/prlbinfmtconfig
      for f in $out/bin/* $out/sbin/*; do
        wrapProgram $f \
          --prefix LD_LIBRARY_PATH ':' "${libPath}" \
          --prefix PATH ':' "${scriptPath}"
      done

      for i in lib/libPrl*.0.0; do
        cp $i $out/lib
        ln -s $out/$i $out/''${i%.0.0}
      done

      substituteInPlace ../99prltoolsd-hibernate \
        --replace-fail "/bin/bash" "${bash}/bin/bash"

      mkdir -p $out/etc/pm/sleep.d
      install -Dm644 ../99prltoolsd-hibernate $out/etc/pm/sleep.d
    )

    runHook postInstall
  '';

  passthru.updateScript = ./update.sh;

  meta = {
    description = "Parallels Tools for Linux guests";
    homepage = "https://parallels.com";
    license = lib.licenses.unfree;
    maintainers = with lib.maintainers; [
      wegank
      codgician
    ];
    platforms = lib.platforms.linux;
  };
})