Skip to content

Commit

Permalink
Multiple services (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh authored Jul 17, 2023
1 parent 197fc1c commit 8b8eea9
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 485 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ TODO

## Services available

- [x] Hello World
- [x] PostgreSQL
- [ ] MySQL
- [x] Redis
Expand Down
6 changes: 3 additions & 3 deletions example/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions example/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
inputs.services-flake.processComposeModules.default
];

services.postgres = {
services.postgres."pg1" = {
enable = true;
listen_addresses = "127.0.0.1";
initialDatabases = [
Expand All @@ -38,23 +38,29 @@
];
};

services.postgres."pg2" = {
enable = true;
listen_addresses = "127.0.0.1";
port = 5433;
};

settings.processes.pgweb =
let
pgcfg = config.services.postgres;
pgcfg = config.services.postgres.pg1;
in
{
environment.PGWEB_DATABASE_URL = "postgres://$USER@${pgcfg.listen_addresses}:${builtins.toString pgcfg.port}/${dbName}";
command = pkgs.pgweb;
depends_on."postgres".condition = "process_healthy";
depends_on."pg1".condition = "process_healthy";
};

# Set this attribute and get NixOS VM tests, as a flake check, for free.
testScript = ''
# FIXME: pgweb is still pending, but only in VM tests for some reason.
process_compose.wait_until(lambda procs:
procs["postgres"]["status"] == "Running"
procs["pg1"]["status"] == "Running"
)
machine.succeed("echo 'SELECT version();' | ${config.services.postgres.package}/bin/psql -h 127.0.0.1 -U tester ${dbName}")
machine.succeed("echo 'SELECT version();' | ${config.services.postgres.pg1.package}/bin/psql -h 127.0.0.1 -U tester ${dbName}")
'';
};
};
Expand Down
37 changes: 35 additions & 2 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
{ 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
{
imports = [
./hello.nix
imports = builtins.map multiService [
./postgres.nix
./redis.nix
];
Expand Down
24 changes: 0 additions & 24 deletions nix/hello.nix

This file was deleted.

Loading

0 comments on commit 8b8eea9

Please sign in to comment.