From 695c97c5ba45598674f833151965c910ce676c10 Mon Sep 17 00:00:00 2001 From: Adrian Gierakowski Date: Thu, 20 Jul 2023 21:51:19 +0100 Subject: [PATCH] feat: add lib to flake outputs exposing multiService func closes #25 --- flake.nix | 2 ++ nix/default.nix | 35 ++--------------------------------- nix/lib.nix | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 nix/lib.nix diff --git a/flake.nix b/flake.nix index ac3c2a34..ca61089b 100644 --- a/flake.nix +++ b/flake.nix @@ -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 { diff --git a/nix/default.nix b/nix/default.nix index a2210fb6..d1209750 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -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 diff --git a/nix/lib.nix b/nix/lib.nix new file mode 100644 index 00000000..45a4a4ed --- /dev/null +++ b/nix/lib.nix @@ -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)) + ]; + }; +}