1{ lib
2, glibc
3, fetchFromGitLab
4, makeWrapper
5, buildGoModule
6, linkFarm
7, writeShellScript
8, containerRuntimePath
9, configTemplate
10, libnvidia-container
11}:
12let
13 isolatedContainerRuntimePath = linkFarm "isolated_container_runtime_path" [
14 {
15 name = "runc";
16 path = containerRuntimePath;
17 }
18 ];
19 warnIfXdgConfigHomeIsSet = writeShellScript "warn_if_xdg_config_home_is_set" ''
20 set -eo pipefail
21
22 if [ -n "$XDG_CONFIG_HOME" ]; then
23 echo >&2 "$(tput setaf 3)warning: \$XDG_CONFIG_HOME=$XDG_CONFIG_HOME$(tput sgr 0)"
24 fi
25 '';
26in
27buildGoModule rec {
28 pname = "container-toolkit/container-toolkit";
29 version = "1.9.0";
30
31 src = fetchFromGitLab {
32 owner = "nvidia";
33 repo = pname;
34 rev = "v${version}";
35 hash = "sha256-b4mybNB5FqizFTraByHk5SCsNO66JaISj18nLgLN7IA=";
36 };
37
38 vendorHash = null;
39
40 postPatch = ''
41 # replace the default hookDefaultFilePath to the $out path
42 substituteInPlace cmd/nvidia-container-runtime/main.go \
43 --replace '/usr/bin/nvidia-container-runtime-hook' '${placeholder "out"}/bin/nvidia-container-runtime-hook'
44 '';
45
46 ldflags = [ "-s" "-w" ];
47
48 nativeBuildInputs = [ makeWrapper ];
49
50 checkFlags =
51 let
52 skippedTests = [
53 # Disable tests executing nvidia-container-runtime command.
54 "TestGoodInput"
55 "TestDuplicateHook"
56 ];
57 in
58 [ "-skip" "${builtins.concatStringsSep "|" skippedTests}" ];
59
60 postInstall = ''
61 mkdir -p $out/etc/nvidia-container-runtime
62
63 # nvidia-container-runtime invokes docker-runc or runc if that isn't
64 # available on PATH.
65 #
66 # Also set XDG_CONFIG_HOME if it isn't already to allow overriding
67 # configuration. This in turn allows users to have the nvidia container
68 # runtime enabled for any number of higher level runtimes like docker and
69 # podman, i.e., there's no need to have mutually exclusivity on what high
70 # level runtime can enable the nvidia runtime because each high level
71 # runtime has its own config.toml file.
72 wrapProgram $out/bin/nvidia-container-runtime \
73 --run "${warnIfXdgConfigHomeIsSet}" \
74 --prefix PATH : ${isolatedContainerRuntimePath}:${libnvidia-container}/bin \
75 --set-default XDG_CONFIG_HOME $out/etc
76
77 cp ${configTemplate} $out/etc/nvidia-container-runtime/config.toml
78
79 substituteInPlace $out/etc/nvidia-container-runtime/config.toml \
80 --subst-var-by glibcbin ${lib.getBin glibc}
81
82 ln -s $out/bin/nvidia-container-{toolkit,runtime-hook}
83
84 wrapProgram $out/bin/nvidia-container-toolkit \
85 --add-flags "-config ${placeholder "out"}/etc/nvidia-container-runtime/config.toml"
86 '';
87
88 meta = with lib; {
89 homepage = "https://gitlab.com/nvidia/container-toolkit/container-toolkit";
90 description = "NVIDIA Container Toolkit";
91 license = licenses.asl20;
92 platforms = platforms.linux;
93 maintainers = with maintainers; [ cpcloud ];
94 };
95}