lib.filesystem.resolveDefaultNix: init (#418824)

This commit is contained in:
Johannes Kirschbauer 2025-07-03 12:54:47 +02:00 committed by GitHub
commit 86603255c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 89 additions and 1 deletions

View File

@ -291,7 +291,7 @@ rec {
if loc != null then
loc.file + ":" + toString loc.line
else if !isFunction fn then
toString fn + optionalString (pathIsDirectory fn) "/default.nix"
toString (lib.filesystem.resolveDefaultNix fn)
else
"<unknown location>";
in

View File

@ -454,4 +454,46 @@ in
)
else
processDir args;
/**
Append `/default.nix` if the passed path is a directory.
# Type
```
resolveDefaultNix :: (Path | String) -> (Path | String)
```
# Inputs
A single argument which can be a [path](https://nix.dev/manual/nix/stable/language/types#type-path) value or a string containing an absolute path.
# Output
If the input refers to a directory that exists, the output is that same path with `/default.nix` appended.
Furthermore, if the input is a string that ends with `/`, `default.nix` is appended to it.
Otherwise, the input is returned unchanged.
# Examples
:::{.example}
## `lib.filesystem.resolveDefaultNix` usage example
This expression checks whether `a` and `b` refer to the same locally available Nix file path.
```nix
resolveDefaultNix a == resolveDefaultNix b
```
For instance, if `a` is `/some/dir` and `b` is `/some/dir/default.nix`, and `/some/dir/` exists, the expression evaluates to `true`, despite `a` and `b` being different references to the same Nix file.
*/
resolveDefaultNix =
v:
if pathIsDirectory v then
v + "/default.nix"
else if lib.isString v && hasSuffix "/" v then
# A path ending in `/` can only refer to a directory, so we take the hint, even if we can't verify the validity of the path's `/` assertion.
# A `/` is already present, so we don't add another one.
v + "default.nix"
else
v;
}

View File

@ -4255,4 +4255,50 @@ runTests {
};
};
};
testFilesystemResolveDefaultNixFile1 = {
expr = lib.filesystem.resolveDefaultNix ./foo.nix;
expected = ./foo.nix;
};
testFilesystemResolveDefaultNixFile2 = {
expr = lib.filesystem.resolveDefaultNix ./default.nix;
expected = ./default.nix;
};
testFilesystemResolveDefaultNixDir1 = {
expr = lib.filesystem.resolveDefaultNix ./.;
expected = ./default.nix;
};
testFilesystemResolveDefaultNixFile1_toString = {
expr = lib.filesystem.resolveDefaultNix (toString ./foo.nix);
expected = toString ./foo.nix;
};
testFilesystemResolveDefaultNixFile2_toString = {
expr = lib.filesystem.resolveDefaultNix (toString ./default.nix);
expected = toString ./default.nix;
};
testFilesystemResolveDefaultNixDir1_toString = {
expr = lib.filesystem.resolveDefaultNix (toString ./.);
expected = toString ./default.nix;
};
testFilesystemResolveDefaultNixDir1_toString2 = {
expr = lib.filesystem.resolveDefaultNix (toString ./.);
expected = toString ./. + "/default.nix";
};
testFilesystemResolveDefaultNixNonExistent = {
expr = lib.filesystem.resolveDefaultNix "/non-existent/this/does/not/exist/for/real/please-dont-mess-with-your-local-fs";
expected = "/non-existent/this/does/not/exist/for/real/please-dont-mess-with-your-local-fs";
};
testFilesystemResolveDefaultNixNonExistentDir = {
expr = lib.filesystem.resolveDefaultNix "/non-existent/this/does/not/exist/for/real/please-dont-mess-with-your-local-fs/";
expected = "/non-existent/this/does/not/exist/for/real/please-dont-mess-with-your-local-fs/default.nix";
};
}