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