From 24d9b8a7060b3fb7d64fa27ee9a745617185a189 Mon Sep 17 00:00:00 2001 From: Moritz Sanft <58110325+msanft@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:48:11 +0100 Subject: [PATCH] libnvidia-container: clean up patches This recreates the patches with `git format-patch` and adds descriptions to each of them. It also separates the binary and library lookup patches from each other. --- ...h => 0001-ldcache-don-t-use-ldcache.patch} | 49 +++++++------------ ...dia-docker-compatible-binary-lookups.patch | 45 +++++++++++++++++ .../0003-nvc-fix-struct-declaration.patch | 23 +++++++++ .../libnvidia-container/inline-c-struct.patch | 14 ------ .../li/libnvidia-container/package.nix | 14 +++--- 5 files changed, 95 insertions(+), 50 deletions(-) rename pkgs/by-name/li/libnvidia-container/{fix-library-resolving.patch => 0001-ldcache-don-t-use-ldcache.patch} (85%) create mode 100644 pkgs/by-name/li/libnvidia-container/0002-nvc-nvidia-docker-compatible-binary-lookups.patch create mode 100644 pkgs/by-name/li/libnvidia-container/0003-nvc-fix-struct-declaration.patch delete mode 100644 pkgs/by-name/li/libnvidia-container/inline-c-struct.patch diff --git a/pkgs/by-name/li/libnvidia-container/fix-library-resolving.patch b/pkgs/by-name/li/libnvidia-container/0001-ldcache-don-t-use-ldcache.patch similarity index 85% rename from pkgs/by-name/li/libnvidia-container/fix-library-resolving.patch rename to pkgs/by-name/li/libnvidia-container/0001-ldcache-don-t-use-ldcache.patch index 9bae2b87e61fa..1a26140b8beeb 100644 --- a/pkgs/by-name/li/libnvidia-container/fix-library-resolving.patch +++ b/pkgs/by-name/li/libnvidia-container/0001-ldcache-don-t-use-ldcache.patch @@ -1,22 +1,22 @@ -From 8799541f99785d2bd881561386676fb0985e939e Mon Sep 17 00:00:00 2001 +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Moritz Sanft <58110325+msanft@users.noreply.github.com> -Date: Thu, 10 Oct 2024 14:32:42 +0200 -Subject: [PATCH] fix library resolving +Date: Fri, 20 Dec 2024 16:34:50 +0100 +Subject: [PATCH] ldcache: don't use ldcache -Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> +This patch hinders libnvidia-container from using the loader cache, which doesn't get used on NixOS. --- src/ldcache.c | 46 +++++++++++++++++----------------------------- src/ldcache.h | 2 +- - src/nvc_info.c | 10 +++------- + src/nvc_info.c | 8 ++------ src/nvc_ldcache.c | 2 +- - 4 files changed, 22 insertions(+), 38 deletions(-) + 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/ldcache.c b/src/ldcache.c -index 38bab055..8cd30a0f 100644 +index 38bab0553208f66b2866ccea6cdb0faca4357f19..1c4acd52b622be4ca6accdc80da5a6fcf9ae67dd 100644 --- a/src/ldcache.c +++ b/src/ldcache.c @@ -108,40 +108,28 @@ ldcache_close(struct ldcache *ctx) - + int ldcache_resolve(struct ldcache *ctx, uint32_t arch, const char *root, const char * const libs[], - char *paths[], size_t size, ldcache_select_fn select, void *select_ctx) @@ -27,10 +27,10 @@ index 38bab055..8cd30a0f 100644 - int override; + char dir[PATH_MAX]; + char lib[PATH_MAX]; - + - h = (struct header_libc6 *)ctx->ptr; memset(paths, 0, size * sizeof(*paths)); - + - for (uint32_t i = 0; i < h->nlibs; ++i) { - int32_t flags = h->libs[i].flags; - char *key = (char *)ctx->ptr + h->libs[i].key; @@ -74,7 +74,7 @@ index 38bab055..8cd30a0f 100644 return (0); } diff --git a/src/ldcache.h b/src/ldcache.h -index 33d78dd7..2b087dbc 100644 +index 33d78dd7e21f65eb696535c115bbd2839a6c67ca..2b087dbca1a6a2946cd495e676a61e956212e3dc 100644 --- a/src/ldcache.h +++ b/src/ldcache.h @@ -50,6 +50,6 @@ void ldcache_init(struct ldcache *, struct error *, const char *); @@ -83,10 +83,10 @@ index 33d78dd7..2b087dbc 100644 int ldcache_resolve(struct ldcache *, uint32_t, const char *, const char * const [], - char *[], size_t, ldcache_select_fn, void *); + char *[], size_t, const char*); - + #endif /* HEADER_LDCACHE_H */ diff --git a/src/nvc_info.c b/src/nvc_info.c -index b7b8adfa..d42f2beb 100644 +index b7b8adfa7c79c326a1acb481a06a05d1463e810f..cf4b1905fd2127c28ee16649501be122d3be5261 100644 --- a/src/nvc_info.c +++ b/src/nvc_info.c @@ -217,15 +217,13 @@ find_library_paths(struct error *err, struct dxcore_context *dxcore, struct nvc_ @@ -95,7 +95,7 @@ index b7b8adfa..d42f2beb 100644 ldcache_init(&ld, err, path); - if (ldcache_open(&ld) < 0) - return (-1); - + info->nlibs = size; info->libs = array_new(err, size); if (info->libs == NULL) @@ -104,7 +104,7 @@ index b7b8adfa..d42f2beb 100644 - info->libs, info->nlibs, select_libraries_fn, info) < 0) + info->libs, info->nlibs, info->nvrm_version) < 0) goto fail; - + info->nlibs32 = size; @@ -233,13 +231,11 @@ find_library_paths(struct error *err, struct dxcore_context *dxcore, struct nvc_ if (info->libs32 == NULL) @@ -114,34 +114,23 @@ index b7b8adfa..d42f2beb 100644 + info->libs32, info->nlibs32, info->nvrm_version) < 0) goto fail; rv = 0; - + fail: - if (ldcache_close(&ld) < 0) - return (-1); return (rv); } - -@@ -253,7 +249,7 @@ find_binary_paths(struct error *err, struct dxcore_context* dxcore, struct nvc_d - char path[PATH_MAX]; - int rv = -1; - -- if ((env = secure_getenv("PATH")) == NULL) { -+ if ((env = "/run/nvidia-docker/bin:/run/nvidia-docker/extras/bin") == NULL) { - error_setx(err, "environment variable PATH not found"); - return (-1); - } + diff --git a/src/nvc_ldcache.c b/src/nvc_ldcache.c -index db3b2f69..ae5def43 100644 +index db3b2f69692270e9058b2e26f18eb31677909d05..ae5def43b4cb3973af3aad55361265173ca938a7 100644 --- a/src/nvc_ldcache.c +++ b/src/nvc_ldcache.c @@ -367,7 +367,7 @@ nvc_ldcache_update(struct nvc_context *ctx, const struct nvc_container *cnt) if (validate_args(ctx, cnt != NULL) < 0) return (-1); - + - argv = (char * []){cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL}; + argv = (char * []){cnt->cfg.ldconfig, "-f", "/tmp/ld.so.conf.nvidia-host", "-C", "/tmp/ld.so.cache.nvidia-host", cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL}; if (*argv[0] == '@') { /* * We treat this path specially to be relative to the host filesystem. --- -2.46.0 diff --git a/pkgs/by-name/li/libnvidia-container/0002-nvc-nvidia-docker-compatible-binary-lookups.patch b/pkgs/by-name/li/libnvidia-container/0002-nvc-nvidia-docker-compatible-binary-lookups.patch new file mode 100644 index 0000000000000..2a52ad2bd2404 --- /dev/null +++ b/pkgs/by-name/li/libnvidia-container/0002-nvc-nvidia-docker-compatible-binary-lookups.patch @@ -0,0 +1,45 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Moritz Sanft <58110325+msanft@users.noreply.github.com> +Date: Fri, 20 Dec 2024 16:37:07 +0100 +Subject: [PATCH] nvc: nvidia-docker-compatible binary lookups + +This patch maintains compatibility with NixOS' `virtualisation.docker.enableNvidia` option (which is to be removed soon), while also enabling supplying a custom PATH, to work with the modern CDI-based approach. +--- + src/nvc_info.c | 16 ++++++++++++++-- + 1 file changed, 14 insertions(+), 2 deletions(-) + +diff --git a/src/nvc_info.c b/src/nvc_info.c +index cf4b1905fd2127c28ee16649501be122d3be5261..2ab552860ef98879b76398a6f9be95f07b2c8a4a 100644 +--- a/src/nvc_info.c ++++ b/src/nvc_info.c +@@ -243,16 +243,28 @@ static int + find_binary_paths(struct error *err, struct dxcore_context* dxcore, struct nvc_driver_info* info, + const char *root, const char * const bins[], size_t size) + { +- char *env, *ptr; ++ char *env, *ptr, *os_path; + const char *dir; + char tmp[PATH_MAX]; + char path[PATH_MAX]; + int rv = -1; + +- if ((env = secure_getenv("PATH")) == NULL) { ++ if ((os_path = secure_getenv("PATH")) == NULL) { + error_setx(err, "environment variable PATH not found"); + return (-1); + } ++ ++ // TODO: Remove this patch once `virtualisation.docker.enableNvidia` is removed from NixOS. ++ // It only exists to maintain compatibility with the old nvidia-docker package. ++ int p_rv = snprintf(env, PATH_MAX, "/run/nvidia-docker/bin:/run/nvidia-docker/extras/bin:%s", os_path); ++ if (p_rv >= PATH_MAX) { ++ error_setx(err, "PATH environment variable too long"); ++ return (-1); ++ } else if (p_rv < 0) { ++ error_setx(err, "error setting PATH environment variable"); ++ return (-1); ++ } ++ + if ((env = ptr = xstrdup(err, env)) == NULL) + return (-1); + diff --git a/pkgs/by-name/li/libnvidia-container/0003-nvc-fix-struct-declaration.patch b/pkgs/by-name/li/libnvidia-container/0003-nvc-fix-struct-declaration.patch new file mode 100644 index 0000000000000..2c91e471d9cc3 --- /dev/null +++ b/pkgs/by-name/li/libnvidia-container/0003-nvc-fix-struct-declaration.patch @@ -0,0 +1,23 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Moritz Sanft <58110325+msanft@users.noreply.github.com> +Date: Fri, 20 Dec 2024 16:38:55 +0100 +Subject: [PATCH] nvc: fix struct declaration + +--- + src/nvcgo.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/src/nvcgo.c b/src/nvcgo.c +index 2e090c9bef83e165dfb722ab27e3287407466173..643504cd485fbe4a89d5959a1adfb69ff6748576 100644 +--- a/src/nvcgo.c ++++ b/src/nvcgo.c +@@ -33,7 +33,8 @@ + void nvcgo_program_1(struct svc_req *, register SVCXPRT *); + + static struct nvcgo_ext { +- struct nvcgo; ++ struct rpc rpc; ++ struct libnvcgo api; + bool initialized; + void *dl_handle; + } global_nvcgo_context; diff --git a/pkgs/by-name/li/libnvidia-container/inline-c-struct.patch b/pkgs/by-name/li/libnvidia-container/inline-c-struct.patch deleted file mode 100644 index dab574e58398c..0000000000000 --- a/pkgs/by-name/li/libnvidia-container/inline-c-struct.patch +++ /dev/null @@ -1,14 +0,0 @@ -diff --git a/src/nvcgo.c b/src/nvcgo.c -index 98789a3..47ad02b 100644 ---- a/src/nvcgo.c -+++ b/src/nvcgo.c -@@ -33,7 +33,8 @@ - void nvcgo_program_1(struct svc_req *, register SVCXPRT *); - - static struct nvcgo_ext { -- struct nvcgo; -+ struct rpc rpc; -+ struct libnvcgo api; - bool initialized; - void *dl_handle; - } global_nvcgo_context; diff --git a/pkgs/by-name/li/libnvidia-container/package.nix b/pkgs/by-name/li/libnvidia-container/package.nix index 137dbf90c5015..85bdbe8be310e 100644 --- a/pkgs/by-name/li/libnvidia-container/package.nix +++ b/pkgs/by-name/li/libnvidia-container/package.nix @@ -45,16 +45,18 @@ stdenv.mkDerivation rec { patches = [ # Locations of nvidia driver libraries are not resolved via ldconfig which # doesn't get used on NixOS. - # TODO: The latter doesn't really apply anymore. - # Additional support binaries like nvidia-smi - # are not resolved via the environment PATH but via the derivation output - # path. - (replaceVars ./fix-library-resolving.patch { + (replaceVars ./0001-ldcache-don-t-use-ldcache.patch { inherit (addDriverRunpath) driverLink; }) + # Use both PATH and the legacy nvidia-docker paths (NixOS artifacts) + # for binary lookups. + # TODO: Remove the legacy compatibility once nvidia-docker is removed + # from NixOS. + ./0002-nvc-nvidia-docker-compatible-binary-lookups.patch + # fix bogus struct declaration - ./inline-c-struct.patch + ./0003-nvc-fix-struct-declaration.patch ]; postPatch = ''