Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

importModules

Adios comes with a function importModules, that will automatically import all the modules in a directory (provided they follow a certain schema).

Usage

Given this directory structure:

./modules
├── default.nix
├── foo
│   └── default.nix
└── bar
    ├── baz
    │   └── default.nix
    └── default.nix

If the root module at default.nix is defined like this:

adios:

{
  name = "root";
  # Other contents omitted
  modules = adios.lib.importTree ./.;
}

Then importTree will generate:

adios:

{
  name = "root";
  # Other contents omitted
  modules = {
    foo = import ./foo { inherit adios; };
    bar = import ./bar { inherit adios; };
  };
}

Notably, importModules is not recursive - the baz/ module was completely ignored. If the bar module wants to depend on another module defined within its folder, it should import those modules itself, like this:

adios:

{
  name = "bar";
  # Other contents omitted
  modules = adios.lib.importModules ./.;
}

Limitations

importTree expects all modules to:

  • Either be defined as:
    • a subfolder, under $MODULE_NAME/default.nix.
    • a Nix file, under $MODULE_NAME.nix (excluding default.nix).
  • take either adios: as the file's input, or a destructed version (commonly { types, ... }: to only take adios.types)
  • use the same name as the folder it's contained within

If your module tree doesn't follow this schema, then it's recommended to define your import logic manually. importTree is only a convenience function, and it's okay to not use it if your tree doesn't fit its schema.