ark/doc/outputs/modules.md

80 lines
1.7 KiB
Markdown
Raw Normal View History

2021-02-14 05:38:20 +03:00
# Modules
The modules directory is a replica of nixpkg's NixOS [modules][nixpkgs-modules]
, and follows the same semantics. This allows for trivial upstreaming into
nixpkgs proper once your module is sufficiently stable.
All modules linked in _module-list.nix_ are automatically exported via
2021-04-19 05:26:27 +03:00
`nixosModules.<file-basename>`, and imported into all [hosts](../concepts/hosts.md).
2021-02-14 05:38:20 +03:00
> ##### _Note:_
> This is reserved for declaring brand new module options. If you just want to
> declare a coherent configuration of already existing and related NixOS options
2021-04-19 05:26:27 +03:00
> , use [profiles](../concepts/profiles.md) instead.
2021-02-14 05:38:20 +03:00
## Semantics
In case you've never written a module for nixpkgs before, here is a brief
outline of the process.
### Declaration
modules/services/service-category/my-service.nix:
```nix
{ config, lib, ... }:
let
cfg = config.services.myService;
in
{
options.services.myService = {
enable = lib.mkEnableOption "Description of my new service.";
# additional options ...
};
config = lib.mkIf cfg.enable {
# implementation ...
};
}
```
### Import
modules/module-list.nix:
```nix
[
./services/service-category/my-service.nix
]
```
## Usage
### Internal
profiles/profile-category/my-profile.nix:
```nix
{ ... }:
{
services.MyService.enable = true;
}
```
### External
flake.nix:
```nix
{
# inputs omitted
2021-02-18 04:31:33 +03:00
outputs = { self, devos, nixpkgs, ... }: {
2021-02-14 05:38:20 +03:00
nixosConfigurations.myConfig = nixpkgs.lib.nixosSystem {
system = "...";
modules = [
2021-02-18 04:31:33 +03:00
devos.nixosModules.my-service
2021-02-14 05:38:20 +03:00
({ ... }: {
services.MyService.enable = true;
})
];
};
};
}
```
[nixpkgs-modules]: https://github.com/NixOS/nixpkgs/tree/master/nixos/modules