diff --git a/flake.nix b/flake.nix index ac3c2a34..54705fd6 100644 --- a/flake.nix +++ b/flake.nix @@ -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 { diff --git a/nix/default.nix b/nix/default.nix index 4aa2340d..31a7c120 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 { inherit pkgs lib; }) multiService; +in { imports = builtins.map multiService [ ./postgres.nix ./redis.nix diff --git a/nix/lib.nix b/nix/lib.nix new file mode 100644 index 00000000..3760cc8a --- /dev/null +++ b/nix/lib.nix @@ -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)) + ]; + }; +}