Skip to content

Commit 95f2e0b

Browse files
authored
feat(nix): add support for running external services through services-flake (#6377)
1 parent 71d9933 commit 95f2e0b

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,9 @@ result*
263263
node_modules/
264264

265265
# cypress credentials
266-
creds.json
266+
creds.json
267+
268+
/.direnv
269+
270+
# Nix services data
271+
/data

docs/try_local_system.md

+43
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Check the Table Of Contents to jump to the relevant section.
1515
- [Run hyperswitch using Docker Compose](#run-hyperswitch-using-docker-compose)
1616
- [Running additional services](#running-additional-services)
1717
- [Set up a development environment using Docker Compose](#set-up-a-development-environment-using-docker-compose)
18+
- [Set up a Nix development environment](#set-up-a-nix-development-environment)
19+
- [Install Nix](#install-nix)
20+
- [Using external services through Nix](#using-external-services-through-nix)
21+
- [Develop in a Nix environment (coming soon)](#develop-in-a-nix-environment-coming-soon)
1822
- [Set up a Rust environment and other dependencies](#set-up-a-rust-environment-and-other-dependencies)
1923
- [Set up dependencies on Ubuntu-based systems](#set-up-dependencies-on-ubuntu-based-systems)
2024
- [Set up dependencies on Windows (Ubuntu on WSL2)](#set-up-dependencies-on-windows-ubuntu-on-wsl2)
@@ -166,6 +170,43 @@ Once the services have been confirmed to be up and running, you can proceed with
166170
If the command returned a `200 OK` status code, proceed with
167171
[trying out our APIs](#try-out-our-apis).
168172

173+
## Set up a Nix development environment
174+
175+
A Nix development environment simplifies the setup of required project dependencies. This is available for MacOS, Linux and WSL2 users.
176+
177+
### Install nix
178+
179+
We recommend that you install Nix using [the DetSys nix-installer][detsys-nixos-installer], which automatically enables flakes.
180+
181+
As an **optional** next step, if you are interested in using Nix to manage your dotfiles and local packages, you can setup [nixos-unified-template][nixos-unified-template-repo].
182+
183+
### Using external services through Nix
184+
185+
Once Nix is installed, you can use it to manage external services via `flakes`. More services will be added soon.
186+
187+
- Run below command in hyperswitch directory
188+
189+
```shell
190+
nix run .#ext-services
191+
```
192+
193+
This will start the following services using `process-compose`
194+
- PostgreSQL
195+
- Creates database and an user to be used by the application
196+
- Redis
197+
198+
### Develop in a Nix environment (coming soon)
199+
200+
Nix development environment ensures all the required project dependencies, including both the tools and services are readily available, eliminating the need for manual setup.
201+
202+
Run below command in hyperswitch directory
203+
204+
```shell
205+
nix develop
206+
```
207+
208+
**NOTE:** This is a work in progress, and only a selected commands are available at the moment. Look in `flake.nix` (hyperswitch-shell) for a full list of packages.
209+
169210
## Set up a Rust environment and other dependencies
170211

171212
If you are using `nix`, please skip the setup dependencies step and jump to
@@ -681,3 +722,5 @@ To explore more of our APIs, please check the remaining folders in the
681722
[refunds-create]: https://www.postman.com/hyperswitch/workspace/hyperswitch-development/request/25176162-4d1315c6-ac61-4411-8f7d-15d4e4e736a1
682723
[refunds-retrieve]: https://www.postman.com/hyperswitch/workspace/hyperswitch-development/request/25176162-137d6260-24f7-4752-9e69-26b61b83df0d
683724
[connector-specific-details]: https://docs.google.com/spreadsheets/d/e/2PACX-1vQWHLza9m5iO4Ol-tEBx22_Nnq8Mb3ISCWI53nrinIGLK8eHYmHGnvXFXUXEut8AFyGyI9DipsYaBLG/pubhtml?gid=748960791&single=true
725+
[detsys-nixos-installer]: https://nixos.asia/en/install
726+
[nixos-unified-template-repo]: https://github.com/juspay/nixos-unified-template#on-non-nixos

flake.lock

+36-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+36-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
# TODO: Move away from these to https://github.com/juspay/rust-flake
99
cargo2nix.url = "github:cargo2nix/cargo2nix/release-0.11.0";
1010
rust-overlay.url = "github:oxalica/rust-overlay";
11+
12+
process-compose-flake.url = "github:Platonic-Systems/process-compose-flake";
13+
services-flake.url = "github:juspay/services-flake";
1114
};
1215

1316
outputs = inputs:
1417
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
18+
imports = [ inputs.process-compose-flake.flakeModule ];
1519
systems = inputs.nixpkgs.lib.systems.flakeExposed;
1620
perSystem = { self', pkgs, lib, system, ... }:
1721
let
@@ -27,17 +31,47 @@
2731
devShells.default = pkgs.mkShell {
2832
name = "hyperswitch-shell";
2933
packages = with pkgs; [
34+
just
35+
nixd
3036
openssl
3137
pkg-config
32-
exa
33-
fd
3438
rust-bin.stable.${rustVersion}.default
3539
] ++ lib.optionals stdenv.isDarwin [
3640
# arch might have issue finding these libs.
3741
frameworks.CoreServices
3842
frameworks.Foundation
3943
];
4044
};
45+
46+
/* For running external services
47+
- Redis
48+
- Postgres
49+
*/
50+
process-compose."ext-services" =
51+
let
52+
developmentToml = lib.importTOML ./config/development.toml;
53+
databaseName = developmentToml.master_database.dbname;
54+
databaseUser = developmentToml.master_database.username;
55+
databasePass = developmentToml.master_database.password;
56+
in
57+
{
58+
imports = [ inputs.services-flake.processComposeModules.default ];
59+
services.redis."r1".enable = true;
60+
/* Postgres
61+
- Create an user and grant all privileges
62+
- Create a database
63+
*/
64+
services.postgres."p1" = {
65+
enable = true;
66+
initialScript = {
67+
before = "CREATE USER ${databaseUser} WITH PASSWORD '${databasePass}' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;";
68+
after = "GRANT ALL PRIVILEGES ON DATABASE ${databaseName} to ${databaseUser};";
69+
};
70+
initialDatabases = [
71+
{ name = databaseName; }
72+
];
73+
};
74+
};
4175
};
4276
};
4377
}

0 commit comments

Comments
 (0)