How to install Hasura on nixOS Latest
Hasura is a popular open-source platform for building scalable and secure GraphQL APIs. NixOS is a Linux distribution known for its functional package manager and declarative configuration. In this tutorial, we'll go over the steps to install Hasura on the latest version of nixOS.
Prerequisites
A running instance of nixOS latest.
A user account on the system with sudo privileges.
Basic knowledge of the command line.
Step 1: Install PostgreSQL
Hasura relies on PostgreSQL as its database backend. We need to make sure PostgreSQL is installed and running.
To install PostgreSQL on nixOS, execute the following command:
$ sudo nix-env -iA nixos.postgresql
Next, we need to create a PostgreSQL user and database for Hasura.
$ sudo su - postgres
$ createdb hasura_db
$ createuser hasura_user -P
During the user creation, enter a secure password for the user.
Step 2: Install Hasura
We will now install Hasura on our nixOS system.
Add the Hasura package to nixOS:
$ sudo nix-env -iA nixos.hasura-cliVerify the installation by running the following command:
$ hasura migrate statusIf the command returns an error, try running
hasura initto initialize the project.
Step 3: Configure Hasura
We need to configure Hasura to connect to the PostgreSQL database we created earlier.
First, create a configuration file
config.yamlin any directory of your choice with the following content:version: 2 metadata_directory: metadata server: port: 8080 webhook: command: - /bin/bash - ./update-hasura-webhook.sh cors: # only necessary during development; do not set in production insecure_routes: "*" database: # change the connection details to match the PostgreSQL database # you created earlier endpoint: postgres://hasura_user:[password]@localhost:5432/hasura_db connection_pool_settings: # tweak these values as per your requirement, if necessary idle_timeout: 180 max_connections: 50 retries: 1 # row-level-security will be enabled by default, unless you set this to 'read-only' mode: read-writeReplace
[password]with the actual password you entered while creating the PostgreSQL user.Create a directory
metadatain the same directory as theconfig.yamlfile. This directory will store the metadata for your Hasura project.
Step 4: Start Hasura
To start Hasura, run the following command in the directory where you created config.yaml:
$ hasura console --config-file=config.yaml
This will start Hasura on port 8080, with the configuration specified in config.yaml. You can access the Hasura console by visiting http://localhost:8080/console.
Conclusion
That's it! You have successfully installed Hasura on the latest version of nixOS. You can now start building GraphQL APIs using Hasura. If you face any issues during the installation or configuration, refer to the Hasura documentation or the nixOS documentation.