In the previous article, we installed a minimal NixOS for servers. This article will build Nextcloud on it.
I got struggled when deploying the Nextcloud because of the lack of documentation and the problem of sending emails. So I even write a better Nextcloud NixOS Wiki. After finishing the deployment, I will try to submit it to replace the current wiki.
Why Nextcloud
Nextcloud is a powerful open-source cloud storage service that can be used to build a private cloud. It has many features, such as file storage, calendar, notes, chat, etc. Although PHP that Nextcloud uses is outdated, it is still the best choice because of its powerful features.
Besides Nextcloud, I also tried these open-source cloud storage services:
- pydio: It is as powerful as Nextcloud, but it doesn't have a Nix package and isn't easy to install on NixOS.
- Seafile: A powerful alternative, it has a Nix package but lacks documentation, so it isn't easy to deploy.
- ownCloud: The predecessor of the Nextcloud. It is not as powerful as Nextcloud and is not stable.
- cloudreve: Not powerful. It can only store files and doesn't have a Nix package.
Basic steps to deploy Nextcloud
Install Nextcloud package
It is very easy to install Nextcloud on NixOS. Just modify the /etc/nixos/configuration.nix file, as add nextcloud to environment.systemPackages.
environment.systemPackages = with pkgs; [
nextcloud26
];
note: The name of the Nextcloud package is
nextcloud+version, notnextcloud.
Do not run nixos-rebuild switch immediately. Nextcloud is not configured yet.
First Configuration
NixOS manual gave an example to config Nextcloud.
{
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"];
};di vi
networking.firewall.allowedTCPPorts = [ 80 443 ];
}
This means:
- Enable Nextcloud service.
- Specify the domain name as
nextcloud.tld. - Specify the database as PostgreSQL and give the specific configuration of the database.
- Specify the startup order of the service.
- Open 80 and 443 ports.
- Specify the password file of the Nextcloud administrator as
/path/to/admin-pass-file.
/path/to/admin-pass-file is not a real path but a placeholder. You should specify it yourself. According to the requirement of NixOS, this file and its parent directory must be accessible by the nextcloud user. Therefore, we need to put this file in the /nextcloud directory and set the permission of the /nextcloud directory to 777.
I change /path/to/admin-pass-file into /nextcloud/nextcloud-admin-pass, and then
# execute as root
cd /
mkdir nextcloud
chown nextcloud nextcloud/
chmod 777 nextcloud/
cd nextcloud
vim nextcloud-admin-pass
# write in the password
nixos-rebuild switch
Because of working in a local test environment, I temporarily change nextcloud.tld to nextcloud.nixos (because my LAN has a self-built DNS server. If you don't have one, it is recommended to change it to its IP address).
Finishing the rebuild, visit http://nextcloud.nixos/. You can see the home page of Nextcloud.
The home page of different versions of Nextcloud may differ, but it is similar.
Log in with the username and password specified in service.nextcloud.config.adminuser and /nextcloud/nextcloud-admin-pass, and you can complete the installation.
Install app for Nextcloud in nix configuration file (simple)
The Nextcloud deployed by the above configuration is installed with a few apps. But Nextcloud is powerful and can be greatly expanded by installing apps. In the Nextcloud app store, you can find and install many apps, but in NixOS, it is a better choice to install apps using the Nix package manager.
NixWiki provides some examples about installing apps.
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 apps. You cannot find their name in the Nextcloud app store, but you can find their name in the Some apps section of NixWiki.
When writing this article. Some apps contains:
[
"bookmarks",
"calendar",
"contacts",
"deck",
"keeweb",
"mail",
"news",
"notes",
"onlyoffice",
"polls",
"tasks",
"twofactor_webauthn"
]
Obviously, these apps are not enough, but there are other ways to install apps.
Install Apps from Nextcloud App Store with nix configuration file
NixWiki also provides an example of installing apps from the Nextcloud app store.
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 apps. We can find the download links of the apps in the Nexcloud app store, and then add them to the configuration file.
Note: You should update them manually if you install apps in this way. When Nextcloud updates, you need to fill in the new download links manually. (Of course, you can also update apps in the Nextcloud app store)
I install:
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 is different from some documents, this also works.
Ensure the name, such as camerarawpreviews, is the same as the name of the tar.gz package. Otherwise, it will not be installed.
If your configuration is correct, there should be no errors in the management settings->overview.
FAQ
Where does 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.
Where is the log?
NixOs config the log type as this by default:
'log_type' => 'syslog',
'loglevel' => '2',
In this case, Nextcloud's log will be output to the daemon's log, which can be viewed with journalctl -t Nextcloud.
What's fxxking interesting is that 'loglevel' => '2' seems to be an invalid configuration. Warnings, Errors, and even Fatal in Nextcloud are treated as Debug output. So you may need to change 'loglevel' => '2' to 'loglevel' => '0'.
services.nextcloud = {
[...]
logLevel = 0;
};
You can add the parameter -f to view the log in real-time.
Where is the app store?
If you install apps from the Nextcloud app store with Nix configuration, app store will be disabled by default.
You can enable it by setting appstoreEnable to true.
services.nextcloud = {
[...]
appstoreEnable = true;
};
Mail Delivery
Because of the design defect, it is impossible to configure mail delivery directly through the Nix file, and extraConfig must be used.
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 is referred to config.php. You can find it from an init Nextcloud instance.
It seems that there is no way to send an email from a local testing server, no matter what settings are used. But it works on cloud servers (such as AWS). This problem seems to be caused by the local network environment, and I have not found a solution.
Configuration file example
Here is an IPFS link for the configuration file.
Note that some settings use placeholders instead, so don't use this configuration file directly.
The key config is:
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_dmoain = "example.com";
mail_smtphost = "smtp.example.com";
mail_smtpport = "465";
mail_smtpayth = 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 have not encountered any maintenance problems because I just built it recently. However, if there is a problem, I will update it here.
For big problems, I may write a separate article, put a link to this article in the article that solves the problem, and put a link to the solution in this article.
If you want to receive updates in time, you can follow my blog on the blockchain.