|
1 | 1 | # Share services |
2 | 2 |
|
3 | | -See <https://github.com/juspay/services-flake/tree/main/example/share-services> |
| 3 | +Let's say you have two projects: `foo` and `bar`. `foo` defines a service that needs to be used by `bar`. Both `foo` and `bar`, being separate projects, have their own `flake.nix`. In order for `bar` to reuse `foo` service instead of redefining it, `foo` can export `processComposeModules` in its flake `outputs`. `processComposeModules` is not a reserved output; it can be named anything, but the naming is appropriate for this scenario. |
| 4 | + |
| 5 | +Next, we will see basic `flake.nix` for `foo` and `bar`. You can find a more real-world example at <https://github.com/juspay/services-flake/tree/main/example/share-services>. |
| 6 | + |
| 7 | +## foo (Exports its service) |
| 8 | + |
| 9 | +```nix |
| 10 | +# foo/flake.nix |
| 11 | +{ |
| 12 | + inputs = { |
| 13 | + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; |
| 14 | + flake-parts.url = "github:hercules-ci/flake-parts"; |
| 15 | + process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; |
| 16 | + services-flake.url = "github:juspay/services-flake"; |
| 17 | + }; |
| 18 | + outputs = inputs: |
| 19 | + inputs.flake-parts.lib.mkFlake { inherit inputs; } { |
| 20 | + systems = [ "x86_64-linux" "aarch64-darwin" ]; |
| 21 | + imports = [ |
| 22 | + inputs.process-compose-flake.flakeModule |
| 23 | + ]; |
| 24 | + flake.processComposeModules.default = ./services.nix; |
| 25 | + perSystem = { pkgs, lib, ... }: { |
| 26 | + process-compose."default" = { |
| 27 | + imports = [ |
| 28 | + inputs.services-flake.processComposeModules.default |
| 29 | + inputs.self.processComposeModules.default |
| 30 | + ]; |
| 31 | + }; |
| 32 | + }; |
| 33 | + }; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +[[custom-service]] exported by `foo` as `processComposeModules.default`: |
| 38 | + |
| 39 | +```nix |
| 40 | +# foo/services.nix |
| 41 | +{ pkgs, lib, ... }: { |
| 42 | + options = { |
| 43 | + services.foo = { |
| 44 | + enable = lib.mkEnableOption "Enable foo service"; |
| 45 | + package = lib.mkPackageOption pkgs "foo" { }; |
| 46 | + }; |
| 47 | + }; |
| 48 | + config = let cfg = config.services.foo; in |
| 49 | + lib.mkIf cfg.enable { |
| 50 | + settings.processes.foo = { |
| 51 | + command = "${lib.getExe cfg.foo}"; |
| 52 | + }; |
| 53 | + }; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## bar (Imports foo service) |
| 58 | + |
| 59 | +`bar` wants to reuse `foo`'s service. |
| 60 | + |
| 61 | +```nix |
| 62 | +# bar/flake.nix |
| 63 | +{ |
| 64 | + inputs = { |
| 65 | + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; |
| 66 | + flake-parts.url = "github:hercules-ci/flake-parts"; |
| 67 | + process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; |
| 68 | + services-flake.url = "github:juspay/services-flake"; |
| 69 | + foo.url = "<foo-source>"; |
| 70 | + }; |
| 71 | + outputs = inputs: |
| 72 | + inputs.flake-parts.lib.mkFlake { inherit inputs; } { |
| 73 | + systems = [ "x86_64-linux" "aarch64-darwin" ]; |
| 74 | + imports = [ |
| 75 | + inputs.process-compose-flake.flakeModule |
| 76 | + ]; |
| 77 | + flake.processComposeModules.default = ./services.nix; |
| 78 | + perSystem = { pkgs, lib, ... }: { |
| 79 | + process-compose."default" = { |
| 80 | + imports = [ |
| 81 | + inputs.services-flake.processComposeModules.default |
| 82 | + inputs.foo.processComposeModules.default |
| 83 | + ]; |
| 84 | + services.foo.enable = true; |
| 85 | + |
| 86 | + # The rest of bar's services goes here... |
| 87 | + }; |
| 88 | + }; |
| 89 | + }; |
| 90 | +} |
| 91 | +
|
| 92 | +``` |
0 commit comments