Building a NixOS Server: A Step-by-Step Guide

Manavendra SenManavendra Sen
3 min read

I had an old laptop lying around—a Dell Inspiron 7572 with a 1TB SSD and 16GB RAM. Instead of letting it gather dust, I decided to repurpose it into a powerful deployment server using NixOS. My goal was to self-host services instead of relying on AWS or other cloud providers. In this blog, I'll walk you through the setup process, from installing NixOS to configuring SSH, monitoring server health, and more.

1. What is NixOS?

NixOS is a unique Linux distribution known for its declarative configuration model. This means that you define the entire system configuration in a single file, which describes all aspects of the system: installed packages, configuration files, and system services. This approach ensures that your system is reproducible and consistent. If you need to rebuild your system or set it up on another machine, you simply use the same configuration file.

Key Features of NixOS:

  • Declarative System Configuration: The state of the system is fully described in a single configuration file.

  • Reproducibility: Ensures that the system can be reproduced exactly on any machine.

  • Atomic Upgrades and Rollbacks: Allows for safe system upgrades and the ability to roll back to previous configurations.

  • Isolation of Dependencies: Different versions of packages can coexist without conflicts.

1. Installing NixOS

To get started, we'll install NixOS on your laptop.

Prerequisites:

  • A USB drive (at least 4GB)

  • NixOS ISO image, which can be downloaded from NixOS Downloads

Follow this guide to setup NixOS - https://youtu.be/GymWdyizBRA

3. What is SSH?

SSH (Secure Shell) is a protocol for securely logging into a remote machine over an insecure network. It provides a secure channel over an unsecured network in a client-server architecture, connecting an SSH client application with an SSH server.

Login Remotely Using Password:

https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server

  • Find your server's IP address:

      ip a
    

    Use an SSH client to log in:

    •   ssh user@server-ip
      
  • Set Up SSH Key Authentication:

    • Generate SSH key pair on your local machine:

        ssh-keygen -t rsa -b 2048
      
    • Copy the public key to the server:

        ssh-copy-id user@server-ip
      

Now, you can log in using your key pair:

ssh -i ./.ssh/<key name> user@server-ip

4. Installing Cockpit for Server Health Monitoring

Cockpit is a web-based interface that makes it easy to administer your Linux servers. It allows you to monitor system health, manage storage, configure networks, and inspect logs.

https://fictionbecomesfact.com/nixos-cockpit

5. Accessing the App on the Internet

To make your application accessible over the internet, you need to set up port forwarding on your router. This allows external traffic to reach your server.

Private IP and Public IP explained - https://youtu.be/92b-jjBURkw

6. Using ngrok

Because my router didn't allow port forwarding I used ngrok.
ngrok is a tool that creates a secure tunnel to your localhost, making your local server accessible over the internet. Till I figure out how to setup a public IP, ngrok is good enough.

Setting Up ngrok:

  1. Download ngrok:

    https://ngrok.com/docs/guides/device-gateway/linux/

  2. Run ngrok:

     ./ngrok http 80
    
  3. Access your server: ngrok will provide a public URL that tunnels to your local server.

7. Setting Up a CI/CD Pipeline and File Transfer Using SSH

A CI/CD pipeline automates the process of integrating and deploying code changes.

Setting up a deployment server with NixOS offers a robust and reproducible environment. By following these steps, you can configure SSH access, monitor server health with Cockpit, make your applications accessible over the internet, and set up a CI/CD pipeline. NixOS's declarative approach ensures that your system setup is consistent and easy :)

11
Subscribe to my newsletter

Read articles from Manavendra Sen directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manavendra Sen
Manavendra Sen