summaryrefslogtreecommitdiff
path: root/pkgs/development/tools/analysis/rizin/default.nix
blob: 40b564e4dd68d591d18e6c1aa4bda0a01d33bdc2 (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
{
  lib,
  pkgs, # for passthru.plugins
  stdenv,
  fetchurl,
  pkg-config,
  libusb-compat-0_1,
  readline,
  libewf,
  perl,
  pcre2,
  zlib,
  openssl,
  file,
  libmspack,
  libzip,
  lz4,
  xxhash,
  xz,
  meson,
  python3,
  cmake,
  ninja,
  capstone,
  tree-sitter,
  zstd,
  binutils,
}:

let
  rizin = stdenv.mkDerivation rec {
    pname = "rizin";
    version = "0.8.2";

    src = fetchurl {
      url = "https://github.com/rizinorg/rizin/releases/download/v${version}/rizin-src-v${version}.tar.xz";
      hash = "sha256-FjDKUrroby/zfrIgaZ/IL5UbWxgIDt+j9Q3TalJsLZU=";
    };

    mesonFlags = [
      "-Duse_sys_capstone=enabled"
      "-Duse_sys_magic=enabled"
      "-Duse_sys_libzip=enabled"
      "-Duse_sys_zlib=enabled"
      "-Duse_sys_lz4=enabled"
      "-Duse_sys_libzstd=enabled"
      "-Duse_sys_lzma=enabled"
      "-Duse_sys_xxhash=enabled"
      "-Duse_sys_openssl=enabled"
      "-Duse_sys_libmspack=enabled"
      "-Duse_sys_tree_sitter=enabled"
      "-Duse_sys_pcre2=enabled"
      # this is needed for wrapping (adding plugins) to work
      "-Dportable=true"
    ];

    patches = [
      # Normally, Rizin only looks for files in the install prefix. With
      # portable=true, it instead looks for files in relation to the parent
      # of the directory of the binary file specified in /proc/self/exe,
      # caching it. This patch replaces the entire logic to only look at
      # the env var NIX_RZ_PREFIX
      ./librz-wrapper-support.patch
    ];

    nativeBuildInputs = [
      pkg-config
      meson
      (python3.withPackages (
        pp: with pp; [
          pyyaml
        ]
      ))
      ninja
      cmake
    ];

    # meson's find_library seems to not use our compiler wrapper if static parameter
    # is either true/false... We work around by also providing LIBRARY_PATH
    preConfigure = ''
      LIBRARY_PATH=""
      for b in ${toString (map lib.getLib buildInputs)}; do
        if [[ -d "$b/lib" ]]; then
          LIBRARY_PATH="$b/lib''${LIBRARY_PATH:+:}$LIBRARY_PATH"
        fi
      done
      export LIBRARY_PATH
    ''
    + lib.optionalString stdenv.hostPlatform.isDarwin ''
      substituteInPlace binrz/rizin/macos_sign.sh \
        --replace 'codesign' '# codesign'
    '';

    buildInputs = [
      file
      libzip
      capstone
      readline
      libusb-compat-0_1
      libewf
      pcre2
      perl
      zlib
      lz4
      openssl
      libmspack
      tree-sitter
      xxhash
      xz
      zstd
      binutils
    ];

    postPatch = ''
      # find_installation without arguments uses Meson’s Python interpreter,
      # which does not have any extra modules.
      # https://github.com/mesonbuild/meson/pull/9904
      substituteInPlace meson.build \
        --replace "import('python').find_installation()" "find_program('python3')"

      substituteInPlace \
        librz/arch/p/asm/asm_x86_as.c \
        librz/arch/p/asm/asm_ppc_as.c \
        --replace '"as"' '"${binutils}/bin/as"'
    '';

    passthru = rec {
      plugins = {
        jsdec = pkgs.callPackage ./jsdec.nix {
          inherit rizin openssl;
        };
        rz-ghidra = pkgs.qt6.callPackage ./rz-ghidra.nix {
          inherit rizin openssl;
          enableCutterPlugin = false;
        };
        # sigdb isn't a real plugin, but it's separated from the main rizin
        # derivation so that only those who need it will download it
        sigdb = pkgs.callPackage ./sigdb.nix { };
      };
      withPlugins =
        filter:
        pkgs.callPackage ./wrapper.nix {
          inherit rizin;
          plugins = filter plugins;
        };
    };

    meta = {
      description = "UNIX-like reverse engineering framework and command-line toolset";
      homepage = "https://rizin.re/";
      license = lib.licenses.gpl3Plus;
      mainProgram = "rizin";
      maintainers = with lib.maintainers; [
        raskin
        makefu
        mic92
      ];
      platforms = with lib.platforms; unix;
    };
  };
in
rizin