Skip to content

Commit 8b8eea9

Browse files
authored
Multiple services (#13)
1 parent 197fc1c commit 8b8eea9

File tree

10 files changed

+504
-485
lines changed

10 files changed

+504
-485
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ TODO
1616

1717
## Services available
1818

19-
- [x] Hello World
2019
- [x] PostgreSQL
2120
- [ ] MySQL
2221
- [x] Redis

example/flake.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/flake.nix

+11-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
inputs.services-flake.processComposeModules.default
2828
];
2929

30-
services.postgres = {
30+
services.postgres."pg1" = {
3131
enable = true;
3232
listen_addresses = "127.0.0.1";
3333
initialDatabases = [
@@ -38,23 +38,29 @@
3838
];
3939
};
4040

41+
services.postgres."pg2" = {
42+
enable = true;
43+
listen_addresses = "127.0.0.1";
44+
port = 5433;
45+
};
46+
4147
settings.processes.pgweb =
4248
let
43-
pgcfg = config.services.postgres;
49+
pgcfg = config.services.postgres.pg1;
4450
in
4551
{
4652
environment.PGWEB_DATABASE_URL = "postgres://$USER@${pgcfg.listen_addresses}:${builtins.toString pgcfg.port}/${dbName}";
4753
command = pkgs.pgweb;
48-
depends_on."postgres".condition = "process_healthy";
54+
depends_on."pg1".condition = "process_healthy";
4955
};
5056

5157
# Set this attribute and get NixOS VM tests, as a flake check, for free.
5258
testScript = ''
5359
# FIXME: pgweb is still pending, but only in VM tests for some reason.
5460
process_compose.wait_until(lambda procs:
55-
procs["postgres"]["status"] == "Running"
61+
procs["pg1"]["status"] == "Running"
5662
)
57-
machine.succeed("echo 'SELECT version();' | ${config.services.postgres.package}/bin/psql -h 127.0.0.1 -U tester ${dbName}")
63+
machine.succeed("echo 'SELECT version();' | ${config.services.postgres.pg1.package}/bin/psql -h 127.0.0.1 -U tester ${dbName}")
5864
'';
5965
};
6066
};

nix/default.nix

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
1+
{ pkgs, lib, ... }:
2+
let
3+
# Create an attrsOf module wrapper (`services.${name}`) around the given submodule.
4+
#
5+
# where module filename is of form `${name}.nix`. The submodule takes this
6+
# 'name' parameter, and is expected to set the final process-compose config in
7+
# its `outputs.settings` option.
8+
multiService = mod:
9+
let
10+
# Derive name from filename
11+
name = lib.pipe mod [
12+
builtins.baseNameOf
13+
(lib.strings.splitString ".")
14+
builtins.head
15+
];
16+
in
17+
{ config, ... }: {
18+
options.services.${name} = lib.mkOption {
19+
description = ''
20+
${name} service
21+
'';
22+
default = { };
23+
type = lib.types.attrsOf (lib.types.submoduleWith {
24+
specialArgs = { inherit pkgs; };
25+
modules = [ mod ];
26+
});
27+
};
28+
config.settings.imports =
29+
lib.pipe config.services.${name} [
30+
(lib.filterAttrs (_: cfg: cfg.enable))
31+
(lib.mapAttrsToList (_: cfg: cfg.outputs.settings))
32+
];
33+
};
34+
in
135
{
2-
imports = [
3-
./hello.nix
36+
imports = builtins.map multiService [
437
./postgres.nix
538
./redis.nix
639
];

nix/hello.nix

-24
This file was deleted.

0 commit comments

Comments
 (0)