1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.webdav;
9 format = pkgs.formats.yaml { };
10in
11{
12 options = {
13 services.webdav = {
14 enable = lib.mkEnableOption "WebDAV server";
15
16 package = lib.mkPackageOption pkgs "webdav" { };
17
18 user = lib.mkOption {
19 type = lib.types.str;
20 default = "webdav";
21 description = "User account under which WebDAV runs.";
22 };
23
24 group = lib.mkOption {
25 type = lib.types.str;
26 default = "webdav";
27 description = "Group under which WebDAV runs.";
28 };
29
30 settings = lib.mkOption {
31 type = format.type;
32 default = { };
33 description = ''
34 Attrset that is converted and passed as config file. Available options
35 can be found at
36 [here](https://github.com/hacdias/webdav).
37
38 This program supports reading username and password configuration
39 from environment variables, so it's strongly recommended to store
40 username and password in a separate
41 [EnvironmentFile](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=).
42 This prevents adding secrets to the world-readable Nix store.
43 '';
44 example = lib.literalExpression ''
45 {
46 address = "0.0.0.0";
47 port = 8080;
48 scope = "/srv/public";
49 modify = true;
50 auth = true;
51 users = [
52 {
53 username = "{env}ENV_USERNAME";
54 password = "{env}ENV_PASSWORD";
55 }
56 ];
57 }
58 '';
59 };
60
61 configFile = lib.mkOption {
62 type = lib.types.path;
63 default = format.generate "webdav.yaml" cfg.settings;
64 defaultText = "Config file generated from services.webdav.settings";
65 description = ''
66 Path to config file. If this option is set, it will override any
67 configuration done in options.services.webdav.settings.
68 '';
69 example = "/etc/webdav/config.yaml";
70 };
71
72 environmentFile = lib.mkOption {
73 type = lib.types.nullOr lib.types.path;
74 default = null;
75 description = ''
76 Environment file as defined in {manpage}`systemd.exec(5)`.
77 '';
78 };
79 };
80 };
81
82 config = lib.mkIf cfg.enable {
83 users.users = lib.mkIf (cfg.user == "webdav") {
84 webdav = {
85 description = "WebDAV daemon user";
86 group = cfg.group;
87 uid = config.ids.uids.webdav;
88 };
89 };
90
91 users.groups = lib.mkIf (cfg.group == "webdav") {
92 webdav.gid = config.ids.gids.webdav;
93 };
94
95 systemd.services.webdav = {
96 description = "WebDAV server";
97 after = [ "network.target" ];
98 wantedBy = [ "multi-user.target" ];
99 serviceConfig = {
100 ExecStart = "${lib.getExe cfg.package} -c ${cfg.configFile}";
101 Restart = "on-failure";
102 User = cfg.user;
103 Group = cfg.group;
104 EnvironmentFile = lib.mkIf (cfg.environmentFile != null) [ cfg.environmentFile ];
105 };
106 };
107 };
108
109 meta.maintainers = with lib.maintainers; [ pmy ];
110}