1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 inherit (lib.attrsets) optionalAttrs;
10 inherit (lib.generators) toINIWithGlobalSection;
11 inherit (lib.lists) optional;
12 inherit (lib.modules) mkIf mkRemovedOptionModule;
13 inherit (lib.options) literalExpression mkEnableOption mkOption;
14 inherit (lib.strings) escape;
15 inherit (lib.types)
16 attrsOf
17 bool
18 int
19 lines
20 oneOf
21 str
22 submodule
23 ;
24
25 cfg = config.services.davfs2;
26
27 escapeString = escape [
28 "\""
29 "\\"
30 ];
31
32 formatValue =
33 value:
34 if true == value then
35 "1"
36 else if false == value then
37 "0"
38 else if builtins.isString value then
39 "\"${escapeString value}\""
40 else
41 toString value;
42
43 configFile = pkgs.writeText "davfs2.conf" (
44 toINIWithGlobalSection {
45 mkSectionName = escapeString;
46 mkKeyValue = k: v: "${k} ${formatValue v}";
47 } cfg.settings
48 );
49in
50{
51
52 imports = [
53 (mkRemovedOptionModule [ "services" "davfs2" "extraConfig" ] ''
54 The option extraConfig got removed, please migrate to
55 services.davfs2.settings instead.
56 '')
57 ];
58
59 options.services.davfs2 = {
60 enable = mkEnableOption "davfs2";
61
62 davUser = mkOption {
63 type = str;
64 default = "davfs2";
65 description = ''
66 When invoked by root the mount.davfs daemon will run as this user.
67 Value must be given as name, not as numerical id.
68 '';
69 };
70
71 davGroup = mkOption {
72 type = str;
73 default = "davfs2";
74 description = ''
75 The group of the running mount.davfs daemon. Ordinary users must be
76 member of this group in order to mount a davfs2 file system. Value must
77 be given as name, not as numerical id.
78 '';
79 };
80
81 settings = mkOption {
82 type = submodule {
83 freeformType =
84 let
85 valueTypes = [
86 bool
87 int
88 str
89 ];
90 in
91 attrsOf (attrsOf (oneOf (valueTypes ++ [ (attrsOf (oneOf valueTypes)) ])));
92 };
93 default = { };
94 example = literalExpression ''
95 {
96 globalSection = {
97 proxy = "foo.bar:8080";
98 use_locks = false;
99 };
100 sections = {
101 "/media/dav" = {
102 use_locks = true;
103 };
104 "/home/otto/mywebspace" = {
105 gui_optimize = true;
106 };
107 };
108 }
109 '';
110 description = ''
111 Extra settings appended to the configuration of davfs2.
112 See {manpage}`davfs2.conf(5)` for available settings.
113 '';
114 };
115 };
116
117 config = mkIf cfg.enable {
118
119 environment.systemPackages = [ pkgs.davfs2 ];
120 environment.etc."davfs2/davfs2.conf".source = configFile;
121
122 services.davfs2.settings = {
123 globalSection = {
124 dav_user = cfg.davUser;
125 dav_group = cfg.davGroup;
126 };
127 };
128
129 users.groups = optionalAttrs (cfg.davGroup == "davfs2") {
130 davfs2.gid = config.ids.gids.davfs2;
131 };
132
133 users.users = optionalAttrs (cfg.davUser == "davfs2") {
134 davfs2 = {
135 createHome = false;
136 group = cfg.davGroup;
137 uid = config.ids.uids.davfs2;
138 description = "davfs2 user";
139 };
140 };
141
142 security.wrappers."mount.davfs" = {
143 program = "mount.davfs";
144 source = "${pkgs.davfs2}/bin/mount.davfs";
145 owner = "root";
146 group = cfg.davGroup;
147 setuid = true;
148 permissions = "u+rx,g+x";
149 };
150
151 security.wrappers."umount.davfs" = {
152 program = "umount.davfs";
153 source = "${pkgs.davfs2}/bin/umount.davfs";
154 owner = "root";
155 group = cfg.davGroup;
156 setuid = true;
157 permissions = "u+rx,g+x";
158 };
159
160 };
161
162}