diff --git a/doc/devshell.md b/doc/devshell.md new file mode 100644 index 00000000..8e8478a5 --- /dev/null +++ b/doc/devshell.md @@ -0,0 +1,17 @@ +# DevShell + +`services-flake` uses [mkShell](https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell) function to provide a shell with packages of all the enabled services. + +```nix +# Inside `perSystem` +{ + process-compose."my-pc" = { ... }; + devShells.default = pkgs.mkShell { + inputsFrom = [ + config.process-compose."my-pc".services.outputs.devShell; + ]; + # ... + }; +} +``` + diff --git a/doc/guide.md b/doc/guide.md index 269ed11c..0b9c934b 100644 --- a/doc/guide.md +++ b/doc/guide.md @@ -2,4 +2,5 @@ # Guide - [[datadir]]# +- [[devshell]]# - [[custom-service]]# diff --git a/example/share-services/northwind/flake.nix b/example/share-services/northwind/flake.nix index 922eff1b..32589ba4 100644 --- a/example/share-services/northwind/flake.nix +++ b/example/share-services/northwind/flake.nix @@ -18,13 +18,14 @@ ]; flake.processComposeModules.default = import ./services.nix { inherit inputs; }; - perSystem = { self', pkgs, lib, ... }: { + perSystem = { self', pkgs, config, lib, ... }: { process-compose."default" = { config, ... }: { imports = [ inputs.services-flake.processComposeModules.default inputs.self.processComposeModules.default ]; }; + devShells.default = config.process-compose."default".services.outputs.devShell; }; }; } diff --git a/example/share-services/pgweb/flake.nix b/example/share-services/pgweb/flake.nix index 1f6320b2..7cccd617 100644 --- a/example/share-services/pgweb/flake.nix +++ b/example/share-services/pgweb/flake.nix @@ -15,7 +15,7 @@ imports = [ inputs.process-compose-flake.flakeModule ]; - perSystem = { self', pkgs, lib, ... }: { + perSystem = { self', pkgs, config, lib, ... }: { process-compose."default" = { config, ... }: { imports = [ inputs.services-flake.processComposeModules.default @@ -32,6 +32,7 @@ environment.PGWEB_DATABASE_URL = config.services.postgres.northwind.connectionURI { dbName = "sample"; }; }; }; + devShells.default = config.process-compose."default".services.outputs.devShell; }; }; } diff --git a/example/simple/flake.nix b/example/simple/flake.nix index 797e9b35..edc01c5e 100644 --- a/example/simple/flake.nix +++ b/example/simple/flake.nix @@ -16,7 +16,7 @@ imports = [ inputs.process-compose-flake.flakeModule ]; - perSystem = { self', pkgs, lib, ... }: { + perSystem = { self', pkgs, config, lib, ... }: { # `process-compose.foo` will add a flake package output called "foo". # Therefore, this will add a default package that you can build using # `nix build` and run using `nix run`. @@ -61,6 +61,9 @@ }; devShells.default = pkgs.mkShell { + inputsFrom = [ + config.process-compose."default".services.outputs.devShell + ]; nativeBuildInputs = [ pkgs.just ]; }; }; diff --git a/nix/services/default.nix b/nix/services/default.nix index 5591c5e3..5dc75b3d 100644 --- a/nix/services/default.nix +++ b/nix/services/default.nix @@ -1,3 +1,4 @@ +{ pkgs, lib, config, ... }: let inherit (import ../lib.nix) multiService; in @@ -24,4 +25,30 @@ in ./searxng.nix ./tika.nix ]; + + options.services.outputs.devShell = lib.mkOption { + type = lib.types.package; + readOnly = true; + description = '' + The devShell that aggregates packages from all the enabled services. + ''; + }; + + config = { + services.outputs.devShell = pkgs.mkShell { + packages = + lib.pipe config.services [ + # `outputs` is a reserved attribute set and is not the name of a service. + (lib.filterAttrs (n: _: n != "outputs")) + # Flatten services attrset + # + # Example: + # Input = { mysql."m1" = ; mysql."m2" = ; redis."r1" = ; } + # Output = [ ] + (lib.foldlAttrs (acc: _: v: (lib.attrValues v) ++ acc) [ ]) + (lib.filter (service: service.enable)) + (map (service: service.package)) + ]; + }; + }; }