summaryrefslogtreecommitdiff
path: root/pkgs/tools/system/netdata/default.nix
blob: 1140ae4d7086008068f022930728f7ed8bbdc5c9 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
{
  bash,
  bison,
  buildGoModule,
  cargo,
  cmake,
  corrosion,
  cups,
  curl,
  dlib,
  fetchFromGitHub,
  fetchzip,
  flex,
  freeipmi,
  go,
  google-cloud-cpp,
  grpc,
  jemalloc,
  json_c,
  lib,
  libbacktrace,
  libbpf,
  libcap,
  libelf,
  libmnl,
  libnetfilter_acct,
  libossp_uuid,
  libuuid,
  libuv,
  libyaml,
  lm_sensors,
  lz4,
  makeWrapper,
  ninja,
  nixosTests,
  openssl,
  pkg-config,
  protobuf,
  replaceVars,
  rustc,
  rustPlatform,
  snappy,
  stdenv,
  symlinkJoin,
  systemd,
  zlib,

  withCloudUi ? false,
  withConnPrometheus ? false,
  withConnPubSub ? false,
  withCups ? false,
  withDBengine ? true,
  withDebug ? false,
  withEbpf ? false,
  withIpmi ? stdenv.hostPlatform.isLinux,
  withLibbacktrace ? true,
  withML ? true,
  withNdsudo ? false,
  withNetfilter ? stdenv.hostPlatform.isLinux,
  withNetworkViewer ? stdenv.hostPlatform.isLinux,
  withOtel ? true,
  withSsl ? true,
  withSystemdJournal ? stdenv.hostPlatform.isLinux,
  withSystemdUnits ? stdenv.hostPlatform.isLinux,
}:
stdenv.mkDerivation (
  finalAttrs:
  {
    pname = "netdata";
    version = "2.10.3";

    src = fetchFromGitHub {
      owner = "netdata";
      repo = "netdata";
      rev = "v${finalAttrs.version}";
      hash = "sha256-ryX+C3zuY7vONPeB4ocXDPttU5aSYbj1ThTosCSxmys=";
      fetchSubmodules = true;
    };

    strictDeps = true;

    nativeBuildInputs = [
      bison
      cmake
      flex
      go
      makeWrapper
      ninja
      pkg-config
    ]
    ++ lib.optionals withCups [ cups.dev ]
    ++ lib.optionals (withOtel || withSystemdJournal) [
      cargo
      corrosion
      rustc
      rustPlatform.cargoSetupHook
    ];

    # bash is only used to rewrite shebangs
    buildInputs = [
      bash
      curl
      jemalloc
      json_c
      libuv
      libyaml
      lz4
      protobuf
      zlib
    ]
    ++ lib.optionals stdenv.hostPlatform.isDarwin [
      libossp_uuid
    ]
    ++ lib.optionals stdenv.hostPlatform.isLinux [
      libcap
      libuuid
      lm_sensors
    ]
    ++ lib.optionals withConnPrometheus [ snappy ]
    ++ lib.optionals withConnPubSub [
      google-cloud-cpp
      grpc
    ]
    ++ lib.optionals withCups [ cups ]
    ++ lib.optionals withEbpf [
      libbpf
      libelf
    ]
    ++ lib.optionals withIpmi [ freeipmi ]
    ++ lib.optionals withLibbacktrace [ libbacktrace ]
    ++ lib.optionals withNetfilter [
      libmnl
      libnetfilter_acct
    ]
    ++ lib.optionals withSsl [ openssl ]
    ++ lib.optionals (withSystemdJournal || withSystemdUnits) [ systemd ];

    patches = [
      # Allow ndsudo to use non-hardcoded `PATH`
      # See https://github.com/netdata/netdata/pull/17377#issuecomment-2183017868
      #     https://github.com/netdata/netdata/security/advisories/GHSA-pmhq-4cxq-wj93
      ./ndsudo-fix-path.patch

      ./disable-binary-permission-check.patch

      ./use-local-corrosion.patch
      ./use-local-libbacktrace.patch
    ]
    ++ lib.optional withCloudUi (
      replaceVars ./dashboard-v3-add.patch {
        # FIXME web.archive.org link can be replace once https://github.com/netdata/netdata-cloud/issues/1081 resolved
        # last update 12/10/2025 21:25:17
        dashboardPath = fetchzip {
          # The `if_` suffix is intentional, with out it the hash will vary depending on the region
          url = "https://web.archive.org/web/20251210212517if_/https://app.netdata.cloud/agent.tar.gz";
          hash = "sha256-8ovYkvt324l6f0YT6wTG+Y2u7VaVqotAdssnNTtHIEk=";
        };
      }
    );

    # Guard against unused build-time development inputs in closure. Without
    # the ./skip-CONFIGURE_COMMAND.patch patch the closure retains inputs up
    # to bootstrap tools:
    #   https://github.com/NixOS/nixpkgs/pull/175719
    # We pick zlib.dev as a simple canary package with pkg-config input.
    disallowedReferences = lib.optional (!withDebug) zlib.dev;

    donStrip = withDebug || withLibbacktrace;
    env.NIX_CFLAGS_COMPILE = lib.optionalString withDebug "-O1 -ggdb -DNETDATA_INTERNAL_CHECKS=1";

    postInstall = ''
      # Relocate one folder above.
      mv $out/usr/* $out/
    ''
    + lib.optionalString stdenv.hostPlatform.isLinux ''
      # rename this plugin so netdata will look for setuid wrapper
      mv $out/libexec/netdata/plugins.d/apps.plugin \
        $out/libexec/netdata/plugins.d/apps.plugin.org
      mv $out/libexec/netdata/plugins.d/cgroup-network \
        $out/libexec/netdata/plugins.d/cgroup-network.org
      mv $out/libexec/netdata/plugins.d/perf.plugin \
        $out/libexec/netdata/plugins.d/perf.plugin.org
      mv $out/libexec/netdata/plugins.d/slabinfo.plugin \
        $out/libexec/netdata/plugins.d/slabinfo.plugin.org
      mv $out/libexec/netdata/plugins.d/debugfs.plugin \
        $out/libexec/netdata/plugins.d/debugfs.plugin.org
      ${lib.optionalString withIpmi ''
        mv $out/libexec/netdata/plugins.d/freeipmi.plugin \
          $out/libexec/netdata/plugins.d/freeipmi.plugin.org
      ''}
      ${lib.optionalString withNdsudo ''
        mv $out/libexec/netdata/plugins.d/ndsudo \
          $out/libexec/netdata/plugins.d/ndsudo.org

        ln -s /var/lib/netdata/ndsudo/ndsudo $out/libexec/netdata/plugins.d/ndsudo
      ''}
      ${lib.optionalString withNetworkViewer ''
        mv $out/libexec/netdata/plugins.d/network-viewer.plugin \
          $out/libexec/netdata/plugins.d/network-viewer.plugin.org
      ''}
      ${lib.optionalString withOtel ''
        mv $out/libexec/netdata/plugins.d/otel-plugin \
          $out/libexec/netdata/plugins.d/otel-plugin.org
      ''}
      ${lib.optionalString withSystemdJournal ''
        mv $out/libexec/netdata/plugins.d/systemd-journal.plugin \
          $out/libexec/netdata/plugins.d/systemd-journal.plugin.org
      ''}
      ${lib.optionalString withSystemdUnits ''
        mv $out/libexec/netdata/plugins.d/systemd-units.plugin \
          $out/libexec/netdata/plugins.d/systemd-units.plugin.org
      ''}
    '';

    preConfigure = ''
      ${lib.optionalString (withOtel || withSystemdJournal) ''
        export CMAKE_PREFIX_PATH="${corrosion}:$CMAKE_PREFIX_PATH"
      ''}

      export GOCACHE=$TMPDIR/go-cache
      export GOPATH=$TMPDIR/go
      export GOSUMDB=off

      substituteInPlace packaging/cmake/Modules/NetdataGoTools.cmake \
        --replace-fail \
          'GOPROXY=https://proxy.golang.org' \
          'GOPROXY=file://${finalAttrs.passthru.netdata-go-modules},file://${finalAttrs.passthru.nd-mcp}'

      # Prevent the path to be caught into the Nix store path.
      substituteInPlace CMakeLists.txt \
        --replace-fail 'set(CACHE_DIR "''${NETDATA_RUNTIME_PREFIX}/var/cache/netdata")' 'set(CACHE_DIR "/var/cache/netdata")' \
        --replace-fail 'set(CONFIG_DIR "''${NETDATA_RUNTIME_PREFIX}/etc/netdata")' 'set(CONFIG_DIR "/etc/netdata")' \
        --replace-fail 'set(LIBCONFIG_DIR "''${NETDATA_RUNTIME_PREFIX}/usr/lib/netdata/conf.d")' 'set(LIBCONFIG_DIR "${placeholder "out"}/share/netdata/conf.d")' \
        --replace-fail 'set(LOG_DIR "''${NETDATA_RUNTIME_PREFIX}/var/log/netdata")' 'set(LOG_DIR "/var/log/netdata")' \
        --replace-fail 'set(PLUGINS_DIR "''${NETDATA_RUNTIME_PREFIX}/usr/libexec/netdata/plugins.d")' 'set(PLUGINS_DIR "${placeholder "out"}/libexec/netdata/plugins.d")' \
        --replace-fail 'set(VARLIB_DIR "''${NETDATA_RUNTIME_PREFIX}/var/lib/netdata")' 'set(VARLIB_DIR "/var/lib/netdata")' \
        --replace-fail 'set(pkglibexecdir_POST "''${NETDATA_RUNTIME_PREFIX}/usr/libexec/netdata")' 'set(pkglibexecdir_POST "${placeholder "out"}/libexec/netdata")' \
        --replace-fail 'set(localstatedir_POST "''${NETDATA_RUNTIME_PREFIX}/var")' 'set(localstatedir_POST "/var")' \
        --replace-fail 'set(BINDIR usr/sbin)' 'set(BINDIR "bin")' \
        --replace-fail 'set(BUILD_INFO_CMAKE_CACHE_ARCHIVE_PATH "usr/share/netdata")' 'set(BUILD_INFO_CMAKE_CACHE_ARCHIVE_PATH "${placeholder "out"}/share/netdata")'
    '';

    cmakeFlags = [
      "-DWEB_DIR=share/netdata/web"
      (lib.cmakeBool "ENABLE_DASHBOARD" withCloudUi)
      (lib.cmakeBool "ENABLE_DBENGINE" withDBengine)
      (lib.cmakeBool "ENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE" withConnPrometheus)
      (lib.cmakeBool "ENABLE_JEMALLOC" true)
      (lib.cmakeBool "ENABLE_LIBBACKTRACE" withLibbacktrace)
      (lib.cmakeBool "ENABLE_ML" withML)
      (lib.cmakeBool "ENABLE_NETDATA_JOURNAL_FILE_READER" withSystemdJournal)
      (lib.cmakeBool "ENABLE_PLUGIN_CUPS" withCups)
      (lib.cmakeBool "ENABLE_PLUGIN_EBPF" withEbpf)
      (lib.cmakeBool "ENABLE_PLUGIN_FREEIPMI" withIpmi)
      (lib.cmakeBool "ENABLE_PLUGIN_NETWORK_VIEWER" withNetworkViewer)
      (lib.cmakeBool "ENABLE_PLUGIN_OTEL_SIGNAL_VIEWER" withOtel)
      (lib.cmakeBool "ENABLE_PLUGIN_OTEL" withOtel)
      (lib.cmakeBool "ENABLE_PLUGIN_SYSTEMD_JOURNAL" withSystemdJournal)
      (lib.cmakeBool "ENABLE_PLUGIN_SYSTEMD_UNITS" withSystemdUnits)
      (lib.cmakeBool "ENABLE_PLUGIN_XENSTAT" false)
      # Suggested by upstream.
      "-G Ninja"
    ]
    ++ lib.optional withML "-DNETDATA_DLIB_SOURCE_PATH=${dlib.src}";

    postFixup = ''
      wrapProgram $out/bin/netdata-claim.sh --prefix PATH : ${lib.makeBinPath [ openssl ]}
      wrapProgram $out/libexec/netdata/plugins.d/cgroup-network-helper.sh --prefix PATH : ${lib.makeBinPath [ bash ]}
      wrapProgram $out/bin/netdatacli --set NETDATA_PIPENAME /run/netdata/ipc
      ${lib.optionalString stdenv.hostPlatform.isLinux ''
        substituteInPlace $out/lib/netdata/conf.d/go.d/sensors.conf --replace-fail '/usr/bin/sensors' '${lm_sensors}/bin/sensors'
      ''}

      # Time to cleanup the output directory.
      cp $out/etc/netdata/edit-config $out/bin/netdata-edit-config
      mv $out/lib/netdata/conf.d $out/share/netdata/conf.d
      rm -rf $out/{var,usr,etc}
    '';

    enableParallelBuilding = true;

    passthru = {
      nd-mcp =
        (buildGoModule {
          pname = "${finalAttrs.pname}-nd-mcp";
          inherit (finalAttrs) version src;

          sourceRoot = "${finalAttrs.src.name}/src/web/mcp/bridges/stdio-golang";

          vendorHash = "sha256-jyCTp52Dc2IuRwzGT+sHFljO30oqAMfe3xVdEpV+R2c=";
          proxyVendor = true;
          doCheck = false;

          subPackages = [ "." ];

          ldflags = [
            "-s"
            "-w"
          ];

          meta = finalAttrs.meta // {
            description = "Netdata Model Context Protocol (MCP) Integration";
            license = lib.licenses.gpl3Only;
          };
        }).goModules;

      netdata-go-modules =
        (buildGoModule {
          pname = "${finalAttrs.pname}-go-plugins";
          inherit (finalAttrs) version src;

          sourceRoot = "${finalAttrs.src.name}/src/go/plugin/go.d";

          vendorHash = "sha256-HRe1bcVIQVzwPZnGlAK5A8AO1VTcjFajkPwBVdl4UIA=";
          proxyVendor = true;
          doCheck = false;

          ldflags = [
            "-s"
            "-w"
            "-X main.version=${finalAttrs.version}"
          ];

          meta = finalAttrs.meta // {
            description = "Netdata orchestrator for data collection modules written in Go";
            mainProgram = "godplugin";
            license = lib.licenses.gpl3Only;
          };
        }).goModules;

      inherit
        withIpmi
        withNdsudo
        withNetworkViewer
        withOtel
        withSystemdJournal
        ;

      tests.netdata = nixosTests.netdata;
    };

    meta = {
      broken = stdenv.buildPlatform != stdenv.hostPlatform || withEbpf;
      description = "Real-time performance monitoring tool";
      homepage = "https://www.netdata.cloud/";
      changelog = "https://github.com/netdata/netdata/releases/tag/v${finalAttrs.version}";
      license = [ lib.licenses.gpl3Plus ] ++ lib.optionals withCloudUi [ lib.licenses.ncul1 ];
      mainProgram = "netdata";
      platforms = lib.platforms.unix;
      maintainers = with lib.maintainers; [
        mkg20001
        rhoriguchi
      ];
    };
  }
  // lib.optionalAttrs (withOtel || withSystemdJournal) {
    cargoDeps = symlinkJoin {
      name = "cargo-vendor-dir";
      paths = [
        (rustPlatform.fetchCargoVendor {
          inherit (finalAttrs)
            pname
            version
            src
            cargoRoot
            ;
          hash = "sha256-mxFpT95e+NMqjJOIRqM+yKHGQHfpWmIFHqFNiiiqXOY=";
        })
        (rustPlatform.fetchCargoVendor {
          pname = "${finalAttrs.pname}-nd-jf";
          inherit (finalAttrs) version src;
          cargoRoot = "${finalAttrs.cargoRoot}/jf";
          hash = "sha256-6spr8WRt2G6tzaUQACxIcVMoDNKOFTg6rSPEOihMgLE=";
        })
      ];
    };
    cargoRoot = "src/crates";
  }
)