about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/network-filesystems/davfs2.nix
diff options
context:
space:
mode:
Diffstat (limited to 'nixpkgs/nixos/modules/services/network-filesystems/davfs2.nix')
-rw-r--r--nixpkgs/nixos/modules/services/network-filesystems/davfs2.nix121
1 files changed, 99 insertions, 22 deletions
diff --git a/nixpkgs/nixos/modules/services/network-filesystems/davfs2.nix b/nixpkgs/nixos/modules/services/network-filesystems/davfs2.nix
index 8024cfba08be..23c04658031f 100644
--- a/nixpkgs/nixos/modules/services/network-filesystems/davfs2.nix
+++ b/nixpkgs/nixos/modules/services/network-filesystems/davfs2.nix
@@ -1,38 +1,52 @@
 { config, lib, pkgs, ... }:
 
-with lib;
-
 let
+  inherit (lib.attrsets) optionalAttrs;
+  inherit (lib.generators) toINIWithGlobalSection;
+  inherit (lib.lists) optional;
+  inherit (lib.modules) mkIf;
+  inherit (lib.options) literalExpression mkEnableOption mkOption;
+  inherit (lib.strings) escape;
+  inherit (lib.types) attrsOf bool int lines oneOf str submodule;
+
   cfg = config.services.davfs2;
-  cfgFile = pkgs.writeText "davfs2.conf" ''
-    dav_user ${cfg.davUser}
-    dav_group ${cfg.davGroup}
-    ${cfg.extraConfig}
-  '';
+
+  escapeString = escape ["\"" "\\"];
+
+  formatValue = value:
+    if true == value then "1"
+    else if false == value then "0"
+    else if builtins.isString value then "\"${escapeString value}\""
+    else toString value;
+
+  configFile = pkgs.writeText "davfs2.conf" (
+    if (cfg.settings != { }) then
+      (toINIWithGlobalSection {
+        mkSectionName = escapeString;
+        mkKeyValue = k: v: "${k} ${formatValue v}";
+      } cfg.settings)
+    else
+      cfg.extraConfig
+  );
 in
 {
+
   options.services.davfs2 = {
-    enable = mkOption {
-      type = types.bool;
-      default = false;
-      description = lib.mdDoc ''
-        Whether to enable davfs2.
-      '';
-    };
+    enable = mkEnableOption "davfs2";
 
     davUser = mkOption {
-      type = types.str;
+      type = str;
       default = "davfs2";
-      description = lib.mdDoc ''
+      description = ''
         When invoked by root the mount.davfs daemon will run as this user.
         Value must be given as name, not as numerical id.
       '';
     };
 
     davGroup = mkOption {
-      type = types.str;
+      type = str;
       default = "davfs2";
-      description = lib.mdDoc ''
+      description = ''
         The group of the running mount.davfs daemon. Ordinary users must be
         member of this group in order to mount a davfs2 file system. Value must
         be given as name, not as numerical id.
@@ -40,22 +54,85 @@ in
     };
 
     extraConfig = mkOption {
-      type = types.lines;
+      type = lines;
       default = "";
       example = ''
-        kernel_fs coda
         proxy foo.bar:8080
         use_locks 0
+
+        [/media/dav]
+        use_locks 1
+
+        [/home/otto/mywebspace]
+        gui_optimize 1
       '';
-      description = lib.mdDoc ''
+      description = ''
         Extra lines appended to the configuration of davfs2.
+        See {manpage}`davfs2.conf(5)` for available settings.
+
+        **Note**: Please pass structured settings via
+        {option}`settings` instead, this option
+        will get deprecated in the future.
+      ''  ;
+    };
+
+    settings = mkOption {
+      type = submodule {
+        freeformType = let
+          valueTypes = [ bool int str ];
+        in
+        attrsOf (attrsOf (oneOf (valueTypes ++ [ (attrsOf (oneOf valueTypes)) ] )));
+      };
+      default = { };
+      example = literalExpression ''
+        {
+          globalSection = {
+            proxy = "foo.bar:8080";
+            use_locks = false;
+          };
+          sections = {
+            "/media/dav" = {
+              use_locks = true;
+            };
+            "/home/otto/mywebspace" = {
+              gui_optimize = true;
+            };
+          };
+        }
+      '';
+      description = ''
+        Extra settings appended to the configuration of davfs2.
+        See {manpage}`davfs2.conf(5)` for available settings.
       ''  ;
     };
   };
 
   config = mkIf cfg.enable {
+
+    assertions = [
+      {
+        assertion = cfg.extraConfig != "" -> cfg.settings == { };
+        message = ''
+          services.davfs2.extraConfig and services.davfs2.settings cannot be used together.
+          Please prefer using services.davfs2.settings.
+        '';
+      }
+    ];
+
+    warnings = optional (cfg.extraConfig != "") ''
+      services.davfs2.extraConfig will be deprecated in future releases;
+      please use services.davfs2.settings instead.
+    '';
+
     environment.systemPackages = [ pkgs.davfs2 ];
-    environment.etc."davfs2/davfs2.conf".source = cfgFile;
+    environment.etc."davfs2/davfs2.conf".source = configFile;
+
+    services.davfs2.settings = {
+      globalSection = {
+        dav_user = cfg.davUser;
+        dav_group = cfg.davGroup;
+      };
+    };
 
     users.groups = optionalAttrs (cfg.davGroup == "davfs2") {
       davfs2.gid = config.ids.gids.davfs2;