Ricky Kresslein

Immich On NixOS With Docker

2023-10-28

There is no need for a long introduction here. This is the contents of my immich.nix file. The immich-net network is needed to link all of the containers. The "--gpus=all" bit is only needed if you want to use GPU acceleration. All of the data is saved on an external MergerFS pool, called /mnt/tank.

{ config, pkgs, ... }:

{
  
  systemd.services.init-filerun-network-and-files = {
    description = "Create the network bridge for Immich.";
    after = [ "network.target" ];
    wantedBy = [ "multi-user.target" ];
    
    serviceConfig.Type = "oneshot";
    script = let dockercli = "${config.virtualisation.docker.package}/bin/docker";
            in ''
              # immich-net network
              check=$(${dockercli} network ls | grep "immich-net" || true)
              if [ -z "$check" ]; then
                ${dockercli} network create immich-net
              else
                echo "immich-net already exists in docker"
              fi
            '';
  };
  
  # Immich
  virtualisation.oci-containers.containers = {
    immich = {
      autoStart = true;
      image = "ghcr.io/imagegenius/immich:latest";
      volumes = [
        "/mnt/tank/Containers/Immich/config:/config"
        "/mnt/tank/Containers/Immich/photos:/photos"
        "/mnt/tank/Containers/Immich/config/machine-learning:/config/machine-learning"
      ];
      ports = [ "2283:8080" ];
      environment = {
        PUID = "1000";
        PGID = "1000";
        TZ = "Europe/Berlin"; # Change this to your timezone
        DB_HOSTNAME = "postgres14";
        DB_USERNAME = "postgres";
        DB_PASSWORD = "postgres";
        DB_DATABASE_NAME = "immich";
        REDIS_HOSTNAME = "redis";
      };
      extraOptions = [ "--network=immich-net" "--gpus=all" ];
    };

    redis = {
      autoStart = true;
      image = "redis";
      ports = [ "6379:6379" ];
      extraOptions = [ "--network=immich-net" ];
    };

    postgres14 = {
      autoStart = true;
      image = "tensorchord/pgvecto-rs:pg14-v0.2.0";
      ports = [ "5432:5432" ];
      volumes = [
        "pgdata:/var/lib/postgresql/data"
      ];
      environment = {
        POSTGRES_USER = "postgres";
        POSTGRES_PASSWORD = "postgres";
        POSTGRES_DB = "immich";
      };
      extraOptions = [ "--network=immich-net" ];
    };
  };

}

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/immich.nix
];

That's it! Run sudo nixos-rebuild switch and, once it downloads and starts, you should have a running Immich instance on your NixOS box! Just go to http://[server_ip]:2283 (if you used the same ports as me) to access the setup page.

Update: According to subwoofage on Reddit, you may need to set boot.kernel.sysctl = { "vm.overcommit_memory" = 1; }; in your Nix config to fix a Redis error. I don't have an error, so I have not been able to confirm this.