pub-solar-os/doc/outputs/modules.md

80 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2021-02-14 02:38:20 +00: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 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:_
> 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
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 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