1{ stdenv
2, lib
3, addOpenGLRunpath
4, fetchFromGitHub
5, pkg-config
6, libelf
7, libcap
8, libseccomp
9, rpcsvc-proto
10, libtirpc
11, makeWrapper
12, substituteAll
13, removeReferencesTo
14, go
15}:
16let
17 modprobeVersion = "495.44";
18 nvidia-modprobe = fetchFromGitHub {
19 owner = "NVIDIA";
20 repo = "nvidia-modprobe";
21 rev = modprobeVersion;
22 sha256 = "sha256-Y3ZOfge/EcmhqI19yWO7UfPqkvY1CHHvFC5l9vYyGuU=";
23 };
24 modprobePatch = substituteAll {
25 src = ./modprobe.patch;
26 inherit modprobeVersion;
27 };
28in
29stdenv.mkDerivation rec {
30 pname = "libnvidia-container";
31 version = "1.9.0";
32
33 src = fetchFromGitHub {
34 owner = "NVIDIA";
35 repo = pname;
36 rev = "v${version}";
37 sha256 = "sha256-7OTawWwjeKU8wIa8I/+aSvAJli4kEua94nJSNyCajpE=";
38 };
39
40 patches = [
41 # locations of nvidia-driver libraries are not resolved via ldconfig which
42 # doesn't get used on NixOS. Additional support binaries like nvidia-smi
43 # are not resolved via the environment PATH but via the derivation output
44 # path.
45 ./libnvc-ldconfig-and-path-fixes.patch
46
47 # fix bogus struct declaration
48 ./inline-c-struct.patch
49 ];
50
51 postPatch = ''
52 sed -i \
53 -e 's/^REVISION ?=.*/REVISION = ${src.rev}/' \
54 -e 's/^COMPILER :=.*/COMPILER = $(CC)/' \
55 mk/common.mk
56
57 mkdir -p deps/src/nvidia-modprobe-${modprobeVersion}
58 cp -r ${nvidia-modprobe}/* deps/src/nvidia-modprobe-${modprobeVersion}
59 chmod -R u+w deps/src
60 pushd deps/src
61
62 patch -p0 < ${modprobePatch}
63 touch nvidia-modprobe-${modprobeVersion}/.download_stamp
64 popd
65
66 # 1. replace DESTDIR=$(DEPS_DIR) with empty strings to prevent copying
67 # things into deps/src/nix/store
68 # 2. similarly, remove any paths prefixed with DEPS_DIR
69 # 3. prevent building static libraries because we don't build static
70 # libtirpc (for now)
71 # 4. prevent installation of static libraries because of step 3
72 # 5. prevent installation of libnvidia-container-go.so twice
73 sed -i Makefile \
74 -e 's#DESTDIR=\$(DEPS_DIR)#DESTDIR=""#g' \
75 -e 's#\$(DEPS_DIR)\$#\$#g' \
76 -e 's#all: shared static tools#all: shared tools#g' \
77 -e '/$(INSTALL) -m 644 $(LIB_STATIC) $(DESTDIR)$(libdir)/d' \
78 -e '/$(INSTALL) -m 755 $(libdir)\/$(LIBGO_SHARED) $(DESTDIR)$(libdir)/d'
79 '';
80
81 enableParallelBuilding = true;
82
83 preBuild = ''
84 HOME="$(mktemp -d)"
85 '';
86
87 env.NIX_CFLAGS_COMPILE = toString [ "-I${libtirpc.dev}/include/tirpc" ];
88 NIX_LDFLAGS = [ "-L${libtirpc.dev}/lib" "-ltirpc" ];
89
90 nativeBuildInputs = [ pkg-config go rpcsvc-proto makeWrapper removeReferencesTo ];
91
92 buildInputs = [ libelf libcap libseccomp libtirpc ];
93
94 makeFlags = [
95 "WITH_LIBELF=yes"
96 "prefix=$(out)"
97 # we can't use the WITH_TIRPC=yes flag that exists in the Makefile for the
98 # same reason we patch out the static library use of libtirpc so we set the
99 # define in CFLAGS
100 "CFLAGS=-DWITH_TIRPC"
101 ];
102
103 postInstall =
104 let
105 inherit (addOpenGLRunpath) driverLink;
106 libraryPath = lib.makeLibraryPath [ "$out" driverLink "${driverLink}-32" ];
107 in
108 ''
109 remove-references-to -t "${go}" $out/lib/libnvidia-container-go.so.1.9.0
110 wrapProgram $out/bin/nvidia-container-cli --prefix LD_LIBRARY_PATH : ${libraryPath}
111 '';
112 disallowedReferences = [ go ];
113
114 meta = with lib; {
115 homepage = "https://github.com/NVIDIA/libnvidia-container";
116 description = "NVIDIA container runtime library";
117 license = licenses.asl20;
118 platforms = platforms.linux;
119 maintainers = with maintainers; [ cpcloud ];
120 };
121}