diff --git a/.gitignore b/.gitignore index d9dfaaf..6299478 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,9 @@ /result # created when `just run ` is used /test/data + +# created when `just run ex-*` is used +/example/share-services/pgweb/data/ +/example/simple/data/ + /.pre-commit-config.yaml diff --git a/example/share-services/README.md b/example/share-services/README.md new file mode 100644 index 0000000..7c83aca --- /dev/null +++ b/example/share-services/README.md @@ -0,0 +1,14 @@ +A demonstration of sharing services across flakes. + +To run, + +``` +cd ./example/share-services +nix run +``` + +To run using latest code, go to the repository root and run the following in a Nix shell: + +```sh +just ex-share-services +``` diff --git a/example/share-services/northwind/flake.lock b/example/share-services/northwind/flake.lock new file mode 100644 index 0000000..1d0540d --- /dev/null +++ b/example/share-services/northwind/flake.lock @@ -0,0 +1,129 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1709336216, + "narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1710097495, + "narHash": "sha256-B7Ea7q7hU7SE8wOPJ9oXEBjvB89yl2csaLjf5v/7jr8=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d40e866b1f98698d454dad8f592fe7616ff705a4", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1709237383, + "narHash": "sha256-cy6ArO4k5qTx+l5o+0mL9f5fa86tYUX3ozE1S+Txlds=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1536926ef5621b09bba54035ae2bb6d806d72ac8", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "northwind": { + "flake": false, + "locked": { + "lastModified": 1666887048, + "narHash": "sha256-wwqHEkZUTNkg78OLnRDX5JPoRYop8v5UI2hxZTclxs8=", + "owner": "pthom", + "repo": "northwind_psql", + "rev": "3d271b23f3357532e63f92ffb10c4f258dfd20af", + "type": "github" + }, + "original": { + "owner": "pthom", + "repo": "northwind_psql", + "type": "github" + } + }, + "process-compose-flake": { + "locked": { + "lastModified": 1708624100, + "narHash": "sha256-zZPheCD9JGg2EtK4A9BsIdyl8447egOow4fjIfHFHRg=", + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "rev": "44d260ddba5a51570dee54d5cd4d8984edaf98c2", + "type": "github" + }, + "original": { + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs", + "northwind": "northwind", + "process-compose-flake": "process-compose-flake", + "services-flake": "services-flake", + "systems": "systems" + } + }, + "services-flake": { + "locked": { + "lastModified": 1709843710, + "narHash": "sha256-BP9Bp725C6Cp3k4YZfNbvPiRm6+3s5SCCPKOPyFpGZY=", + "owner": "juspay", + "repo": "services-flake", + "rev": "218fa6cc9a875c20def622e974fb4e0f391b3b6d", + "type": "github" + }, + "original": { + "owner": "juspay", + "repo": "services-flake", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/example/share-services/northwind/flake.nix b/example/share-services/northwind/flake.nix new file mode 100644 index 0000000..922eff1 --- /dev/null +++ b/example/share-services/northwind/flake.nix @@ -0,0 +1,30 @@ +{ + description = "A flake module exposing postgres service with northwind sample database"; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + systems.url = "github:nix-systems/default"; + process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; + services-flake.url = "github:juspay/services-flake"; + + northwind.url = "github:pthom/northwind_psql"; + northwind.flake = false; + }; + outputs = inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + systems = import inputs.systems; + imports = [ + inputs.process-compose-flake.flakeModule + ]; + flake.processComposeModules.default = + import ./services.nix { inherit inputs; }; + perSystem = { self', pkgs, lib, ... }: { + process-compose."default" = { config, ... }: { + imports = [ + inputs.services-flake.processComposeModules.default + inputs.self.processComposeModules.default + ]; + }; + }; + }; +} diff --git a/example/share-services/northwind/services.nix b/example/share-services/northwind/services.nix new file mode 100644 index 0000000..ce29ce8 --- /dev/null +++ b/example/share-services/northwind/services.nix @@ -0,0 +1,20 @@ +{ inputs }: +let + globalSocket = { name, ... }: { + # Required due to socket length being limited to 100 chars, see: https://github.com/juspay/services-flake/pull/77 + socketDir = "$HOME/.services/postgres/${name}"; + }; +in +{ + services.postgres."northwind" = { + imports = [ globalSocket ]; + enable = true; + listen_addresses = "127.0.0.1"; + initialDatabases = [ + { + name = "sample"; + schemas = [ "${inputs.northwind}/northwind.sql" ]; + } + ]; + }; +} diff --git a/example/share-services/pgweb/flake.lock b/example/share-services/pgweb/flake.lock new file mode 100644 index 0000000..d97483f --- /dev/null +++ b/example/share-services/pgweb/flake.lock @@ -0,0 +1,252 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1709336216, + "narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_2": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib_2" + }, + "locked": { + "lastModified": 1709336216, + "narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1710097495, + "narHash": "sha256-B7Ea7q7hU7SE8wOPJ9oXEBjvB89yl2csaLjf5v/7jr8=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d40e866b1f98698d454dad8f592fe7616ff705a4", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1709237383, + "narHash": "sha256-cy6ArO4k5qTx+l5o+0mL9f5fa86tYUX3ozE1S+Txlds=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1536926ef5621b09bba54035ae2bb6d806d72ac8", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib_2": { + "locked": { + "dir": "lib", + "lastModified": 1709237383, + "narHash": "sha256-cy6ArO4k5qTx+l5o+0mL9f5fa86tYUX3ozE1S+Txlds=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1536926ef5621b09bba54035ae2bb6d806d72ac8", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1710097495, + "narHash": "sha256-B7Ea7q7hU7SE8wOPJ9oXEBjvB89yl2csaLjf5v/7jr8=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d40e866b1f98698d454dad8f592fe7616ff705a4", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "northwind": { + "inputs": { + "flake-parts": "flake-parts_2", + "nixpkgs": "nixpkgs_2", + "northwind": "northwind_2", + "process-compose-flake": "process-compose-flake", + "services-flake": "services-flake", + "systems": "systems" + }, + "locked": { + "dir": "example/share-services/northwind", + "lastModified": 1710167564, + "narHash": "sha256-StRjzomzbYEho0bpDGIJK6HdFgBTm3gcMxu3XDvrjkY=", + "owner": "juspay", + "repo": "services-flake", + "rev": "fc3cebb519a44c2eeb7b850080b300779488f19b", + "type": "github" + }, + "original": { + "dir": "example/share-services/northwind", + "owner": "juspay", + "ref": "share-services", + "repo": "services-flake", + "type": "github" + } + }, + "northwind_2": { + "flake": false, + "locked": { + "lastModified": 1666887048, + "narHash": "sha256-wwqHEkZUTNkg78OLnRDX5JPoRYop8v5UI2hxZTclxs8=", + "owner": "pthom", + "repo": "northwind_psql", + "rev": "3d271b23f3357532e63f92ffb10c4f258dfd20af", + "type": "github" + }, + "original": { + "owner": "pthom", + "repo": "northwind_psql", + "type": "github" + } + }, + "process-compose-flake": { + "locked": { + "lastModified": 1708624100, + "narHash": "sha256-zZPheCD9JGg2EtK4A9BsIdyl8447egOow4fjIfHFHRg=", + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "rev": "44d260ddba5a51570dee54d5cd4d8984edaf98c2", + "type": "github" + }, + "original": { + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "type": "github" + } + }, + "process-compose-flake_2": { + "locked": { + "lastModified": 1708624100, + "narHash": "sha256-zZPheCD9JGg2EtK4A9BsIdyl8447egOow4fjIfHFHRg=", + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "rev": "44d260ddba5a51570dee54d5cd4d8984edaf98c2", + "type": "github" + }, + "original": { + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs", + "northwind": "northwind", + "process-compose-flake": "process-compose-flake_2", + "services-flake": "services-flake_2", + "systems": "systems_2" + } + }, + "services-flake": { + "locked": { + "lastModified": 1709843710, + "narHash": "sha256-BP9Bp725C6Cp3k4YZfNbvPiRm6+3s5SCCPKOPyFpGZY=", + "owner": "juspay", + "repo": "services-flake", + "rev": "218fa6cc9a875c20def622e974fb4e0f391b3b6d", + "type": "github" + }, + "original": { + "owner": "juspay", + "repo": "services-flake", + "type": "github" + } + }, + "services-flake_2": { + "locked": { + "lastModified": 1709843710, + "narHash": "sha256-BP9Bp725C6Cp3k4YZfNbvPiRm6+3s5SCCPKOPyFpGZY=", + "owner": "juspay", + "repo": "services-flake", + "rev": "218fa6cc9a875c20def622e974fb4e0f391b3b6d", + "type": "github" + }, + "original": { + "owner": "juspay", + "repo": "services-flake", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/example/share-services/pgweb/flake.nix b/example/share-services/pgweb/flake.nix new file mode 100644 index 0000000..41c8624 --- /dev/null +++ b/example/share-services/pgweb/flake.nix @@ -0,0 +1,43 @@ +{ + description = "pgweb frontend for the northwind db in ../northwind flake"; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + systems.url = "github:nix-systems/default"; + process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; + services-flake.url = "github:juspay/services-flake"; + + # TODO: Remove 'share-services' branch before merging to `main`. + northwind.url = "github:juspay/services-flake/share-services?dir=example/share-services/northwind"; + }; + outputs = inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + systems = import inputs.systems; + imports = [ + inputs.process-compose-flake.flakeModule + ]; + perSystem = { self', pkgs, lib, ... }: { + process-compose."default" = { config, ... }: { + imports = [ + inputs.services-flake.processComposeModules.default + # Importing this brings whatever processes/services the + # ../northwind/services.nix module exposes, which in our case is a + # postgresql process loaded with northwind sample database. + inputs.northwind.processComposeModules.default + ]; + + # Add a pgweb process, that knows how to connect to our northwind db + settings.processes.pgweb = { + command = pkgs.pgweb; + depends_on."northwind".condition = "process_healthy"; + environment.PGWEB_DATABASE_URL = + let + inherit (config.services.postgres.northwind) + listen_addresses port; + in + "postgres://$USER@${listen_addresses}:${builtins.toString port}/sample"; + }; + }; + }; + }; +} diff --git a/example/flake.lock b/example/simple/flake.lock similarity index 100% rename from example/flake.lock rename to example/simple/flake.lock diff --git a/example/flake.nix b/example/simple/flake.nix similarity index 92% rename from example/flake.nix rename to example/simple/flake.nix index 08cb988..9effb46 100644 --- a/example/flake.nix +++ b/example/simple/flake.nix @@ -17,7 +17,9 @@ inputs.process-compose-flake.flakeModule ]; perSystem = { self', pkgs, lib, ... }: { - # This adds a `self.packages.default` + # `process-compose.foo` will add a flake package output called "foo". + # Therefore, this will add a default package that you can build using + # `nix build` and run using `nix run`. process-compose."default" = { config, ... }: let dbName = "sample"; diff --git a/example/justfile b/example/simple/justfile similarity index 100% rename from example/justfile rename to example/simple/justfile diff --git a/flake.nix b/flake.nix index bca37e3..201ee44 100644 --- a/flake.nix +++ b/flake.nix @@ -12,9 +12,16 @@ # Config for https://github.com/srid/nixci # To run this, `nix run github:srid/nixci` nixci.default = let overrideInputs = { "services-flake" = ./.; }; in { - example = { + simple-example = { inherit overrideInputs; - dir = "./example"; + dir = "./example/simple"; + }; + share-services-example = { + overrideInputs = { + inherit (overrideInputs) services-flake; + northwind = ./example/share-services/northwind; + }; + dir = "./example/share-services/pgweb"; }; test = { inherit overrideInputs; diff --git a/justfile b/justfile index f9790ff..192616e 100644 --- a/justfile +++ b/justfile @@ -2,9 +2,16 @@ default: @just --list -# Run example -ex: - cd ./example && nix run . --override-input services-flake .. +# Run example/simple +ex-simple: + cd ./example/simple && nix run . --override-input services-flake ../.. + +# Run example/share-services +ex-share-services: + cd ./example/share-services/pgweb && \ + nix run . \ + --override-input services-flake ../../.. \ + --override-input northwind ../northwind \ # Auto-format the project tree fmt: