services-flake/nix/lib.nix
2023-09-07 20:57:07 +05:30

35 lines
1012 B
Nix

{
# Create an attrsOf module wrapper (`services.${name}`) around the given submodule.
#
# where module filename is of form `${name}.nix`. The submodule takes this
# 'name' parameter, and is expected to set the final process-compose config in
# its `outputs.settings` option.
multiService = mod:
{ config, pkgs, lib, ... }:
let
# Derive name from filename
name = lib.pipe mod [
builtins.baseNameOf
(lib.strings.splitString ".")
builtins.head
];
in
{
options.services.${name} = lib.mkOption {
description = ''
${name} service
'';
default = { };
type = lib.types.attrsOf (lib.types.submoduleWith {
specialArgs = { inherit pkgs; };
modules = [ mod ];
});
};
config.settings.imports =
lib.pipe config.services.${name} [
(lib.filterAttrs (_: cfg: cfg.enable))
(lib.mapAttrsToList (_: cfg: cfg.outputs.settings))
];
};
}