Skip to content

Commit 15ad16e

Browse files
authored
Merge pull request #15 from Platonic-Systems/split
Refactor: split into modules
2 parents 7d49fbd + c5cd60e commit 15ad16e

File tree

3 files changed

+139
-101
lines changed

3 files changed

+139
-101
lines changed

nix/flake-module.nix

Lines changed: 23 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -9,108 +9,30 @@ 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;
54-
description = ''
55-
Port to serve process-compose's Swagger API on.
56-
'';
57-
};
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-
}));
12+
options.perSystem = mkPerSystemOption ({ config, pkgs, lib, ... }:
13+
let
14+
submoduleWithPkgs = mod:
15+
types.submoduleWith {
16+
specialArgs = { inherit pkgs lib; };
17+
modules = [ mod ];
8518
};
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;
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 {
27+
imports = [
28+
./process-compose
29+
];
30+
});
11331
};
114-
};
32+
33+
config.packages = lib.mapAttrs
34+
(name: cfg: cfg.outputs.package)
35+
config.process-compose;
36+
});
11537
}
11638

nix/process-compose/default.nix

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{ name, config, pkgs, lib, ... }:
2+
3+
let
4+
inherit (lib) types mkOption literalExpression;
5+
in
6+
{
7+
imports = [
8+
./settings.nix
9+
];
10+
11+
options = {
12+
package = mkOption {
13+
type = types.package;
14+
default = pkgs.process-compose;
15+
defaultText = lib.literalExpression "pkgs.process-compose";
16+
description = ''
17+
The process-compose package to bundle up in the command package and flake app.
18+
'';
19+
};
20+
port = mkOption {
21+
type = types.nullOr types.int;
22+
default = null;
23+
description = ''
24+
Port to serve process-compose's Swagger API on.
25+
'';
26+
};
27+
tui = mkOption {
28+
type = types.nullOr types.bool;
29+
default = null;
30+
description = "Enable or disable the TUI for the application.";
31+
};
32+
extraCliArgs =
33+
let
34+
cliArgsAttr = {
35+
port = "-p ${toString config.port}";
36+
tui = "-t=${lib.boolToString config.tui}";
37+
};
38+
getCliArgs =
39+
lib.mapAttrsToList
40+
(opt: arg: lib.optionalString (config.${opt} != null) arg)
41+
cliArgsAttr;
42+
in
43+
mkOption {
44+
type = types.str;
45+
default = lib.concatStringsSep " " getCliArgs;
46+
internal = true;
47+
readOnly = true;
48+
description = ''
49+
Extra command-line arguments to pass to process-compose.
50+
'';
51+
};
52+
outputs.package = mkOption {
53+
type = types.package;
54+
description = ''
55+
The final package that will run 'process-compose up' for this configuration.
56+
'';
57+
};
58+
};
59+
60+
config.outputs.package =
61+
pkgs.writeShellApplication {
62+
inherit name;
63+
runtimeInputs = [ config.package ];
64+
text = ''
65+
process-compose up \
66+
-f ${config.settingsYaml} \
67+
${config.extraCliArgs} \
68+
"$@"
69+
'';
70+
};
71+
}
72+

nix/process-compose/settings.nix

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

0 commit comments

Comments
 (0)