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

Allow for directories in module-list #224

Merged
merged 1 commit into from
Mar 29, 2021
Merged

Conversation

Pacman99
Copy link
Member

@Pacman99 Pacman99 commented Mar 28, 2021

fixes #221
building on #222 this PR improves the lib functions pathsToImportedAttrs and pathsIn. First to add support for directories. This does not support actually passing a file in a directory, so ./matrix/default.nix won't work but ./matrix will - I should probably document this somewhere.
Also I moved the filtering for nix files to pathsIn, since its only necessary for auto-import. We can assume that users would pass proper files in module-list.nix.

@Pacman99 Pacman99 requested a review from nrdxp March 28, 2021 03:45
@nrdxp
Copy link
Collaborator

nrdxp commented Mar 28, 2021

It might just be a translation issue, but I'm still not clear on exactly what the issue is in #221 or how this addresses it.

@Pacman99
Copy link
Member Author

Ohh so currently when you try to add a directory with a default.nix in module-list.nix, pathsToImportedAttrs filters it out. Because it only accepts files with the '.nix' extension. So the issue in #221 is when you pass a folder, it doesn't get included.
In this PR I change it so that the name is just the file if it doesn't have a '.nix' extension and if it does then it strips the '.nix'. And along with that I removed any filtering.
But then that breaks the overlays folder since now the folder's README will try to be imported. So I then add filtering in pathsIn, which I think logically makes sense, since thats where the auto-import is done.

@GTrunSec
Copy link
Contributor

when you try to add a directory with a default.nix in module-list.nix, pathsToImportedAttrs filters it out. Because it only accepts files with the '.nix' extension. So the issue in #221 is when you pass a folder, it doesn't get included.

Exactly, thanks for you clarifying my issue.

@nrdxp
Copy link
Collaborator

nrdxp commented Mar 28, 2021

Okay great.

Wouldn't a more minimal fix for pathsToImportedAttrs be:

{
  pathsToImportedAttrs = paths:
    let
      paths' = lib.filter
        (path: lib.hasSuffix ".nix" path
          # simply add another filter condition
          || lib.pathExists "${path}/default.nix")
        paths;
    in
    genAttrs' paths' (path: {
      name = lib.removeSuffix ".nix"
        # Safe as long this is just used as a name
        (builtins.unsafeDiscardStringContext (baseNameOf path));
      value = import path;
    });
}

I'll take a look at pathsIn to get a better understanding of the issue there.

@Pacman99
Copy link
Member Author

Ohh yeah that would work well. I don't mind switching to that.
But I still don't like that pathsToImportedAttrs does any filtering in the first place. I feel like that should be done in functions which are directly related to auto-importing. It doesn't make sense to try and filter things that a user explicitly passes in their module-list.

@nrdxp
Copy link
Collaborator

nrdxp commented Mar 28, 2021

I hear what your saying but I kinda feel the opposite. The final operation is an import, and if this is called on anything besides a valid nix file it will fail, so filtering is performed so invalid files cannot be imported.

I reciprocate your feeling, but about pathsIn instead. Users may want to collect paths of all files, not just nix files, which is fine since they might want them for something other than import, so no filtering should be done there.

@Pacman99
Copy link
Member Author

Pacman99 commented Mar 28, 2021

I hear what your saying but I kinda feel the opposite. The final operation is an import, and if this is called on anything besides a valid nix file it will fail, so filtering is performed so invalid files cannot be imported.

I reciprocate your feeling, but about pathsIn instead. Users may want to collect paths of all files, not just nix files, which is fine since they might want them for something other than import, so no filtering should be done there.

I see what your saying. Thanks for explaining the reasoning. I'll update this PR.

@Pacman99
Copy link
Member Author

updated, but I also updated teh relevant test and thats failing. I'll debug tomorrow.

@blaggacao
Copy link
Contributor

Is this also how nixpkgs module system works?

@nrdxp
Copy link
Collaborator

nrdxp commented Mar 28, 2021

Is this also how nixpkgs module system works?

In what respect?

tests/lib.nix Outdated
Comment on lines 49 to 57
./testPathsToImportedAttrs/dir
./testPathsToImportedAttrs/foo.nix
./testPathsToImportedAttrs/bar.nix
./testPathsToImportedAttrs/t.nix
./testPathsToImportedAttrs/f.nix
];

expected = {
dir = { a = 5; };
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm lost, this fails nix flake check. But when I run the exact expression in nix repl, this the output I get.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The following will fix it:

        "${self}/tests/testPathsToImportedAttrs/dir"
        "${self}/tests/testPathsToImportedAttrs/foo.nix"
        "${self}/tests/testPathsToImportedAttrs/bar.nix"
        "${self}/tests/testPathsToImportedAttrs/t.nix"
        "${self}/tests/testPathsToImportedAttrs/f.nix"

Copy link
Member Author

@Pacman99 Pacman99 Mar 29, 2021

Choose a reason for hiding this comment

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

fixed, good find, I never thought that this would be necessary

@Pacman99
Copy link
Member Author

Is this also how nixpkgs module system works?

For the most part yeah, you can pass files or directories. The difference here is that we want to export modules as an attribute, so we need some way to get a name from a file. Whereas nixpkgs, doesn't need to do that, since it instead lumps them all into nixosSystem.

@Pacman99 Pacman99 force-pushed the module-paths branch 2 times, most recently from 6c29b48 to 836e70b Compare March 29, 2021 15:52
check if directory has a default.nix and use directory name as key

Co-authored-by: Timothy DeHerrera <tim.deh@pm.me>
Copy link
Collaborator

@nrdxp nrdxp left a comment

Choose a reason for hiding this comment

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

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 29, 2021

Build succeeded:

@bors bors bot merged commit f14dcda into divnix:core Mar 29, 2021
@Pacman99 Pacman99 deleted the module-paths branch April 1, 2021 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pathsToImportedAttrs does not accept directories
4 participants