From cd3e72261284843126f4a76a7e56dd980ab657bc Mon Sep 17 00:00:00 2001 From: Patrik Stutz Date: Sun, 29 Sep 2024 11:59:19 +0200 Subject: [PATCH] make arguments configurable via options --- nix/eval-modules.nix | 1 + nix/flake-module.nix | 5 +- nix/process-compose-flake-lib.nix | 137 ++++++++++++++++++++++++++++++ nix/process-compose/cli.nix | 33 +++---- nix/process-compose/default.nix | 20 ++--- 5 files changed, 166 insertions(+), 30 deletions(-) create mode 100644 nix/process-compose-flake-lib.nix diff --git a/nix/eval-modules.nix b/nix/eval-modules.nix index dab9d5f..855d7f6 100644 --- a/nix/eval-modules.nix +++ b/nix/eval-modules.nix @@ -2,6 +2,7 @@ rec { evalModules = { pkgs, name, modules }: (pkgs.lib.evalModules { specialArgs = { inherit name pkgs; + process-compose-flake-lib = (import ./process-compose-flake-lib.nix) { lib = pkgs.lib; }; }; modules = [ ./process-compose diff --git a/nix/flake-module.nix b/nix/flake-module.nix index 36dcd70..9d911db 100644 --- a/nix/flake-module.nix +++ b/nix/flake-module.nix @@ -16,7 +16,10 @@ in executables from process-compose configurations written as Nix attribute sets. ''; type = types.attrsOf (types.submoduleWith { - specialArgs = { inherit pkgs; }; + specialArgs = { + inherit pkgs; + process-compose-flake-lib = (import ./process-compose-flake-lib.nix) { inherit lib types; }; + }; modules = [ ./process-compose ]; diff --git a/nix/process-compose-flake-lib.nix b/nix/process-compose-flake-lib.nix new file mode 100644 index 0000000..2d8facc --- /dev/null +++ b/nix/process-compose-flake-lib.nix @@ -0,0 +1,137 @@ +{ lib }: +let + inherit (lib) types; +in +{ + mkProcessComposeArgumentsOption = {}: + lib.mkOption { + type = types.submodule + ({ config, lib, ... }: + let inherit (lib) types mkOption; + in + { + options = { + config = mkOption { + type = types.listOf types.str; + default = [ ]; + }; + detached = mkOption { + type = types.bool; + default = false; + }; + disable-dotenv = mkOption { + type = types.bool; + default = false; + }; + env = mkOption { + type = types.listOf types.str; + default = [ ]; + }; + hide-disabled = mkOption { + type = types.bool; + default = false; + }; + keep-project = mkOption { + type = types.bool; + default = false; + }; + namespace = mkOption { + type = types.listOf types.str; + default = [ ]; + }; + no-deps = mkOption { + type = types.bool; + default = false; + }; + ref-rate = mkOption { + type = types.str; + default = ""; + }; + reverse = mkOption { + type = types.bool; + default = false; + }; + sort = mkOption { + type = types.str; + default = ""; + }; + theme = mkOption { + type = types.str; + default = ""; + }; + tui = mkOption { + type = types.bool; + default = true; + }; + log-file = mkOption { + type = types.str; + default = ""; + }; + no-server = mkOption { + type = types.bool; + default = false; + + }; + ordered-shutdown = mkOption { + type = types.bool; + default = false; + }; + port = mkOption { + type = types.nullOr types.int; + default = null; + }; + read-only = mkOption { + type = types.bool; + default = false; + }; + unix-socket = mkOption { + type = types.str; + default = ""; + }; + use-uds = mkOption { + type = types.bool; + default = false; + }; + output = mkOption { + type = types.submodule { + options = { + global = mkOption { + type = types.str; + default = lib.escapeShellArgs ( + (lib.optionals (config.log-file != "") [ "--log-file" config.log-file ]) + ++ (lib.optionals config.no-server [ "--no-server" ]) + ++ (lib.optionals config.ordered-shutdown [ "--ordered-shutdown" ]) + ++ (lib.optionals (config.port != null) [ "--port" "${builtins.toString config.port}" ]) + ++ (lib.optionals config.read-only [ "--read-only" ]) + ++ (lib.optionals (config.unix-socket != "") [ "--unix-socket" config.unix-socket ]) + ++ (lib.optionals config.use-uds [ "--use-uds" ]) + ); + }; + up = mkOption { + type = types.str; + default = lib.escapeShellArgs ( + (lib.concatMap (v: [ "--config" v ]) config.config) + ++ (lib.optionals config.detached [ "--detached" ]) + ++ (lib.optionals config.disable-dotenv [ "--disable-dotenv" ]) + ++ (lib.concatMap (v: [ "--env" v ]) config.env) + ++ (lib.optionals config.hide-disabled [ "--hide-disabled" ]) + ++ (lib.optionals config.keep-project [ "--keep-project" ]) + ++ (lib.concatMap (v: [ "--namespace" v ]) config.namespace) + ++ (lib.optionals config.no-deps [ "--no-deps" ]) + ++ (lib.optionals (config.ref-rate != "") [ "--ref-rate" config.ref-rate ]) + ++ (lib.optionals config.reverse [ "--reverse" ]) + ++ (lib.optionals (config.sort != "") [ "--sort" config.sort ]) + ++ (lib.optionals (config.theme != "") [ "--theme" config.theme ]) + ++ (lib.optionals config.reverse [ "--reverse" ]) + ++ (lib.optionals (!config.tui) [ "--tui=false" ]) + ); + }; + }; + }; + default = { }; + }; + }; + }); + default = { }; + }; +} diff --git a/nix/process-compose/cli.nix b/nix/process-compose/cli.nix index 92c98c6..13ff578 100644 --- a/nix/process-compose/cli.nix +++ b/nix/process-compose/cli.nix @@ -1,4 +1,4 @@ -{ lib, ... }: +{ lib, config, process-compose-flake-lib, ... }: let inherit (lib) types mkOption; @@ -48,19 +48,6 @@ in specified location. ''; }; - outputs.cliOpts = lib.mkOption { - type = types.str; - internal = true; - readOnly = true; - default = - if config.enable - then '' - ${if config.port != null then "--port ${builtins.toString config.port}" else ""} \ - ${if builtins.isBool config.uds then if config.uds then "-U" else "" else "--unix-socket ${config.uds}"} \ - '' else '' - --no-server \ - ''; - }; }; }); default = { }; @@ -70,6 +57,22 @@ in default = true; description = "Enable or disable the TUI for the application."; }; + arguments = process-compose-flake-lib.mkProcessComposeArgumentsOption { }; + test-arguments = process-compose-flake-lib.mkProcessComposeArgumentsOption { }; }; + config = { + arguments = lib.mkMerge [{ + tui = config.tui; + port = config.httpServer.port; + use-uds = config.httpServer.uds != false; + unix-socket = if builtins.isString config.httpServer.uds then config.httpServer.uds else ""; + no-server = if config.httpServer.enable == true then true else false; + config = [ "${config.outputs.settingsFile}" ]; + }]; + test-arguments = lib.mkMerge [ + (config.arguments // { + config = [ "${config.outputs.settingsTestFile}" ]; + }) + ]; + }; } - diff --git a/nix/process-compose/default.nix b/nix/process-compose/default.nix index e46d885..36e75fa 100644 --- a/nix/process-compose/default.nix +++ b/nix/process-compose/default.nix @@ -37,21 +37,14 @@ in config.outputs = let - mkProcessComposeWrapper = { name, tui, httpServer, configFile, preHook, postHook }: + mkProcessComposeWrapper = { name, arguments, preHook, postHook, }: pkgs.writeShellApplication { inherit name; runtimeInputs = [ config.package ]; text = '' - export PC_CONFIG_FILES=${configFile} - ${ - # Once the following issue is fixed we should be able to simply do: - # export PC_DISABLE_TUI=${builtins.toJSON (!config.tui)} - # https://github.com/F1bonacc1/process-compose/issues/75 - if tui then "" else "export PC_DISABLE_TUI=true" - } ${preHook} - set -x; process-compose ${httpServer.outputs.cliOpts} "$@"; set +x + set -x; process-compose ${arguments.global} ${arguments.up}"$@"; set +x ${postHook} ''; @@ -62,8 +55,8 @@ in mkProcessComposeWrapper { inherit name; - inherit (config) tui httpServer preHook postHook; - configFile = config.outputs.settingsFile; + inherit (config) preHook postHook; + arguments = config.arguments.output; }; testPackage = if @@ -72,10 +65,9 @@ in mkProcessComposeWrapper { name = "${name}-test"; - inherit (config) tui httpServer preHook postHook; - configFile = config.outputs.settingsTestFile; + inherit (config) preHook postHook; + arguments = config.test-arguments.output; } else null; }; } -