Skip to content

Commit

Permalink
feat: add lib to flake outputs exposing multiService func
Browse files Browse the repository at this point in the history
closes #25
  • Loading branch information
adrian-gierakowski authored and srid committed Sep 6, 2023
1 parent 2c1bbc1 commit 14904d0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 2 additions & 33 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 32 additions & 0 deletions nix/lib.nix
Original file line number Diff line number Diff line change
@@ -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))
];
};
}

0 comments on commit 14904d0

Please sign in to comment.