refactor both file server modules

This commit is contained in:
Yan Lin 2026-02-13 07:33:30 +01:00
parent d882bbe2f2
commit dbe79f5a89
3 changed files with 53 additions and 66 deletions

View file

@ -9,17 +9,22 @@ let
in
{
options.services.dufs = {
sharedPath = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Path to the folder to share via WebDAV. Set to null to disable dufs.";
example = "/mnt/storage/shared";
};
port = lib.mkOption {
type = lib.types.port;
default = 5099;
description = "Port to listen on";
shares = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options = {
path = lib.mkOption {
type = lib.types.str;
description = "Path to the folder to share via WebDAV";
};
port = lib.mkOption {
type = lib.types.port;
default = 5099;
description = "Port to listen on";
};
};
});
default = {};
description = "WebDAV shares to expose via dufs. Each entry creates a separate dufs instance.";
};
user = lib.mkOption {
@ -35,27 +40,25 @@ in
};
};
config = lib.mkIf (cfg.sharedPath != null) {
# Install dufs package
config = lib.mkIf (cfg.shares != {}) {
environment.systemPackages = [ pkgs.dufs ];
systemd.services.dufs = {
description = "Dufs WebDAV File Server";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
systemd.services = lib.mapAttrs' (name: s:
lib.nameValuePair "dufs-${name}" {
description = "Dufs WebDAV File Server - ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
UMask = "0022";
ExecStart = ''/bin/sh -c "${pkgs.dufs}/bin/dufs ${cfg.sharedPath} --port ${toString cfg.port} --bind 0.0.0.0 --allow-all --auth $(cat ${authFile})@/:rw"'';
Restart = "on-failure";
RestartSec = "10s";
};
};
# Open firewall port (optional, since traffic comes through WireGuard)
# networking.firewall.allowedTCPPorts = [ cfg.port ];
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
UMask = "0022";
ExecStart = ''/bin/sh -c "${pkgs.dufs}/bin/dufs ${s.path} --port ${toString s.port} --bind 0.0.0.0 --allow-all --auth $(cat ${authFile})@/:rw"'';
Restart = "on-failure";
RestartSec = "10s";
};
}
) cfg.shares;
};
}