Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.packagesFromDirectoryRecursive: Improved documentation #359898

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions lib/filesystem.nix
Original file line number Diff line number Diff line change
Expand Up @@ -306,53 +306,78 @@ in
As a result, directories with no `.nix` files (including empty
directories) will be transformed into empty attribute sets.

# Inputs

Structured function argument

: Attribute set containing the following attributes.
Additional attributes are ignored.

`callPackage`

: `pkgs.callPackage`

Type: `Path -> AttrSet -> a`
# Type

`directory`
```
packagesFromDirectoryRecursive :: {
callPackage :: Path -> {} -> a,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does {} mean?

Copy link
Contributor Author

@nbraud nbraud Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty attrset; Path -> {} -> a is a supertype of Path -> Attrset -> a, which is (closer to) pkgs.callPackage's type.

In other words, the type annotation guarantees promises that packagesFromDirectoryRecursive will only ever invoke callPackage _ {}.

directory :: Path,
...
} -> AttrSet
```

: The directory to read package files from
# Inputs

Type: `Path`
`callPackage`
: The function used to convert a Nix file's path into a leaf of the attribute set.
It is typically the `callPackage` function, taken from either `pkgs` or a new scope corresponding to the `directory`.

`directory`
: The directory to read package files from.

# Type

```
packagesFromDirectoryRecursive :: AttrSet -> AttrSet
```

# Examples
:::{.example}
## `lib.filesystem.packagesFromDirectoryRecursive` usage example
## Basic use of `lib.packagesFromDirectoryRecursive`

```nix
packagesFromDirectoryRecursive {
inherit (pkgs) callPackage;
directory = ./my-packages;
}
=> { ... }
```

In this case, `callPackage` will only search `pkgs` for a file's input parameters.
In other words, a file cannot refer to another file in the directory in its input parameters.
:::

::::{.example}
## Create a scope for the nix files found in a directory
```nix
lib.makeScope pkgs.newScope (
self: packagesFromDirectoryRecursive {
callPackage = self.callPackage;
inherit (self) callPackage;
directory = ./my-packages;
}
)
=> { ... }
```

For example, take the following directory structure:
```
my-packages
├── a.nix → { b }: assert b ? b1; ...
└── b
├── b1.nix → { a }: ...
└── b2.nix
```

Here, `b1.nix` can specify `{ a }` as a parameter, which `callPackage` will resolve as expected.
Likewise, `a.nix` receive an attrset corresponding to the contents of the `b` directory.

:::{.note}
`a.nix` cannot directly take as inputs packages defined in a child directory, such as `b1`.
:::

:::{.warning}
As of now, `lib.packagesFromDirectoryRecursive` cannot create nested scopes for sub-directories.

In particular, files under `b/` can only require (as inputs) other files under `my-packages`,
but not to those in the same directory, nor those in a parent directory; e.g, `b2.nix` cannot directly
require `b1`.
:::
::::
*/
packagesFromDirectoryRecursive =
{
Expand Down
Loading