summary refs log tree commit diff
path: root/pkgs/tools/misc/fx_cast/default.nix
blob: 5181610a039115100de900b6df3349787f08b344 (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
{ stdenv, fetchurl, dpkg }:

stdenv.mkDerivation rec {
  pname = "fx_cast_bridge";
  version = "0.0.6";

  src = fetchurl {
     url = "https://github.com/hensm/fx_cast/releases/download/v${version}/${pname}-${version}-x64.deb";
     sha256 = "1mjpwd27b0cpigz4cc2mdl97d78rj5ikn2bqfdic50lqjciaqi1b";
  };

  nativeBuildInputs = [ dpkg ];

  unpackPhase = ''
    runHook preUnpack
    dpkg-deb -xv $src ./
    runHook postUnpack
  '';

  dontBuild = true;
  dontPatchELF = true;

  installPhase = ''
    install -DT {opt/fx_cast,$out/bin}/${pname}
    install -DT {usr,$out}/lib/mozilla/native-messaging-hosts/${pname}.json

    substituteInPlace $out/lib/mozilla/native-messaging-hosts/${pname}.json \
      --replace {/opt/fx_cast,$out/bin}/${pname}
  '';

  # See now-cli/default.nix
  dontStrip = true;
  preFixup = let
    libPath = stdenv.lib.makeLibraryPath [stdenv.cc.cc stdenv.cc.libc];
    bin = "$out/bin/${pname}";
  in ''

    orig_size=$(stat --printf=%s ${bin})

    patchelf --set-interpreter "${stdenv.cc.bintools.dynamicLinker}" ${bin}
    patchelf --set-rpath ${libPath} ${bin}
    chmod +x ${bin}

    new_size=$(stat --printf=%s ${bin})

    ###### zeit-pkg fixing starts here.
    # we're replacing plaintext js code that looks like
    # PAYLOAD_POSITION = '1234                  ' | 0
    # [...]
    # PRELUDE_POSITION = '1234                  ' | 0
    # ^-----20-chars-----^^------22-chars------^
    # ^-- grep points here
    #
    # var_* are as described above
    # shift_by seems to be safe so long as all patchelf adjustments occur
    # before any locations pointed to by hardcoded offsets

    var_skip=20
    var_select=22
    shift_by=$(expr $new_size - $orig_size)

    function fix_offset {
      # $1 = name of variable to adjust
      location=$(grep -obUam1 "$1" ${bin} | cut -d: -f1)
      location=$(expr $location + $var_skip)

      value=$(dd if=${bin} iflag=count_bytes,skip_bytes skip=$location \
                 bs=1 count=$var_select status=none)
      value=$(expr $shift_by + $value)

      echo -n $value | dd of=${bin} bs=1 seek=$location conv=notrunc
    }

    fix_offset PAYLOAD_POSITION
    fix_offset PRELUDE_POSITION

  '';

  meta = with stdenv.lib; {
    description = "Implementation of the Chrome Sender API (Chromecast) within Firefox";
    homepage = https://hensm.github.io/fx_cast/;
    license = licenses.mit;
    maintainers = with maintainers; [ dtzWill ];
  };
}