diff --git a/CHANGELOG.md b/CHANGELOG.md index b0fdc82..4c5658f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - #39: Allow `test` process to act as a test, which then gets run as part of flake checks. - #55: Add `lib` flake output - library of useful functions - #80: Add `evalModules`, to use process-compose-flake without flake-parts + - #102: Add `defaults.processSettings` option to set default settings for all processes - New options - #52: Add `is_foreground` option - ~~#54: Add `apiServer` option to control REST API server~~ diff --git a/doc/index.md b/doc/index.md index 557a24e..b5a9162 100644 --- a/doc/index.md +++ b/doc/index.md @@ -100,6 +100,40 @@ process-compose.watch-server = { }; ``` +### Default settings for all processes + +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. + +```nix +process-compose.watch-server = { + defaults.processSettings = { name, ... }: { + # Set default namespace for all processes + namespace = lib.mkDefault "watch-server"; + # Set default restart behavior + availability.restart = lib.mkDefault "on_failure"; + availability.max_restarts = lib.mkDefault 3; + # Use the process name in log locations + log_location = ".logs/${name}.log"; + }; + + settings.processes = { + backend-server.command = "..."; + frontend-server.command = "..."; + # This process overrides the default namespace + proxy-server = { + command = "..."; + namespace = "proxy"; # Overrides the default + }; + }; +}; +``` + +**Important:** Use `lib.mkDefault` when setting defaults to ensure individual process settings can override them. + +You can access the process `name` parameter to create dynamic defaults (e.g., per-process log files). + +You can disable defaults entirely by setting `defaults.enable = false`. + ## Module API Our submodule mirrors the [process-compose YAML schema](https://github.com/F1bonacc1/process-compose/blob/main/process-compose.yaml). A few things to remember: diff --git a/example/flake.nix b/example/flake.nix index 396330d..1c91fb7 100644 --- a/example/flake.nix +++ b/example/flake.nix @@ -23,6 +23,11 @@ dataFile = "data.sqlite"; in { + # Default settings for all processes + defaults.processSettings = { + namespace = lib.mkDefault "sqlite-demo"; + }; + cli = { # environment.PC_DISABLE_TUI = true; # Global options for `process-compose` diff --git a/nix/process-compose/default.nix b/nix/process-compose/default.nix index 8ce07d9..c24d5b3 100644 --- a/nix/process-compose/default.nix +++ b/nix/process-compose/default.nix @@ -6,6 +6,7 @@ in { imports = [ ./cli.nix + ./defaults.nix ./settings ./test.nix ]; diff --git a/nix/process-compose/defaults.nix b/nix/process-compose/defaults.nix new file mode 100644 index 0000000..ad310ca --- /dev/null +++ b/nix/process-compose/defaults.nix @@ -0,0 +1,43 @@ +# A module representing the default values for all processes in process-compose-flake. +{ name, lib, config, ... }: +let + inherit (lib) + mkOption + types; +in +{ + options.defaults = { + enable = mkOption { + type = types.bool; + description = '' + Whether to enable default settings for processes in this configuration. + ''; + default = true; + }; + + processSettings = mkOption { + type = types.deferredModule; + description = '' + Default settings that will be applied to all processes in this configuration. + + Individual process settings can override these defaults. When setting defaults, + use `lib.mkDefault` to ensure individual process settings take precedence. + + Example: + ```nix + defaults.processSettings = { + availability.restart = lib.mkDefault "on_failure"; + availability.max_restarts = lib.mkDefault 3; + namespace = lib.mkDefault "myapp"; + }; + ``` + ''; + apply = settings: + if config.defaults.enable then + settings + else + { }; + default = { }; + }; + }; +} diff --git a/nix/process-compose/settings/default.nix b/nix/process-compose/settings/default.nix index c00c7d9..327f4f0 100644 --- a/nix/process-compose/settings/default.nix +++ b/nix/process-compose/settings/default.nix @@ -10,7 +10,12 @@ in modules = [{ options = { processes = mkOption { - type = types.attrsOf (types.submoduleWith { modules = [ ./process.nix ]; }); + type = types.attrsOf (types.submoduleWith { + modules = [ + ./process.nix + config.defaults.processSettings + ]; + }); default = { }; description = '' A map of process names to their configuration. diff --git a/nix/process-compose/settings/process.nix b/nix/process-compose/settings/process.nix index f65bcb1..3e64617 100644 --- a/nix/process-compose/settings/process.nix +++ b/nix/process-compose/settings/process.nix @@ -217,7 +217,7 @@ in example = true; description = '' Whether the process is disabled. Useful when a process is required to be started only in a given scenario, like while running in CI. - + Even if disabled, the process is still listed in the TUI and the REST client, and can be started manually when needed. ''; };