Skip to content

Commit 230955d

Browse files
committed
Add submodule type for YAML file
1 parent f2ec5b5 commit 230955d

File tree

11 files changed

+356
-67
lines changed

11 files changed

+356
-67
lines changed

example/flake.nix

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# This adds a `self.packages.default`
2020
process-compose."default" = {
2121
settings = {
22+
environment = [
23+
"DATAFILE=data.sqlite"
24+
];
2225
processes = {
2326

2427
# Print a pony every 2 seconds, because why not.
@@ -30,17 +33,23 @@
3033
'';
3134

3235
# Create .sqlite database from chinook database.
33-
sqlite-init.command = ''
34-
echo "`date`: Importing Chinook database..."
35-
${lib.getExe pkgs.sqlite} data.sqlite < ${inputs.chinookDb}/ChinookDatabase/DataSources/Chinook_Sqlite.sql
36-
echo "`date`: Done."
37-
'';
36+
sqlite-init.command = pkgs.writeShellApplication {
37+
name = "sqlite-init";
38+
text = ''
39+
echo "$(date): Importing Chinook database ($DATAFILE) ..."
40+
${lib.getExe pkgs.sqlite} "$DATAFILE" < ${inputs.chinookDb}/ChinookDatabase/DataSources/Chinook_Sqlite.sql
41+
echo "$(date): Done."
42+
'';
43+
};
3844

3945
# Run sqlite-web on the local chinook database.
4046
sqlite-web = {
41-
command = ''
42-
${pkgs.sqlite-web}/bin/sqlite_web data.sqlite
43-
'';
47+
command = pkgs.writeShellApplication {
48+
name = "sqlite-web";
49+
text = ''
50+
${pkgs.sqlite-web}/bin/sqlite_web "$DATAFILE"
51+
'';
52+
};
4453
# The 'depends_on' will have this process wait until the above one is completed.
4554
depends_on."sqlite-init".condition = "process_completed_successfully";
4655
};

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
outputs = { self, ... }: {
2+
outputs = _: {
33
flakeModule = ./nix/flake-module.nix;
44

55
templates.default = {

nix/process-compose/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ in
66
{
77
imports = [
88
./cli.nix
9-
./settings.nix
9+
./settings
1010
];
1111

1212
options = {

nix/process-compose/settings.nix

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{ lib, ... }:
2+
3+
args:
4+
lib.mkOption (args // {
5+
type = lib.types.either lib.types.package lib.types.str;
6+
apply = pkg:
7+
if builtins.isString pkg then pkg else
8+
lib.getExe pkg;
9+
})
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{ name, config, pkgs, lib, ... }:
2+
let
3+
inherit (lib) types mkOption literalExpression;
4+
submoduleWithPkgs = mod:
5+
types.submoduleWith {
6+
specialArgs = { inherit pkgs lib; };
7+
modules = [ mod ];
8+
};
9+
in
10+
{
11+
options = {
12+
settings = mkOption {
13+
type = types.submodule {
14+
options = {
15+
processes = mkOption {
16+
type = types.attrsOf (submoduleWithPkgs ./process.nix);
17+
default = { };
18+
description = ''
19+
A map of process names to their configuration.
20+
'';
21+
};
22+
23+
environment = import ./environment.nix { inherit lib; };
24+
25+
log_length = mkOption {
26+
type = types.nullOr types.ints.unsigned;
27+
default = null;
28+
example = 3000;
29+
};
30+
log_level = mkOption {
31+
type = types.nullOr (types.enum [
32+
"trace"
33+
"debug"
34+
"info"
35+
"warn"
36+
"error"
37+
"fatal"
38+
"panic"
39+
]);
40+
default = null;
41+
example = "info";
42+
};
43+
log_location = mkOption {
44+
type = types.nullOr types.str;
45+
default = null;
46+
example = "./pc.log";
47+
};
48+
49+
shell = {
50+
shell_argument = mkOption {
51+
type = types.str;
52+
default = "-c";
53+
example = "-c";
54+
};
55+
shell_command = import ./command.nix { inherit lib; } {
56+
description = ''
57+
The shell to use to run the process `command`s.
58+
59+
For reproducibility across systems, by default this uses
60+
`pkgs.bash`.
61+
'';
62+
default = pkgs.bash;
63+
};
64+
};
65+
66+
version = mkOption {
67+
type = types.nullOr types.str;
68+
default = null;
69+
example = "0.5";
70+
};
71+
};
72+
};
73+
example =
74+
# packages.${system}.watch-server becomes available
75+
# execute `nix run .#watch-server` or incude packages.${system}.watch-server
76+
# as a nativeBuildInput to your devShell
77+
literalExpression ''
78+
{
79+
watch-server = {
80+
processes = {
81+
backend = "''${pkgs.simple-http-server}";
82+
frontend = "''${pkgs.simple-http-server}";
83+
};
84+
};
85+
};
86+
'';
87+
description = ''
88+
For each attribute `x = process-compose config` a flake app and package `x` is added to the flake.
89+
Which runs process-compose with the declared config.
90+
'';
91+
};
92+
93+
outputs.settingsYaml = mkOption {
94+
type = types.attrsOf types.raw;
95+
internal = true;
96+
};
97+
};
98+
99+
config.outputs.settingsYaml =
100+
let
101+
removeNullAndEmptyAttrs = attrs:
102+
let
103+
f = lib.filterAttrsRecursive (key: value: value != null && value != { });
104+
# filterAttrsRecursive doesn't delete the *resulting* empty attrs, so we must
105+
# evaluate it again and to get rid of it.
106+
in
107+
lib.pipe attrs [ f f ];
108+
toYAMLFile =
109+
attrs:
110+
pkgs.runCommand "toYamlFile" { buildInputs = [ pkgs.yq-go ]; } ''
111+
yq -P '.' ${pkgs.writeTextFile { name = "process-compose-${name}.json"; text = (builtins.toJSON attrs); }} > $out
112+
'';
113+
in
114+
toYAMLFile (removeNullAndEmptyAttrs config.settings);
115+
}
116+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{ lib, ... }:
2+
lib.mkOption {
3+
type = lib.types.nullOr (lib.types.listOf lib.types.str);
4+
default = null;
5+
example = [ "ABC=2221" "PRINT_ERR=111" ];
6+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{ lib, ... }:
2+
3+
let
4+
inherit (lib) types mkOption;
5+
in
6+
{
7+
options = {
8+
failure_threshold = mkOption {
9+
type = types.ints.unsigned;
10+
default = 3;
11+
example = 3;
12+
};
13+
http_get = mkOption {
14+
type = types.nullOr (types.submodule {
15+
options = {
16+
host = mkOption {
17+
type = types.str;
18+
example = "google.com";
19+
};
20+
scheme = mkOption {
21+
type = types.str;
22+
default = "http";
23+
example = "http";
24+
};
25+
path = mkOption {
26+
type = types.str;
27+
default = "/";
28+
example = "/";
29+
};
30+
port = mkOption {
31+
type = types.port;
32+
example = "8080";
33+
};
34+
};
35+
});
36+
default = null;
37+
};
38+
exec = mkOption {
39+
type = types.nullOr (types.submodule {
40+
command = mkOption {
41+
type = types.str;
42+
example = "ps -ef | grep -v grep | grep my-proccess";
43+
};
44+
});
45+
default = null;
46+
};
47+
initial_delay_seconds = mkOption {
48+
type = types.ints.unsigned;
49+
default = 0;
50+
example = 0;
51+
};
52+
period_seconds = mkOption {
53+
type = types.ints.unsigned;
54+
default = 10;
55+
example = 10;
56+
};
57+
success_threshold = mkOption {
58+
type = types.ints.unsigned;
59+
default = 1;
60+
example = 1;
61+
};
62+
timeout_seconds = mkOption {
63+
type = types.ints.unsigned;
64+
default = 3;
65+
example = 3;
66+
};
67+
};
68+
}

0 commit comments

Comments
 (0)