Ricky Kresslein

Home Assistant On NixOS With Docker

2023-02-06

The Home Assistant Docker container requires some extra options for network access and USB access. The USB access is especially important if you have a hub of some sort plugged into the USB port of your server that connects to all of your devices. It took me a while to figure out how to set all of this up on NixOS, so hopefully this is helpful.

This is the contents of my /etc/nixos/containers/homeassistant.nix file.

{ config, pkgs, ... }:

{
  # Home Assistant
  virtualisation.oci-containers.containers."homeassistant" = {
    autoStart = true;
    image = "ghcr.io/home-assistant/home-assistant:stable";
    volumes = [
      "/home/ricky/HomeAssistant:/config"
      "/etc/localtime:/etc/localtime:ro"
    ];
    extraOptions = [
      "--device=/dev/ttyUSB0"
      "--network=host"
      "--privileged"
    ];
  };
}

Since that's in its own .nix file, we need to add it to the imports section of the /etc/nixos/configuration.nix, like so:

imports = [
  ./hardware-configuration.nix
  # Docker containers
  ./containers/homeassistant.nix
];

That's it! Run sudo nixos-rebuild switch and, once it downloads and starts, you should have a running Home Assistant instance on your NixOS box! Just got to http://[server_ip]:8123 to access the setup page.