How to Install Traefik on NixOS Latest

Traefik is a modern reverse proxy and load balancer that makes deploying services easy and efficient. This tutorial will walk you through the steps of installing Traefik on NixOS Latest.

Prerequisites

Step 1: Install Docker

Traefik is distributed as a Docker image, so the first step is to install Docker on your machine. To do so, add the following line to the environment.systemPackages list in your NixOS configuration file (/etc/nixos/configuration.nix):

environment.systemPackages = with pkgs; [ docker ];

Then, rebuild your system using the sudo nixos-rebuild switch command.

Step 2: Create a Traefik Configuration File

Create a configuration file for Traefik by running the following command:

sudo touch /etc/traefik.toml

Then, open the file using your favorite text editor and add the following configuration:

[entryPoints]
  [entryPoints.http]
  address = ":80"
[api]
  dashboard = true
  insecure = true

This configuration file sets up Traefik to listen on port 80, and enables the Traefik dashboard API.

Step 3: Create a Docker Compose File

Create a Docker Compose file to start Traefik using the following command:

sudo touch /etc/docker-compose.yml

Then, open the file using your text editor and add the following configuration:

version: "3"

services:
  traefik:
    image: traefik
    command: --docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /etc/traefik.toml:/traefik.toml

This Docker Compose file sets up a Traefik container that listens on port 80 and the Traefik dashboard API on port 8080. It also mounts the Docker socket and the Traefik configuration file as volumes.

Step 4: Start Traefik

Start the Traefik container by running the following command:

sudo docker-compose up -d

This command will start the Traefik container and run it in the background. You can verify that the container is running by running the following command:

sudo docker ps

You should see a Traefik container listed with the name traefik.

Step 5: Configure Your Applications to Use Traefik

Now, you can configure your applications to use Traefik as a reverse proxy. Simply set up a Docker Compose file or Docker run command for each application, and specify the appropriate Traefik labels in the container configuration.

For example, to set up a WordPress application, you could use the following Docker Compose file:

version: "3"

services:
  wordpress:
    image: wordpress
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - ./wp-content:/var/www/html/wp-content
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.entrypoints=http"
      - "traefik.http.routers.wordpress.rule=Host(`example.com`)"

This Docker Compose file sets up a WordPress container and specifies the Traefik labels that tell Traefik to route traffic to this container.

With Traefik, you can easily configure multiple applications to run on the same server and expose them to the internet with ease.

Conclusion

In this tutorial, you learned how to install Traefik on NixOS Latest and configure it as a reverse proxy for your applications. With Traefik, you can easily manage multiple applications and route traffic to them with ease. Happy deploying!

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!

Alternatively, for the best virtual desktop, try Shells!