Skip to content

Commit

Permalink
feat(devShell): Output a shell with packages of all the enabled services
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh committed Oct 9, 2024
1 parent ca67793 commit 4e44899
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 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
27 changes: 27 additions & 0 deletions nix/services/default.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{ pkgs, lib, config, ... }:
let
inherit (import ../lib.nix) multiService;
in
Expand All @@ -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" = <cfg1>; mysql."m2" = <cfg2>; redis."r1" = <cfg3>; }
# Output = [ <cfg1> <cfg2> <cfg3> ]
(lib.foldlAttrs (acc: _: v: (lib.attrValues v) ++ acc) [ ])
(lib.filter (service: service.enable))
(map (service: service.package))
];
};
};
}

0 comments on commit 4e44899

Please sign in to comment.