From 55f54cc2c33e52ae8ac57bced83046b7d9dbc6cd Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Sat, 11 Nov 2023 20:52:31 +0000 Subject: [PATCH] nix-required-mounts: restore (optional) symlink support --- nixos/tests/nix-required-mounts/default.nix | 4 ++- .../test-require-feature.nix | 12 ++++++++- .../nix_required_mounts.py | 25 ++++++++++++------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/nixos/tests/nix-required-mounts/default.nix b/nixos/tests/nix-required-mounts/default.nix index 38f94bf6fd98..49bf3b0268a6 100644 --- a/nixos/tests/nix-required-mounts/default.nix +++ b/nixos/tests/nix-required-mounts/default.nix @@ -26,11 +26,13 @@ in guest = "/run/opengl-driver/lib"; } ]; + unsafeFollowSymlinks = true; }; users.users.person.isNormalUser = true; systemd.tmpfiles.rules = [ "d /supported-feature-files 0755 person users -" - "f /usr/lib/imaginary-fhs-drivers/libcuda.so 0444 root root -" + "f /usr/lib/libcuda.so 0444 root root - fakeContent" + "L /usr/lib/imaginary-fhs-drivers/libcuda.so 0444 root root - /usr/lib/libcuda.so" ]; }; testScript = '' diff --git a/nixos/tests/nix-required-mounts/test-require-feature.nix b/nixos/tests/nix-required-mounts/test-require-feature.nix index 061b59e1628a..647d9a92d4a3 100644 --- a/nixos/tests/nix-required-mounts/test-require-feature.nix +++ b/nixos/tests/nix-required-mounts/test-require-feature.nix @@ -8,9 +8,19 @@ pkgs.runCommandNoCC "${feature}-present" echo "The host declares ${feature} support, but doesn't expose /${feature}-files" >&2 exit 1 fi - if [[ ! -f /run/opengl-driver/lib/libcuda.so ]] ; then + libcudaLocation=/run/opengl-driver/lib/libcuda.so + if [[ -e "$libcudaLocation" || -h "$libcudaLocation" ]] ; then + true # we're good + else echo "The host declares ${feature} support, but it the hook fails to handle the hostPath != guestPath cases" >&2 exit 1 fi + if cat "$libcudaLocation" | xargs test fakeContent = ; then + true # we're good + else + echo "The host declares ${feature} support, but it seems to fail to follow symlinks" >&2 + echo "The content of /run/opengl-driver/lib/libcuda.so is: $(cat /run/opengl-driver/lib/libcuda.so)" >&2 + exit 1 + fi touch $out '' diff --git a/pkgs/by-name/ni/nix-required-mounts/nix_required_mounts.py b/pkgs/by-name/ni/nix-required-mounts/nix_required_mounts.py index 50f0f80f0bf2..029740e4b80c 100644 --- a/pkgs/by-name/ni/nix-required-mounts/nix_required_mounts.py +++ b/pkgs/by-name/ni/nix-required-mounts/nix_required_mounts.py @@ -143,17 +143,24 @@ def entrypoint(): mounts: List[Tuple[PathString, PathString]] = [] while queue: - guest_path, host_path, follow_symlinks = queue.popleft() - if (guest_path, host_path) not in unique_mounts: - mounts.append((guest_path, host_path)) - unique_mounts.add((guest_path, host_path)) + guest_path_str, host_path_str, follow_symlinks = queue.popleft() + if (guest_path_str, host_path_str) not in unique_mounts: + mounts.append((guest_path_str, host_path_str)) + unique_mounts.add((guest_path_str, host_path_str)) if not follow_symlinks: continue - for parent in symlink_parents(Path(host_path)): - parent_str = parent.absolute().as_posix() - queue.append((parent_str, parent_str, follow_symlinks)) + host_path = Path(host_path_str) + if not (host_path.is_dir() or host_path.is_symlink()): + continue + + # assert host_path_str == guest_path_str, (host_path_str, guest_path_str) + + for child in host_path.iterdir() if host_path.is_dir() else [host_path]: + for parent in symlink_parents(child): + parent_str = parent.absolute().as_posix() + queue.append((parent_str, parent_str, follow_symlinks)) # the pre-build-hook command if args.issue_command == "always" or ( @@ -165,8 +172,8 @@ def entrypoint(): print_paths = False # arguments, one per line - for guest_path, host_path in mounts if print_paths else []: - print(f"{guest_path}={host_path}") + for guest_path_str, host_path_str in mounts if print_paths else []: + print(f"{guest_path_str}={host_path_str}") # terminated by an empty line something_to_terminate = args.issue_stop == "conditional" and mounts