|
| 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 | + |
0 commit comments