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

  1. A running instance of nixOS latest.

  2. A user account on the system with sudo privileges.

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

  1. Add the Hasura package to nixOS:

    $ sudo nix-env -iA nixos.hasura-cli
    
  2. Verify the installation by running the following command:

    $ hasura migrate status
    

    If the command returns an error, try running hasura init to initialize the project.

Step 3: Configure Hasura

We need to configure Hasura to connect to the PostgreSQL database we created earlier.

  1. First, create a configuration file config.yaml in 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-write
    

    Replace [password] with the actual password you entered while creating the PostgreSQL user.

  2. Create a directory metadata in the same directory as the config.yaml file. 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.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!