Skip to content

Commit

Permalink
no-broken-symlinks: restrict checks to symlinks pointing inside the s…
Browse files Browse the repository at this point in the history
…tore
  • Loading branch information
ConnorBaker committed Jan 23, 2025
1 parent ff0f85a commit b2416f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/stdenv/stdenv.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,10 @@ This setup hook checks for, reports, and (by default) fails builds when "broken"

This hook can be disabled by setting `dontCheckForBrokenSymlinks`.

::: {.note}
The hook only considers symlinks with targets inside the Nix store.
:::

::: {.note}
The check for reflexivity is direct and does not account for transitivity, so this hook will not prevent cycles in symlinks.
:::
Expand Down
5 changes: 5 additions & 0 deletions pkgs/build-support/setup-hooks/no-broken-symlinks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ noBrokenSymlinks() {
symlinkTarget="$(realpath --no-symlinks --canonicalize-missing "$pathParent/$symlinkTarget")"
fi

if [[ $symlinkTarget != "$NIX_STORE"/* ]]; then
nixInfoLog "symlink $path points outside the Nix store; ignoring"
continue
fi

if [[ $path == "$symlinkTarget" ]]; then
nixErrorLog "the symlink $path is reflexive $symlinkTarget"
numReflexiveSymlinks+=1
Expand Down
14 changes: 14 additions & 0 deletions pkgs/test/stdenv/no-broken-symlinks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ let
ln -s${if absolute then "r" else ""} "$out/valid" "$out/valid-symlink"
'';

mkValidSymlinkOutsideNixStore = absolute: ''
ln -s${if absolute then "r" else ""} "/etc/my_file" "$out/valid-symlink"
'';

testBuilder =
{
name,
Expand Down Expand Up @@ -188,4 +192,14 @@ in
name = "pass-valid-symlink-absolute";
commands = [ (mkValidSymlink true) ];
};

pass-valid-symlink-outside-nix-store-relative = testBuilder {
name = "pass-valid-symlink-outside-nix-store-relative";
commands = [ (mkValidSymlinkOutsideNixStore false) ];
};

pass-valid-symlink-outside-nix-store-absolute = testBuilder {
name = "pass-valid-symlink-outside-nix-store-absolute";
commands = [ (mkValidSymlinkOutsideNixStore true) ];
};
}

0 comments on commit b2416f4

Please sign in to comment.