Skip to content

Commit

Permalink
Use XDG_DATA_DIRS instead of hardcoding /usr/share
Browse files Browse the repository at this point in the history
When running nvidia-ctk on a system that uses a custom XDG_DATA_DIRS
environment variable value, the configuration files for `glvnd`,
`vulkan`, and `egl` fail to get passed through from the host to the
container. Reading from XDG_DATA_DIRS instead of hardcoding the default
value allows for finding said files so they can be mounted in the
container.

Signed-off-by: Jared Baur <jaredbaur@fastmail.com>
  • Loading branch information
jmbaur committed Mar 26, 2024
1 parent 1ddc859 commit 2877f6c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
17 changes: 15 additions & 2 deletions internal/discover/graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ func NewGraphicsMountsDiscoverer(logger logger.Interface, driver *root.Driver, n
},
)

searchPaths := []string{"/etc"}
searchPaths = append(searchPaths, xdgDataDirs()...)

jsonMounts := NewMounts(
logger,
lookup.NewFileLocator(
lookup.WithLogger(logger),
lookup.WithRoot(driver.Root),
lookup.WithSearchPaths("/etc", "/usr/share"),
lookup.WithSearchPaths(searchPaths...),
),
driver.Root,
[]string{
Expand Down Expand Up @@ -295,7 +298,7 @@ func newXorgDiscoverer(logger logger.Interface, driver *root.Driver, nvidiaCTKPa
lookup.NewFileLocator(
lookup.WithLogger(logger),
lookup.WithRoot(driver.Root),
lookup.WithSearchPaths("/usr/share"),
lookup.WithSearchPaths(xdgDataDirs()...),
),
driver.Root,
[]string{"X11/xorg.conf.d/10-nvidia.conf"},
Expand Down Expand Up @@ -371,3 +374,13 @@ func (s selectDeviceByPath) MountIsSelected(Mount) bool {
func (s selectDeviceByPath) HookIsSelected(Hook) bool {
return true
}

// xdgDataDirs finds the paths as specified in the environment variable XDG_DATA_DIRS.
// See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html.
func xdgDataDirs() []string {
if dirs, exists := os.LookupEnv("XDG_DATA_DIRS"); exists && dirs != "" {
return root.NormalizeSearchPaths(dirs)
}

return []string{"/usr/local/share", "/usr/share"}
}
6 changes: 3 additions & 3 deletions internal/lookup/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func New(logger logger.Interface, path string, librarySearchPaths []string) *Dri
return &Driver{
logger: logger,
Root: path,
librarySearchPaths: normalizeSearchPaths(librarySearchPaths...),
librarySearchPaths: NormalizeSearchPaths(librarySearchPaths...),
}
}

Expand All @@ -51,12 +51,12 @@ func (r *Driver) Libraries() lookup.Locator {
)
}

// normalizeSearchPaths takes a list of paths and normalized these.
// NormalizeSearchPaths takes a list of paths and normalized these.
// Each of the elements in the list is expanded if it is a path list and the
// resultant list is returned.
// This allows, for example, for the contents of `PATH` or `LD_LIBRARY_PATH` to
// be passed as a search path directly.
func normalizeSearchPaths(paths ...string) []string {
func NormalizeSearchPaths(paths ...string) []string {
var normalized []string
for _, path := range paths {
normalized = append(normalized, filepath.SplitList(path)...)
Expand Down

0 comments on commit 2877f6c

Please sign in to comment.