1{ lib, stdenv, fetchFromGitHub, buildPackages, perl, which, ncurses, nukeReferences }:
2
3let
4 dialect = with lib; last (splitString "-" stdenv.hostPlatform.system);
5in
6
7stdenv.mkDerivation rec {
8 pname = "lsof";
9 version = "4.99.3";
10
11 src = fetchFromGitHub {
12 owner = "lsof-org";
13 repo = "lsof";
14 rev = version;
15 hash = "sha256-XW3l+E9D8hgI9jGJGKkIAKa8O9m0JHgZhEASqg4gYuw=";
16 };
17
18 postPatch = ''
19 patchShebangs --build lib/dialects/*/Mksrc
20 # Do not re-build version.h in every 'make' to allow nuke-refs below.
21 # We remove phony 'FRC' target that forces rebuilds:
22 # 'version.h: FRC ...' is translated to 'version.h: ...'.
23 sed -i lib/dialects/*/Makefile -e 's/version.h:\s*FRC/version.h:/'
24 '' + lib.optionalString stdenv.isDarwin ''
25 sed -i 's|lcurses|lncurses|g' Configure
26 '';
27
28 depsBuildBuild = [ buildPackages.stdenv.cc ];
29 nativeBuildInputs = [ nukeReferences perl which ];
30 buildInputs = [ ncurses ];
31
32 # Stop build scripts from searching global include paths
33 LSOF_INCLUDE = "${lib.getDev stdenv.cc.libc}/include";
34 configurePhase = "LINUX_CONF_CC=$CC_FOR_BUILD LSOF_CC=$CC LSOF_AR=\"$AR cr\" LSOF_RANLIB=$RANLIB ./Configure -n ${dialect}";
35
36 preBuild = ''
37 for filepath in $(find dialects/${dialect} -type f); do
38 sed -i "s,/usr/include,$LSOF_INCLUDE,g" $filepath
39 done
40
41 # Wipe out development-only flags from CFLAGS embedding
42 make version.h
43 nuke-refs version.h
44 '';
45
46 installPhase = ''
47 # Fix references from man page https://github.com/lsof-org/lsof/issues/66
48 substituteInPlace Lsof.8 \
49 --replace ".so ./00DIALECTS" "" \
50 --replace ".so ./version" ".ds VN ${version}"
51 mkdir -p $out/bin $out/man/man8
52 cp Lsof.8 $out/man/man8/lsof.8
53 cp lsof $out/bin
54 '';
55
56 meta = with lib; {
57 homepage = "https://github.com/lsof-org/lsof";
58 description = "Tool to list open files";
59 mainProgram = "lsof";
60 longDescription = ''
61 List open files. Can show what process has opened some file,
62 socket (IPv6/IPv4/UNIX local), or partition (by opening a file
63 from it).
64 '';
65 license = licenses.purdueBsd;
66 maintainers = with maintainers; [ dezgeg ];
67 platforms = platforms.unix;
68 };
69}