os/doc/outputs/modules.md

90 lines
1.7 KiB
Markdown
Raw Normal View History

2021-02-14 02:38:20 +00:00
# Modules
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
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 02:26:27 +00:00
`nixosModules.<file-basename>`, and imported into all [hosts](../concepts/hosts.md).
2021-02-14 02:38:20 +00:00
> ##### _Note:_
2023-01-28 20:49:10 +00:00
>
2021-02-14 02:38:20 +00:00
> 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 02:26:27 +00:00
> , use [profiles](../concepts/profiles.md) instead.
2021-02-14 02:38:20 +00:00
## Semantics
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
In case you've never written a module for nixpkgs before, here is a brief
outline of the process.
### Declaration
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
modules/services/service-category/my-service.nix:
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
```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
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
modules/module-list.nix:
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
```nix
[
./services/service-category/my-service.nix
]
```
## Usage
### Internal
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
profiles/profile-category/my-profile.nix:
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
```nix
{ ... }:
{
services.MyService.enable = true;
}
```
### External
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
flake.nix:
2023-01-28 20:49:10 +00:00
2021-02-14 02:38:20 +00:00
```nix
{
# inputs omitted
2021-02-18 01:31:33 +00:00
outputs = { self, devos, nixpkgs, ... }: {
2021-02-14 02:38:20 +00:00
nixosConfigurations.myConfig = nixpkgs.lib.nixosSystem {
system = "...";
modules = [
2021-02-18 01:31:33 +00:00
devos.nixosModules.my-service
2021-02-14 02:38:20 +00:00
({ ... }: {
services.MyService.enable = true;
})
];
};
};
}
```
[nixpkgs-modules]: https://github.com/NixOS/nixpkgs/tree/master/nixos/modules