Skip to content

Commit

Permalink
feat(nix): add support for running external services through services…
Browse files Browse the repository at this point in the history
…-flake (#6377)
  • Loading branch information
kashif-m authored Nov 5, 2024
1 parent 71d9933 commit 95f2e0b
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,9 @@ result*
node_modules/

# cypress credentials
creds.json
creds.json

/.direnv

# Nix services data
/data
43 changes: 43 additions & 0 deletions docs/try_local_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Check the Table Of Contents to jump to the relevant section.
- [Run hyperswitch using Docker Compose](#run-hyperswitch-using-docker-compose)
- [Running additional services](#running-additional-services)
- [Set up a development environment using Docker Compose](#set-up-a-development-environment-using-docker-compose)
- [Set up a Nix development environment](#set-up-a-nix-development-environment)
- [Install Nix](#install-nix)
- [Using external services through Nix](#using-external-services-through-nix)
- [Develop in a Nix environment (coming soon)](#develop-in-a-nix-environment-coming-soon)
- [Set up a Rust environment and other dependencies](#set-up-a-rust-environment-and-other-dependencies)
- [Set up dependencies on Ubuntu-based systems](#set-up-dependencies-on-ubuntu-based-systems)
- [Set up dependencies on Windows (Ubuntu on WSL2)](#set-up-dependencies-on-windows-ubuntu-on-wsl2)
Expand Down Expand Up @@ -166,6 +170,43 @@ Once the services have been confirmed to be up and running, you can proceed with
If the command returned a `200 OK` status code, proceed with
[trying out our APIs](#try-out-our-apis).

## Set up a Nix development environment

A Nix development environment simplifies the setup of required project dependencies. This is available for MacOS, Linux and WSL2 users.

### Install nix

We recommend that you install Nix using [the DetSys nix-installer][detsys-nixos-installer], which automatically enables flakes.

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].

### Using external services through Nix

Once Nix is installed, you can use it to manage external services via `flakes`. More services will be added soon.

- Run below command in hyperswitch directory

```shell
nix run .#ext-services
```

This will start the following services using `process-compose`
- PostgreSQL
- Creates database and an user to be used by the application
- Redis

### Develop in a Nix environment (coming soon)

Nix development environment ensures all the required project dependencies, including both the tools and services are readily available, eliminating the need for manual setup.

Run below command in hyperswitch directory

```shell
nix develop
```

**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.

## Set up a Rust environment and other dependencies

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 36 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
# TODO: Move away from these to https://github.com/juspay/rust-flake
cargo2nix.url = "github:cargo2nix/cargo2nix/release-0.11.0";
rust-overlay.url = "github:oxalica/rust-overlay";

process-compose-flake.url = "github:Platonic-Systems/process-compose-flake";
services-flake.url = "github:juspay/services-flake";
};

outputs = inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ inputs.process-compose-flake.flakeModule ];
systems = inputs.nixpkgs.lib.systems.flakeExposed;
perSystem = { self', pkgs, lib, system, ... }:
let
Expand All @@ -27,17 +31,47 @@
devShells.default = pkgs.mkShell {
name = "hyperswitch-shell";
packages = with pkgs; [
just
nixd
openssl
pkg-config
exa
fd
rust-bin.stable.${rustVersion}.default
] ++ lib.optionals stdenv.isDarwin [
# arch might have issue finding these libs.
frameworks.CoreServices
frameworks.Foundation
];
};

/* For running external services
- Redis
- Postgres
*/
process-compose."ext-services" =
let
developmentToml = lib.importTOML ./config/development.toml;
databaseName = developmentToml.master_database.dbname;
databaseUser = developmentToml.master_database.username;
databasePass = developmentToml.master_database.password;
in
{
imports = [ inputs.services-flake.processComposeModules.default ];
services.redis."r1".enable = true;
/* Postgres
- Create an user and grant all privileges
- Create a database
*/
services.postgres."p1" = {
enable = true;
initialScript = {
before = "CREATE USER ${databaseUser} WITH PASSWORD '${databasePass}' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;";
after = "GRANT ALL PRIVILEGES ON DATABASE ${databaseName} to ${databaseUser};";
};
initialDatabases = [
{ name = databaseName; }
];
};
};
};
};
}

0 comments on commit 95f2e0b

Please sign in to comment.