Skip to content

Commit 6cb26cc

Browse files
authored
Add defaults.processSettings option for shared process configuration (#102)
1 parent 3c1fa0f commit 6cb26cc

File tree

7 files changed

+91
-2
lines changed

7 files changed

+91
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- #39: Allow `test` process to act as a test, which then gets run as part of flake checks.
1212
- #55: Add `lib` flake output - library of useful functions
1313
- #80: Add `evalModules`, to use process-compose-flake without flake-parts
14+
- #102: Add `defaults.processSettings` option to set default settings for all processes
1415
- New options
1516
- #52: Add `is_foreground` option
1617
- ~~#54: Add `apiServer` option to control REST API server~~

doc/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,40 @@ process-compose.watch-server = {
100100
};
101101
```
102102

103+
### Default settings for all processes
104+
105+
You can define default settings that apply to all processes using `defaults.processSettings`. This is useful when you want to set common configuration like namespace, restart behavior, or other settings across all processes.
106+
107+
```nix
108+
process-compose.watch-server = {
109+
defaults.processSettings = { name, ... }: {
110+
# Set default namespace for all processes
111+
namespace = lib.mkDefault "watch-server";
112+
# Set default restart behavior
113+
availability.restart = lib.mkDefault "on_failure";
114+
availability.max_restarts = lib.mkDefault 3;
115+
# Use the process name in log locations
116+
log_location = ".logs/${name}.log";
117+
};
118+
119+
settings.processes = {
120+
backend-server.command = "...";
121+
frontend-server.command = "...";
122+
# This process overrides the default namespace
123+
proxy-server = {
124+
command = "...";
125+
namespace = "proxy"; # Overrides the default
126+
};
127+
};
128+
};
129+
```
130+
131+
**Important:** Use `lib.mkDefault` when setting defaults to ensure individual process settings can override them.
132+
133+
You can access the process `name` parameter to create dynamic defaults (e.g., per-process log files).
134+
135+
You can disable defaults entirely by setting `defaults.enable = false`.
136+
103137
## Module API
104138

105139
Our submodule mirrors the [process-compose YAML schema](https://github.com/F1bonacc1/process-compose/blob/main/process-compose.yaml). A few things to remember:

example/flake.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
dataFile = "data.sqlite";
2424
in
2525
{
26+
# Default settings for all processes
27+
defaults.processSettings = {
28+
namespace = lib.mkDefault "sqlite-demo";
29+
};
30+
2631
cli = {
2732
# environment.PC_DISABLE_TUI = true;
2833
# Global options for `process-compose`

nix/process-compose/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ in
66
{
77
imports = [
88
./cli.nix
9+
./defaults.nix
910
./settings
1011
./test.nix
1112
];

nix/process-compose/defaults.nix

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# A module representing the default values for all processes in process-compose-flake.
2+
{ name, lib, config, ... }:
3+
let
4+
inherit (lib)
5+
mkOption
6+
types;
7+
in
8+
{
9+
options.defaults = {
10+
enable = mkOption {
11+
type = types.bool;
12+
description = ''
13+
Whether to enable default settings for processes in this configuration.
14+
'';
15+
default = true;
16+
};
17+
18+
processSettings = mkOption {
19+
type = types.deferredModule;
20+
description = ''
21+
Default settings that will be applied to all processes in this configuration.
22+
23+
Individual process settings can override these defaults. When setting defaults,
24+
use `lib.mkDefault` to ensure individual process settings take precedence.
25+
26+
Example:
27+
```nix
28+
defaults.processSettings = {
29+
availability.restart = lib.mkDefault "on_failure";
30+
availability.max_restarts = lib.mkDefault 3;
31+
namespace = lib.mkDefault "myapp";
32+
};
33+
```
34+
'';
35+
apply = settings:
36+
if config.defaults.enable then
37+
settings
38+
else
39+
{ };
40+
default = { };
41+
};
42+
};
43+
}

nix/process-compose/settings/default.nix

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ in
1010
modules = [{
1111
options = {
1212
processes = mkOption {
13-
type = types.attrsOf (types.submoduleWith { modules = [ ./process.nix ]; });
13+
type = types.attrsOf (types.submoduleWith {
14+
modules = [
15+
./process.nix
16+
config.defaults.processSettings
17+
];
18+
});
1419
default = { };
1520
description = ''
1621
A map of process names to their configuration.

nix/process-compose/settings/process.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ in
217217
example = true;
218218
description = ''
219219
Whether the process is disabled. Useful when a process is required to be started only in a given scenario, like while running in CI.
220-
220+
221221
Even if disabled, the process is still listed in the TUI and the REST client, and can be started manually when needed.
222222
'';
223223
};

0 commit comments

Comments
 (0)