Skip to content

Commit

Permalink
make arguments configurable via options
Browse files Browse the repository at this point in the history
  • Loading branch information
VanCoding committed Sep 29, 2024
1 parent f02a8e4 commit cd3e722
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 30 deletions.
1 change: 1 addition & 0 deletions nix/eval-modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion nix/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
Expand Down
137 changes: 137 additions & 0 deletions nix/process-compose-flake-lib.nix
Original file line number Diff line number Diff line change
@@ -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 = { };
};
}
33 changes: 18 additions & 15 deletions nix/process-compose/cli.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, ... }:
{ lib, config, process-compose-flake-lib, ... }:

let
inherit (lib) types mkOption;
Expand Down Expand Up @@ -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 = { };
Expand All @@ -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}" ];
})
];
};
}

20 changes: 6 additions & 14 deletions nix/process-compose/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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}
'';
Expand All @@ -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
Expand All @@ -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;
};
}

0 comments on commit cd3e722

Please sign in to comment.