Nextcloud with DataDir on network share

Hi!

I’m new to nixos and after setting up my desktop and laptop, I want to migrate my nextcloud server to nixos. I want the datadir on a network share. The nextcloud module is complaining about permissions. How must I mount the network share, so that the nextcloud module has the rights to use this share?

Here is the error:

warning: the following units failed: nextcloud-setup.service

× nextcloud-setup.service
     Loaded: loaded (/etc/systemd/system/nextcloud-setup.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Thu 2024-05-16 14:31:19 CEST; 297ms ago
    Process: 2773 ExecStart=/nix/store/9r0alp08jsmk8sbcb6knpv7cbb09v36z-unit-script-nextcloud-setup-start/bin/nextcloud-setup-start (code=exited, status=1/FAILURE)
   Main PID: 2773 (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 191ms

Mai 16 14:31:19 marsellus systemd[1]: Starting nextcloud-setup.service...
Mai 16 14:31:19 marsellus nextcloud-setup-start[2900]: Nextcloud is not installed - only a limited number of commands are available
Mai 16 14:31:19 marsellus nextcloud-setup-start[2900]:                                      
Mai 16 14:31:19 marsellus nextcloud-setup-start[2900]:   Command "upgrade" is not defined.
Mai 16 14:31:19 marsellus nextcloud-setup-start[2900]:                                      
Mai 16 14:31:19 marsellus systemd[1]: nextcloud-setup.service: Main process exited, code=exited, status=1/FAILURE
Mai 16 14:31:19 marsellus systemd[1]: nextcloud-setup.service: Failed with result 'exit-code'.
Mai 16 14:31:19 marsellus systemd[1]: Failed to start nextcloud-setup.service.

And the same config works if pointed to a local DataDir?

Yes. When using default settings everything works.

Here’s the relevant nextcloud part:

services.nextcloud = {
  enable = true;
  package = pkgs.nextcloud28;
  ...
  datadir = "/nextcloud";

That’s the mount part:

fileSystems."/nextcloud" = {
    device = "//192.168.1.3/Daten/nextcloud";
    fsType = "cifs";
    options = let
      # this line prevents hanging on network split
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";

    in ["${automount_opts},credentials=/home/specter/smb-secrets,uid=nextcloud,gid=nextcloud,file_mode=0777,dir_mode=0777,mfsymlinks"];
  };

OK, same happens when pointing the datadir to local folder. Createt folder /test and chown nextcloud:nextcloud. Pointing the datadir there resulted in same error:

warning: the following units failed: nextcloud-setup.service

× nextcloud-setup.service
     Loaded: loaded (/etc/systemd/system/nextcloud-setup.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Thu 2024-05-16 15:36:55 CEST; 531ms ago
    Process: 2789 ExecStart=/nix/store/hlxghwk8099kyadlqfh36jq6y43bagdv-unit-script-nextcloud-setup-start/bin/nextcloud-setup-start (code=exited, status=1/FAILURE)
   Main PID: 2789 (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 202ms

Mai 16 15:36:55 marsellus systemd[1]: Starting nextcloud-setup.service...
Mai 16 15:36:55 marsellus nextcloud-setup-start[2810]: Nextcloud is not installed - only a limited number of commands are available
Mai 16 15:36:55 marsellus nextcloud-setup-start[2810]:                                      
Mai 16 15:36:55 marsellus nextcloud-setup-start[2810]:   Command "upgrade" is not defined.
Mai 16 15:36:55 marsellus nextcloud-setup-start[2810]:                                      
Mai 16 15:36:55 marsellus systemd[1]: nextcloud-setup.service: Main process exited, code=exited, status=1/FAILURE
Mai 16 15:36:55 marsellus systemd[1]: nextcloud-setup.service: Failed with result 'exit-code'.
Mai 16 15:36:55 marsellus systemd[1]: Failed to start nextcloud-setup.service.

OK, I’m giving up on that. The nixos nextcloud module seem to not work with datadir on mounted network share or even outside /var/lib/nextcloud.

So, is it possible to have the container method stored on a network share?

I’d imagine that’s a bug in the nextcloud setup script. It looks like the nextcloud files aren’t placed inside the dataDir - this could be because it was partially configured and some random file (or database entry) makes the script skip some setup.

Have you tried copying the contents of /var/lib/nextcloud to the target directory first?

I also wonder if the problem is that nextcloud starts before the filesystem is mounted. You might need to add a dependency on the mount in the nextcloud module.

Another problem you’ll probably run into is that the NixOS module creates symlinks in the dataDir that point to stuff in the nix store. You’ll know this better than me, but I imagine that those symlinks will not work through an smb share. You might need to host specific subdirectories of the dataDir on your share instead, so that the configuration, plugins and such are hosted locally (since you use this network share mainly for backup purposes, that data is superfluous anyway, since it can be perfectly recovered from your NixOS config).

I deleted /var/lib/nextcloud, /var/lib/mysql, deleted all generations and gc store. Same output.

Have you tried copying the contents of /var/lib/nextcloud to the target directory first?

No, I missed that. I will keep this in mind

I also wonder if the problem is that nextcloud starts before the filesystem is mounted. You might need to add a dependency on the mount in the nextcloud module.

The nextcloud module already created the folders inside the network share, so I don’t think it’s a dependency problem.

Another problem you’ll probably run into is that the NixOS module creates symlinks in the dataDir that point to stuff in the nix store. You’ll know this better than me, but I imagine that those symlinks will not work through an smb share. You might need to host specific subdirectories of the dataDir on your share instead, so that the configuration, plugins and such are hosted locally (since you use this network share mainly for backup purposes, that data is superfluous anyway, since it can be perfectly recovered from your NixOS config).

I’m really not an expert in this, but I saw that inside the smb share where symlinks created. This is my samba mount:

environment.systemPackages = [ pkgs.cifs-utils ];
fileSystems.“/mnt/nextcloud” = {
device = “//192.168.1.3/nextcloud”;
fsType = “cifs”;
options = let
# this line prevents hanging on network split
automount_opts = “x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s”;
in [“${automount_opts},credentials=/home/nextcloud/smb-secrets,uid=nextcloud,gid=nextcloud,file_mode=0644,dir_mode=0644,mfsymlinks”];
};

Right now I’m going the nixos nextcloud module route with stock settings and setting up nextcloud external samba storages for each user. I really would like a more declarative setup for some of my most important services like nextcloud.

Server died → nixos-rebuild switch --flake ‘…#server’ → done!

Hi @specter - I’m in a similar situation

I have a VPS running NixOS with a cheap storage mounted on it through SSHFS, both are rented from Hetzner. Then I have a NixOS container on the VPS which runs Nextcloud. It’s configured to bind-mount a directory on the network drive for the Nextcloud home and it doesn’t work correctly. If I instead bindmount a directory on the local storage then everything works fine.

The config for mounting the network drive is here, I’m using the allow_other option to make sure it’s not an ownership issue. I can even go in with an unprivileged user and make a new directory and files.

The config for bind-mounting in the container is here, I also have some lines below to set ownership on the directories. The path to the network mount is commented out now as that doesn’t work and it’s now pointing to a local path instead.

When I try to use a network mount I see an error message that says Configuration was not read or initialized correctly, not overwriting config.php or something similar. I saw the same error message when I earlier attempted to bindmount /root/nextcloud/home instead of /persistent/nextcloud/home as I do now. That’s makes me think that this might be something permissions related, but I don’t really know how that would be.

Update: It magically works if I bind mount only the data directory on the network drive.

@specter I made a thread about my config here. I think it’s similar to what you wanted to achieve, might give you some inspiration.

1 Like

As I wrote, I had given up and went the standard way. Thank you very much for the inspiration and I will give it a try. I had also tried the container way, but I wanted to swap the whole volume to the unraid storage. Unfortunately, that didn’t work for me either. I have the suspicion that it has something to do with the way unraid makes the shares available. A permission problem. Thanks for the inspiration!