Publié le: 2023-07-21

Home assistant on NixOS with docker and Bluetooth

Home assistant est un excellent logiciel de domotique et propose une distribution pour fonctionner sur divers appareils.

Je ne vais pas le présenter, car il y a divers sujets sur le net à ce sujet, mais je vais montrer comment l’exécuter sur NixOS de manière reproductible.

NixOS utilise nix pour avoir des environnements reproductibles et constructibles. C’est très puissant et, d’après mon expérience avec 10 ans sur Ansible et 7 ans de Terraform, je pense que c’est l’avenir de l’automatisation dans des environnements reproductibles.

Configuration initiale de NixOS

Nous pouvons tout configurer au sein d’un seul fichier, mais nous allons le diviser en plusieurs étapes pour comprendre ce que nous faisons.

Voici la configuration initiale (pour raspberry PI), /etc/nixos/configuration.nix:

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  nix.settings.auto-optimise-store = true;

  # Use the extlinux boot loader. (NixOS wants to enable GRUB by default)
  boot.loader.grub.enable = false;
  # Enables the generation of /boot/extlinux/extlinux.conf
  boot.loader.generic-extlinux-compatible.enable = true;

  networking.hostName = "nixoslab-rpi3"; # Define your hostname.
  # Pick only one of the below networking options.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.
  # networking.networkmanager.enable = true;  # Easiest to use and most distros use this by default.

  # Set your time zone.
  time.timeZone = "Europe/Paris";
  users.users.nixos= {
    isNormalUser  = true;
    home  = "/home/nixos";
    description  = "NixOS";
    extraGroups  = [ "wheel" ];
    openssh.authorizedKeys.keys  = [];
  };

  security.sudo.wheelNeedsPassword = false;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
     vim
     curl
     libraspberrypi
   ];

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  services.openssh.enable = true;

  documentation.nixos.enable = false;
  nix.gc.automatic = true;
  nix.gc.options = "--delete-older-than 30d";
  boot.cleanTmpDir = true;

  # Copy the NixOS configuration file and link it from the resulting system
  # (/run/current-system/configuration.nix). This is useful in case you
  # accidentally delete configuration.nix.
  system.copySystemConfiguration = true;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "23.05"; # Did you read the comment?

}

Avec cette configuration, nous configurons une version NixOS 23.05, activons OpenSSH et créons un utilisateur nommé nixos sans mot de passe et une clé ssh (à définir dans la liste).

Configuration du Bluetooth

Maintenant, nous allons activer les services bluetooth. Comme c’est Nix, c’est juste une déclaration d’une seule ligne !

Ajoutez ceci dans le fichier configuration.nix:

hardware.bluetooth.enable = true;

Configuration de Docker

Pour la configuration de docker, nous utiliserons Docker et sont intégration NixOS.

Ajoutez ceci dans le fichier configuration.nix:

virtualisation = {
  backend = "docker";
  containers.homeassistant = {
    volumes = [
      "home-assistant:/config"
      "/var/run/dbus:/run/dbus:ro"
    ];
    environment.TZ = "Europe/Paris";
    image = "ghcr.io/home-assistant/home-assistant:2023.7.3";
    extraOptions = [
      "--network=host"
    ];
  };
};

Cette configuration bootstrap un container home-assistant avec la version 2023.7.3, un volume nommé home-assistant qui persistera entre les recréations du container et sera sur le disque, et monte le socket dbus afin d’avoir le support bluetooth dans le container.

Configuration complète

Voici la configuration finale complète, il suffit de l’appliquer sur n’importe quelle installation NixOS et tada, vous obtenez un OS complet installant Home Assistant avec support du Bluetooth.

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  hardware.bluetooth.enable = true;
  nix.settings.auto-optimise-store = true;

  # Use the extlinux boot loader. (NixOS wants to enable GRUB by default)
  boot.loader.grub.enable = false;
  # Enables the generation of /boot/extlinux/extlinux.conf
  boot.loader.generic-extlinux-compatible.enable = true;

  networking.hostName = "nixoslab-rpi3"; # Define your hostname.
  # Pick only one of the below networking options.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.
  # networking.networkmanager.enable = true;  # Easiest to use and most distros use this by default.

  # Set your time zone.
  time.timeZone = "Europe/Paris";
  users.users.nixos= {
    isNormalUser  = true;
    home  = "/home/nixos";
    description  = "NixOS";
    extraGroups  = [ "wheel" ];
    openssh.authorizedKeys.keys  = [];
  };

  security.sudo.wheelNeedsPassword = false;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
     vim
     curl
     libraspberrypi
   ];

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  services.openssh.enable = true;

  virtualisation.oci-containers = {
    backend = "docker";
    containers.homeassistant = {
      volumes = [
        "home-assistant:/config"
        "/var/run/dbus:/run/dbus:ro"
      ];
      environment.TZ = "Europe/Paris";
      image = "ghcr.io/home-assistant/home-assistant:2023.7.3";
      extraOptions = [
        "--network=host"
      ];
    };
  };

  networking.firewall.enable = false;

  documentation.nixos.enable = false;
  nix.gc.automatic = true;
  nix.gc.options = "--delete-older-than 30d";
  boot.cleanTmpDir = true;

  # Copy the NixOS configuration file and link it from the resulting system
  # (/run/current-system/configuration.nix). This is useful in case you
  # accidentally delete configuration.nix.
  system.copySystemConfiguration = true;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "23.05"; # Did you read the comment?

}