feat: add lib to flake outputs exposing multiService func

closes #25
This commit is contained in:
Adrian Gierakowski 2023-07-20 21:51:19 +01:00 committed by Sridhar Ratnakumar
parent 2c1bbc1258
commit 14904d0b7f
3 changed files with 36 additions and 33 deletions

View File

@ -7,6 +7,8 @@
path = builtins.path { path = ./example; filter = path: _: baseNameOf path == "flake.nix"; };
};
lib = import ./nix/lib.nix;
# Config for https://github.com/srid/nixci
# To run this, `nix run github:srid/nixci`
nixci = let overrideInputs = { "services-flake" = ./.; }; in {

View File

@ -1,38 +1,7 @@
{ pkgs, lib, ... }:
let
# 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:
let
# Derive name from filename
name = lib.pipe mod [
builtins.baseNameOf
(lib.strings.splitString ".")
builtins.head
];
in
{ config, ... }: {
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))
];
};
in
{
inherit (import ./lib.nix) multiService;
in {
imports = builtins.map multiService [
./apache-kafka.nix
./postgres.nix

32
nix/lib.nix Normal file
View File

@ -0,0 +1,32 @@
{
# 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))
];
};
}