How to install LiveKit on nixOS Latest

LiveKit is a powerful solution that provides easy-to-use and scalable video communication tools. In this tutorial, we will show you how to install LiveKit on nixOS Latest.

Prerequisites

  • A nixOS Latest distribution running on your device.

Installation Process

  1. First, you need to create a LiveKit user and a directory for it. Execute the following commands.
sudo useradd -m -s /bin/bash livekit
sudo mkdir /home/livekit/livekit && sudo chown livekit:livekit /home/livekit/livekit
  1. Next, download and install the LiveKit server. Type in the following command in your nixOS console.
sudo su - livekit
curl -o livekit-server -sSL https://github.com/livekit/livekit-server/releases/latest/download/livekit-server-linux-amd64.tar.gz | tar xzvf -
exit
  1. After installing the LiveKit server, you need to configure it. Use the following commands to create a configuration file for the server.
sudo nano /etc/nixos/livekit.nix
  1. Add the following configuration to the file and save it.
{
  environment.etc."livekit.yml".text = ''
    app:
      listen: 0.0.0.0:7880
      metrics_listen: 0.0.0.0:9090
      cors_allowed_origins:
        - 'https://example.com'

    // sessions are used to prevent unauthorized access to rooms
    session:
      secret: random-32byte-string
      ttl_sec: 3600

    // list of STUN servers to use for ICE candidates
    stun_servers:
      - stun:stun.1.google.com:19302

    // configuration for the TURN server
    turn_servers:
      - urls: ["turn:YOURIP:3478?transport=udp"] // e.g. turn:127.0.0.1:3478?transport=udp
        secret: some-secret-string
        ttl_sec: 86400 // default is 1800

    // custom broadcaster allows you to use any custom method of broadcasting a video
    broadcaster:
      bitrates:
        - 100000
        - 260000
        - 450000
        - 850000
        - 1600000
  '';
  systemd.services.livekit = {
    environment = {
      "PATH" = "${pkgs.coreutils}/bin:${pkgs.gnumake}/bin:${pkgs.gawk}/bin"; 
      "LIVEKIT_CONFIG_PATH" = "/etc/livekit.yml"; 
    };
    wantedBy = [ "multi-user.target" ];
    script = "${pkgs.livekit}/bin/livekit-server"; 
    user = "livekit"; 
  };
  systemd.tmpfiles.rules = [
    "d /run/livekit 0755 livekit livekit",
  ];
}
  1. Finally, you need to activate the LiveKit server by running the following command.
sudo nixos-rebuild switch

Conclusion

Congratulation! You have successfully installed LiveKit on nixOS Latest. You can now easily use its scalable and robust video communication tools.