Skip to content

Commit

Permalink
feat: Provide a devShell with packages from enabled services (#355)
Browse files Browse the repository at this point in the history
resolves #194
  • Loading branch information
shivaraj-bh authored Oct 10, 2024
1 parent 9ee3aa3 commit 6e1251b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
17 changes: 17 additions & 0 deletions doc/devshell.md
Original file line number Diff line number Diff line change
@@ -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;
];
# ...
};
}
```

1 change: 1 addition & 0 deletions doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# Guide

- [[datadir]]#
- [[devshell]]#
- [[custom-service]]#
3 changes: 2 additions & 1 deletion example/share-services/northwind/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
}
3 changes: 2 additions & 1 deletion example/share-services/pgweb/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,6 +32,7 @@
environment.PGWEB_DATABASE_URL = config.services.postgres.northwind.connectionURI { dbName = "sample"; };
};
};
devShells.default = config.process-compose."default".services.outputs.devShell;
};
};
}
5 changes: 4 additions & 1 deletion example/simple/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -61,6 +61,9 @@
};

devShells.default = pkgs.mkShell {
inputsFrom = [
config.process-compose."default".services.outputs.devShell
];
nativeBuildInputs = [ pkgs.just ];
};
};
Expand Down
7 changes: 5 additions & 2 deletions nix/services/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ let
inherit (import ../lib.nix) multiService;
in
{
imports = builtins.map multiService [
imports = (builtins.map multiService [
./apache-kafka.nix
./clickhouse
./elasticsearch.nix
./mongodb.nix
./mysql
./nginx
./ollama.nix
Expand All @@ -23,6 +24,8 @@ in
./weaviate.nix
./searxng.nix
./tika.nix
./mongodb.nix
]) ++ [
./devshell.nix
];

}
50 changes: 50 additions & 0 deletions nix/services/devshell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ config, pkgs, lib, ... }:
let
inherit (lib) types;
in
{
options.services.outputs = {
enabledServices = lib.mkOption {
type = types.attrsOf (types.listOf types.str);
readOnly = true;
description = ''
Names of all the enabled service instances, grouped by the service name.
'';
example = lib.literalExpression ''
{
mysql = [ "m1" "m2" ];
redis = [ "r1" ];
}
'';
};
devShell = lib.mkOption {
type = types.package;
readOnly = true;
description = ''
The devShell that aggregates packages from all the enabled services.
'';
};
};

config = {
services.outputs.enabledServices = lib.pipe config.services [
# `outputs` is a reserved attribute set and is not the name of a service.
(lib.filterAttrs (n: _: n != "outputs"))

(lib.mapAttrs (_service: instances:
lib.attrNames (lib.filterAttrs (_: v: v.enable) instances)))
];

services.outputs.devShell = pkgs.mkShell {
packages = lib.pipe config.services.outputs.enabledServices [
(lib.mapAttrsToList (service: instances:
map
(instance:
lib.attrByPath ([ service instance "package" ]) (builtins.throw "${service}.${instance} doesn't define a `package` option") config.services)
instances))

lib.flatten
];
};
};
}

0 comments on commit 6e1251b

Please sign in to comment.