Ricky Kresslein

Nextcloud On NixOS With Docker

2022-10-7

The Nextcloud Docker container requires a network, a database, and the Nextcloud container itself. It took me a while to figure out how to set all of this up on Docker, so hopefully this is helpful.

This is the contents of my nextcloud.nix file. All of the data is saved on an external (encrypted) SSD, called /media.

{ config, pkgs, ... }:

{
  # Network linker
  system.activationScripts.NextcloudNetwork =
    let
      backend = config.virtualisation.oci-containers.backend;
      backendBin = "${pkgs.${backend}}/bin/${backend}";
    in
    ''
      ${backendBin} network create nextcloud-net --subnet 172.20.0.0/16 || true
    '';

  # Database
  virtualisation.oci-containers.containers."nextcloud-db" = {
    autoStart = true;
    image = "mariadb:10.5";
    cmd = [ "--transaction-isolation=READ-COMMITTED" "--binlog-format=ROW" ];
    volumes = [
      "nextcloud-db:/var/lib/mysql"
    ];
    ports = [ "3306:3306" ];
    environment = {
      MYSQL_ROOT_PASSWORD = "nextcloud";
      MYSQL_PASSWORD = "nextcloud";
      MYSQL_DATABASE = "nextcloud";
      MYSQL_USER = "nextcloud";
    };
    extraOptions = [ "--network=nextcloud-net" ];
  };

  # NextCloud
  virtualisation.oci-containers.containers."nextcloud" = {
    image = "nextcloud";
    ports = [ "8080:80" ];
    environment = {
      MYSQL_PASSWORD = "nextcloud";
      MYSQL_DATABASE = "nextcloud";
      MYSQL_USER = "nextcloud";
      MYSQL_HOST = "nextcloud-db";
    };
    dependsOn = [ "nextcloud-db" ];
    volumes = [
      "/media/Containers/Nextcloud/data:/var/www/html"
    ];
    extraOptions = [ "--network=nextcloud-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/nextcloud.nix
];

That's it! Run sudo nixos-rebuild switch and, once it downloads and starts, you should have a running Nextcloud instance on your NixOS box!