Skip to content

Commit ed02f6b

Browse files
committed
Move settings def to its own module
1 parent 7d49fbd commit ed02f6b

File tree

2 files changed

+117
-99
lines changed

2 files changed

+117
-99
lines changed

nix/flake-module.nix

Lines changed: 74 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,108 +9,83 @@ let
99
literalExpression;
1010
in
1111
{
12-
options = {
13-
perSystem = mkPerSystemOption
14-
({ config, self', inputs', pkgs, system, ... }: {
15-
options.process-compose = mkOption {
16-
description = mdDoc ''
17-
process-compose-flake: creates [process-compose](https://github.com/F1bonacc1/process-compose)
18-
executables from process-compose configurations written as Nix attribute sets.
19-
'';
20-
type = types.attrsOf (types.submodule ({ config, ... }: {
21-
options = {
22-
package = mkOption {
23-
type = types.package;
24-
default = pkgs.process-compose;
25-
defaultText = lib.literalExpression "pkgs.process-compose";
26-
description = ''
27-
The process-compose package to bundle up in the command package and flake app.
28-
'';
29-
};
30-
settings = mkOption {
31-
type = (pkgs.formats.yaml { }).type;
32-
example =
33-
# packages.${system}.watch-server becomes available
34-
# execute `nix run .#watch-server` or incude packages.${system}.watch-server
35-
# as a nativeBuildInput to your devShell
36-
literalExpression ''
37-
{
38-
watch-server = {
39-
processes = {
40-
backend = "''${pkgs.simple-http-server}";
41-
frontend = "''${pkgs.simple-http-server}";
42-
};
43-
};
44-
};
45-
'';
46-
description = mdDoc ''
47-
For each attribute `x = process-compose config` a flake app and package `x` is added to the flake.
48-
Which runs process-compose with the declared config.
49-
'';
50-
};
51-
port = mkOption {
52-
type = types.nullOr types.int;
53-
default = null;
12+
options.perSystem = mkPerSystemOption ({ config, pkgs, lib, ... }:
13+
let
14+
submoduleWithPkgs = mod:
15+
types.submoduleWith {
16+
specialArgs = { inherit pkgs lib; };
17+
modules = [ mod ];
18+
};
19+
in
20+
{
21+
options.process-compose = mkOption {
22+
description = mdDoc ''
23+
process-compose-flake: creates [process-compose](https://github.com/F1bonacc1/process-compose)
24+
executables from process-compose configurations written as Nix attribute sets.
25+
'';
26+
type = types.attrsOf (submoduleWithPkgs ({ config, ... }: {
27+
imports = [
28+
./settings.nix
29+
];
30+
options = {
31+
package = mkOption {
32+
type = types.package;
33+
default = pkgs.process-compose;
34+
defaultText = lib.literalExpression "pkgs.process-compose";
35+
description = ''
36+
The process-compose package to bundle up in the command package and flake app.
37+
'';
38+
};
39+
port = mkOption {
40+
type = types.nullOr types.int;
41+
default = null;
42+
description = ''
43+
Port to serve process-compose's Swagger API on.
44+
'';
45+
};
46+
tui = mkOption {
47+
type = types.nullOr types.bool;
48+
default = null;
49+
description = "Enable or disable the TUI for the application.";
50+
};
51+
extraCliArgs =
52+
let
53+
cliArgsAttr = {
54+
port = "-p ${toString config.port}";
55+
tui = "-t=${lib.boolToString config.tui}";
56+
};
57+
getCliArgs =
58+
lib.mapAttrsToList
59+
(opt: arg: lib.optionalString (config.${opt} != null) arg)
60+
cliArgsAttr;
61+
in
62+
mkOption {
63+
type = types.str;
64+
default = lib.concatStringsSep " " getCliArgs;
65+
internal = true;
66+
readOnly = true;
5467
description = ''
55-
Port to serve process-compose's Swagger API on.
68+
Extra command-line arguments to pass to process-compose.
5669
'';
5770
};
58-
tui = mkOption {
59-
type = types.nullOr types.bool;
60-
default = null;
61-
description = "Enable or disable the TUI for the application.";
62-
};
63-
extraCliArgs =
64-
let
65-
cliArgsAttr = {
66-
port = "-p ${toString config.port}";
67-
tui = "-t=${lib.boolToString config.tui}";
68-
};
69-
getCliArgs =
70-
lib.mapAttrsToList
71-
(opt: arg: lib.optionalString (config.${opt} != null) arg)
72-
cliArgsAttr;
73-
in
74-
mkOption {
75-
type = types.str;
76-
default = lib.concatStringsSep " " getCliArgs;
77-
internal = true;
78-
readOnly = true;
79-
description = ''
80-
Extra command-line arguments to pass to process-compose.
81-
'';
82-
};
83-
};
84-
}));
85-
};
86-
});
87-
};
88-
config = {
89-
perSystem = { config, self', inputs', pkgs, ... }:
90-
let
91-
toYAMLFile =
92-
attrs:
93-
pkgs.runCommand "toYamlFile" { buildInputs = [ pkgs.yq-go ]; } ''
94-
yq -P '.' ${pkgs.writeTextFile { name = "tmp.json"; text = (builtins.toJSON attrs); }} > $out
95-
'';
96-
packages = pkgs.lib.mapAttrs
97-
(name: cfg:
98-
pkgs.writeShellApplication {
99-
inherit name;
100-
runtimeInputs = [ cfg.package ];
101-
text = ''
102-
process-compose up \
103-
-f ${toYAMLFile cfg.settings} \
104-
${cfg.extraCliArgs} \
105-
"$@"
106-
'';
107-
}
108-
)
109-
config.process-compose;
110-
in
111-
{
112-
inherit packages;
71+
};
72+
}));
11373
};
114-
};
74+
75+
config.packages = lib.mapAttrs
76+
(name: cfg:
77+
pkgs.writeShellApplication {
78+
inherit name;
79+
runtimeInputs = [ cfg.package ];
80+
text = ''
81+
process-compose up \
82+
-f ${cfg.settingsYaml} \
83+
${cfg.extraCliArgs} \
84+
"$@"
85+
'';
86+
}
87+
)
88+
config.process-compose;
89+
});
11590
}
11691

nix/settings.nix

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{ name, config, pkgs, lib, ... }:
2+
let
3+
inherit (lib) types mkOption literalExpression;
4+
in {
5+
options = {
6+
settings = mkOption {
7+
type = (pkgs.formats.yaml { }).type;
8+
example =
9+
# packages.${system}.watch-server becomes available
10+
# execute `nix run .#watch-server` or incude packages.${system}.watch-server
11+
# as a nativeBuildInput to your devShell
12+
literalExpression ''
13+
{
14+
watch-server = {
15+
processes = {
16+
backend = "''${pkgs.simple-http-server}";
17+
frontend = "''${pkgs.simple-http-server}";
18+
};
19+
};
20+
};
21+
'';
22+
description = ''
23+
For each attribute `x = process-compose config` a flake app and package `x` is added to the flake.
24+
Which runs process-compose with the declared config.
25+
'';
26+
};
27+
28+
settingsYaml = mkOption {
29+
type = types.attrsOf types.raw;
30+
internal = true;
31+
};
32+
};
33+
config.settingsYaml =
34+
let
35+
toYAMLFile =
36+
attrs:
37+
pkgs.runCommand "toYamlFile" { buildInputs = [ pkgs.yq-go ]; } ''
38+
yq -P '.' ${pkgs.writeTextFile { name = "process-compose-${name}.json"; text = (builtins.toJSON attrs); }} > $out
39+
'';
40+
in
41+
toYAMLFile config.settings;
42+
}
43+

0 commit comments

Comments
 (0)