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 committed Jul 20, 2023
1 parent f3a7ea9 commit bdf7997
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
path = builtins.path { path = ./example; filter = path: _: baseNameOf path == "flake.nix"; };
};

# NOTE: this is a lib factory, as it takes: { pkgs, lib }
# so before using we need to pass it to pkgs.callPackage
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 { inherit pkgs lib; }) multiService;
in {
imports = builtins.map multiService [
./postgres.nix
./redis.nix
Expand Down
33 changes: 33 additions & 0 deletions nix/lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{ pkgs, lib ? pkgs.lib }: {
# 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))
];
};
}

0 comments on commit bdf7997

Please sign in to comment.