In the previous article, we installed NixOS suitable for servers. This article will build Nextcloud on this basis.
The blogger encountered significant difficulties while setting up Nextcloud, mainly due to the inability to send emails and the sparse official documentation of NixOS. So I even wrote a better Nextcloud NixOS Wiki. After tinkering with Nextcloud + NixOS, I will try to submit it to replace the current Wiki.
In fact, you do not need to follow the blog step by step; you can directly download my configured configuration.nix
and modify it according to your needs.
Why Nextcloud#
Nextcloud is an open-source cloud storage service that can be used to build a private cloud. Its functionality is very powerful and can be used for file storage, calendars, notes, chat, and more. Although Nextcloud uses outdated PHP, its features are so robust that there are no good alternatives, so Nextcloud was chosen.
In addition to Nextcloud, the blogger also tried these open-source cloud storage services:
- pydio: Similarly powerful, but lacks Nix packages, making installation on NixOS more complicated.
- Seafile: A sufficiently powerful alternative, has Nix packages, but lacks documentation and is difficult to deploy.
- ownCloud: The predecessor of Nextcloud, not powerful enough, and updates are unstable.
- cloudreve: Not powerful enough, can only store files, and lacks Nix packages.
Basic Process of Setting Up Nextcloud Locally#
Installing Nextcloud Package#
Installing Nextcloud is very simple; just modify the /etc/nixos/configuration.nix
file and add nextcloud
to environment.systemPackages
.
environment.systemPackages = with pkgs; [
nextcloud26
];
Note: The Nix package for Nextcloud is
nextcloud
+ version number, not justnextcloud
.
Don't rush to run nixos-rebuild switch
yet; Nextcloud has not been configured.
Initial Configuration#
The NixOS manual provides the following configuration:
{
services.nextcloud = {
enable = true;
hostName = "nextcloud.tld";
config = {
dbtype = "pgsql";
dbuser = "nextcloud";
dbhost = "/run/postgresql"; # nextcloud will add /.s.PGSQL.5432 by itself
dbname = "nextcloud";
adminpassFile = "/path/to/admin-pass-file";
adminuser = "root";
};
};
services.postgresql = {
enable = true;
ensureDatabases = [ "nextcloud" ];
ensureUsers = [
{ name = "nextcloud";
ensurePermissions."DATABASE nextcloud" = "ALL PRIVILEGES";
}
];
};
# ensure that postgres is running *before* running the setup
systemd.services."nextcloud-setup" = {
requires = ["postgresql.service"];
after = ["postgresql.service"];
};
networking.firewall.allowedTCPPorts = [ 80 443 ];
}
The meaning of this configuration is:
- Enable Nextcloud service
- Specify the domain name as
nextcloud.tld
- Specify the database as PostgreSQL and provide specific database configurations
- Specify the startup order of the service
- Open ports 80 and 443
- Specify the password file for the Nextcloud administrator as
/path/to/admin-pass-file
/path/to/admin-pass-file
is not an actual path but a placeholder that we need to specify ourselves. According to NixOS requirements, this file must be accessible by the nextcloud
user. However, after testing, the blogger found that the directory containing this file also needs to be accessible by the nextcloud
user. Therefore, we need to place this file in the /nextcloud
directory and set the permissions of the /nextcloud
directory to 777
.
Change /path/to/admin-pass-file
to /nextcloud/nextcloud-admin-pass
, then
# Run as root
cd /
mkdir nextcloud
chown nextcloud nextcloud/
chmod 777 nextcloud/
cd nextcloud
vim nextcloud-admin-pass
# Write the password
nixos-rebuild switch
Since this is a local testing environment, temporarily change nextcloud.tld
to nextcloud.nixos
(because the blogger's local area network has a self-built DNS server; if you don't have one, it's recommended to change it to an address).
After the build is complete, visiting http://nextcloud.nixos/
will show the Nextcloud installation interface similar to the image below.
The installation interface for different versions of Nextcloud may vary, but they are generally similar.
Enter the username specified in service.nextcloud.config.adminuser
and the password specified in /nextcloud/nextcloud-admin-pass
to complete the installation.
Installing Nextcloud Applications in Nix Configuration File (Simple Method)#
The Nextcloud deployed with the above configuration only installs a few applications, but Nextcloud is a powerful platform that can greatly expand its functionality by installing applications. Many applications can be found and installed in the Nextcloud app store, but using the Nix package manager to install applications in NixOS is a better choice.
The NixWiki provides examples of installing applications, such as
services.nextcloud = {
enable = true;
[...]
package = pkgs.nextcloud26;
extraApps = with pkgs.nextcloud26Packages.apps; {
inherit mail news contacts;
};
extraAppsEnable = true;
};
This configuration installs the mail, news, and contacts applications. However, these application names cannot be found in the Nextcloud app store, but we can find these application names in the Some apps
section of NixWiki.
At the time of this article's publication, the names of these applications are as follows:
[
"bookmarks",
"calendar",
"contacts",
"deck",
"keeweb",
"mail",
"news",
"notes",
"onlyoffice",
"polls",
"tasks",
"twofactor_webauthn"
]
Clearly, these applications are not enough, but there are other methods to install applications.
Installing Applications from Nextcloud App Store in Nix Configuration File#
NixWiki also provides a method to install applications through the Nextcloud app store in the nix configuration file, such as
services.nextcloud = {
enable = true;
[...]
extraApps = {
mail = pkgs.fetchNextcloudApp rec {
url = "https://github.com/nextcloud-releases/mail/releases/download/v1.14.1/mail-v1.14.1.tar.gz";
sha256 = "sha256-sQUsYC3cco6fj9pF2l1NrCEhA3KJoOvJRhXvBlVpNqo=";
};
contacts = pkgs.fetchNextcloudApp rec {
url = "https://github.com/nextcloud-releases/contacts/releases/download/v4.2.2/contacts-v4.2.2.tar.gz";
sha256 = "sha256-eTc51pkg3OdHJB7X4/hD39Ce+9vKzw1nlJ7BhPOzdy0=";
};
};
extraAppsEnable = true;
};
This configuration installs the mail and contacts applications. We can find the download links for applications in the Nextcloud app store and then add them to the configuration file.
Note: Applications installed this way need to be updated manually. When a new version is released, you need to manually fill in the new download link again. (Of course, you can also update through the app store within Nextcloud)
The blogger installed these applications
services.nextcloud = {
enable = true;
package = pkgs.nextcloud26;
[...]
extraApps = {
contacts = pkgs.nextcloud26Packages.apps.contacts;
mail = pkgs.nextcloud26Packages.apps.mail;
calendar = pkgs.fetchNextcloudApp rec {
url = "https://github.com/nextcloud-releases/calendar/releases/download/v4.3.4/calendar-v4.3.4.tar.gz";
sha256 = "0pj1h86kdnckzfrn13hllgps4wa921z4s24pg5d2666fqx89rwrv";
};
notes = pkgs.fetchNextcloudApp rec {
url = "https://github.com/nextcloud-releases/notes/releases/download/v4.7.2/notes.tar.gz";
sha256 = "0klqf8dixrrb8yp8cc60ggnvhmqb3yh9f6y1281jn8ia5jml622v";
};
camerarawpreviews = pkgs.fetchNextcloudApp rec {
url = "https://github.com/ariselseng/camerarawpreviews/releases/download/v0.8.1/camerarawpreviews_nextcloud.tar.gz";
sha256 = "1n1395m81m81klxzxd03ww07m0xjp0blbmx23y457k62j3kkr0m2";
};
drawio = pkgs.fetchNextcloudApp rec {
url = "https://github.com/jgraph/drawio-nextcloud/releases/download/v2.1.1/drawio-v2.1.1.tar.gz";
sha256 = "0frizrgkbmc3mhhap7cq45z43l4whzkszx7v0v0q2ylmq8sbxszm";
};
registration = pkgs.fetchNextcloudApp rec {
url = "https://github.com/nextcloud-releases/registration/releases/download/v2.1.0/registration-v2.1.0.tar.gz";
sha256 = "07dqc670qmdb3c8jjnj7azxxspjhiv6m9nrj960y3rjabyzy25m9";
};
music = pkgs.fetchNextcloudApp rec {
url = "https://github.com/owncloud/music/releases/download/v1.8.3/music_1.8.3_for_nextcloud.tar.gz";
sha256 = "1kajm5ppp63g42xdvkmv0glw7snsc2fi7pcra1sg4kd005ffz42d";
};
bookmarks = pkgs.fetchNextcloudApp rec {
url = "https://github.com/nextcloud/bookmarks/releases/download/v13.0.1/bookmarks-13.0.1.tar.gz";
sha256 = "0xx331bgly91y8ncxk36ha3ncrr2xlivblfi7rix6ffkrdx73yb9";
};
};
}
You can use the
nix-prefetch-url --unpack <url>
command to get the sha256 value of the tar.gz package.
Although the format differs from some documents, this method also works.
Note that names like camerarawpreviews
must match the names of the tar.gz packages; otherwise, installation will fail.
If your setup is correct, there should be no errors in the management settings -> overview.
Solutions to Common Problems#
Where did Nix install Nextcloud?#
According to the source code, the default installation path is /var/lib/nextcloud
, and the data directory is /var/lib/nextcloud/data
.
Can't find the logs#
By default, NixOS modifies the log output mode of Nextcloud to
'log_type' => 'syslog',
'loglevel' => '2',
In this case, Nextcloud's logs will be output to the daemon's logs, which can be viewed using journalctl -t Nextcloud
.
Interestingly, it seems that 'loglevel' => '2'
is an ineffective configuration. Warnings, errors, and even fatal errors within Nextcloud are treated as debug output. Therefore, it may be necessary to change 'loglevel' => '2'
to 'loglevel' => '0'
.
services.nextcloud = {
[...]
logLevel = 0;
};
If needed, you can add the -f
parameter to view the logs in real-time.
Where is the app store?#
By default, the Nextcloud installed by NixOS does not have an app store.
You can set appstoreEnable
to true
to enable the app store.
services.nextcloud = {
[...]
appstoreEnable = true;
};
Unable to send emails#
Due to design flaws, emails cannot be configured directly through the Nix file; you must use extraConfig
for configuration.
services.nextcloud = {
[...]
extraOptions = {
mail_smtpmode = "smtp";
smtpsecure = "ssl";
mail_sendmailmode = "smtp";
mail_from_address = "nextcloud";
mail_domain = "example.com";
mail_smtphost = "smtp.example.com";
mail_smtpport = "465";
mail_smtpauth = 1;
mail_smtpname = "[email protected]";
mail_smtppassword = "password";
};
};
The above configuration comes from the config.php
file, which you can find in a brand new Nextcloud instance.
When setting up a local testing server, it seems that no matter what settings are used, emails cannot be sent, but on cloud servers (like AWS), emails can be sent normally. This issue seems to be caused by the local network environment, and I have not found a solution.
Example Nix Configuration File#
Here is the IPFS link
Note that some settings use placeholders, so do not use this configuration file directly.
The main modifications are:
services.nextcloud = {
enable = true;
hostName = "nextcloud.nixos";
config = {
dbtype = "pgsql";
dbuser = "nextcloud";
dbhost = "/run/postgresql"; # nextcloud will add /.s.PGSQL.5432 by itself
dbname = "nextcloud";
adminpassFile = "/path/to/your/nextcloud/adminpass.txt"; # Replace with your own path
adminuser = "root";
};
package = pkgs.nextcloud26;
extraApps = {
contacts = pkgs.nextcloud26Packages.apps.contacts;
mail = pkgs.nextcloud26Packages.apps.mail;
calendar = pkgs.fetchNextcloudApp rec {
url = "https://github.com/nextcloud-releases/calendar/releases/download/v4.3.4/calendar-v4.3.4.tar.gz";
sha256 = "0pj1h86kdnckzfrn13hllgps4wa921z4s24pg5d2666fqx89rwrv";
};
# More configuration can be found in My IPFS share
};
extraOptions = {
# Replace below with your own mail server settings
mail_smtpmode = "smtp";
smtpsecure = "ssl";
mail_sendmailmode = "smtp";
mail_from_address = "system";
mail_domain = "example.com";
mail_smtphost = "smtp.example.com";
mail_smtpport = "465";
mail_smtpauth = 1;
mail_smtpname = "[email protected]";
mail_smtppassword = "password";
};
};
services.postgresql = {
enable = true;
ensureDatabases = [ "nextcloud" ];
ensureUsers = [
{ name = "nextcloud";
ensurePermissions."DATABASE nextcloud" = "ALL PRIVILEGES";
}
];
};
systemd.services."nextcloud-setup" = {
requires = ["postgresql.service"];
after = ["postgresql.service"];
};
networking.firewall.allowedTCPPorts = [ 80 443 465 587 ];
Maintenance#
I just set it up not long ago, so I haven't encountered any maintenance issues yet. If there are problems, I will update here.
For major issues, I may write a separate article, placing a link to this article in the problem-solving post and a link to the solution here.
If you want to receive updates in a timely manner, you can follow my blog on the blockchain.