|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + appName, |
| 6 | + package, |
| 7 | + modulePath, |
| 8 | + profilePath, |
| 9 | +}: |
| 10 | +with lib; |
| 11 | +let |
| 12 | + jsonFormat = pkgs.formats.json { }; |
| 13 | + |
| 14 | + # Process a handler entry, validating path vs uriTemplate |
| 15 | + processHandler = |
| 16 | + mimeTypeOrScheme: handler: |
| 17 | + let |
| 18 | + hasPath = handler.path or null != null; |
| 19 | + hasUriTemplate = handler.uriTemplate or null != null; |
| 20 | + hasName = handler.name or null != null; |
| 21 | + in |
| 22 | + { |
| 23 | + assertion = !(hasPath && hasUriTemplate); |
| 24 | + message = "'${mimeTypeOrScheme}': handler can't have both 'path' and 'uriTemplate' set."; |
| 25 | + result = |
| 26 | + (optionalAttrs hasName { inherit (handler) name; }) |
| 27 | + // (optionalAttrs hasPath { inherit (handler) path; }) |
| 28 | + // (optionalAttrs hasUriTemplate { inherit (handler) uriTemplate; }); |
| 29 | + }; |
| 30 | + |
| 31 | + # Process all handlers for a mime type or scheme |
| 32 | + processHandlers = |
| 33 | + name: value: |
| 34 | + let |
| 35 | + hasHandlers = value.handlers != [ ]; |
| 36 | + processedHandlers = map (processHandler name) value.handlers; |
| 37 | + handlerAssertions = map (h: { inherit (h) assertion message; }) processedHandlers; |
| 38 | + handlerResults = map (h: h.result) processedHandlers; |
| 39 | + in |
| 40 | + { |
| 41 | + assertions = handlerAssertions ++ [ |
| 42 | + { |
| 43 | + assertion = !hasHandlers || value.action == 2; |
| 44 | + message = "'${name}': handlers can only be set when 'action' is set to 2 (Use helper app)."; |
| 45 | + } |
| 46 | + ]; |
| 47 | + result = { |
| 48 | + inherit (value) action ask; |
| 49 | + } |
| 50 | + // optionalAttrs hasHandlers { handlers = handlerResults; }; |
| 51 | + }; |
| 52 | + |
| 53 | + # Process mime types, including extensions field |
| 54 | + processMimeType = |
| 55 | + name: value: |
| 56 | + let |
| 57 | + processed = processHandlers name value; |
| 58 | + in |
| 59 | + { |
| 60 | + inherit (processed) assertions; |
| 61 | + result = processed.result // { |
| 62 | + inherit (value) extensions; |
| 63 | + }; |
| 64 | + }; |
| 65 | + |
| 66 | + # Process all mime types |
| 67 | + processedMimeTypes = mapAttrs processMimeType (config.mimeTypes or { }); |
| 68 | + mimeTypeAssertions = flatten (mapAttrsToList (_: v: v.assertions) processedMimeTypes); |
| 69 | + mimeTypeResults = mapAttrs (_: v: v.result) processedMimeTypes; |
| 70 | + |
| 71 | + # Process all schemes |
| 72 | + processedSchemes = mapAttrs processHandlers (config.schemes or { }); |
| 73 | + schemeAssertions = flatten (mapAttrsToList (_: v: v.assertions) processedSchemes); |
| 74 | + schemeResults = mapAttrs (_: v: v.result) processedSchemes; |
| 75 | + |
| 76 | + # Combine all assertions |
| 77 | + allAssertions = mimeTypeAssertions ++ schemeAssertions; |
| 78 | + |
| 79 | + # Build the final handlers.json structure |
| 80 | + settings = { |
| 81 | + defaultHandlersVersion = { }; |
| 82 | + isDownloadsImprovementsAlreadyMigrated = false; |
| 83 | + mimeTypes = mimeTypeResults; |
| 84 | + schemes = schemeResults; |
| 85 | + }; |
| 86 | + |
| 87 | + # Common options shared between mimeTypes and schemes |
| 88 | + commonHandlerOptions = { |
| 89 | + action = mkOption { |
| 90 | + type = types.enum [ |
| 91 | + 0 |
| 92 | + 1 |
| 93 | + 2 |
| 94 | + 3 |
| 95 | + 4 |
| 96 | + ]; |
| 97 | + default = 1; |
| 98 | + description = '' |
| 99 | + The action to take for this MIME type / URL scheme. Possible values: |
| 100 | + - 0: Save file |
| 101 | + - 1: Always ask |
| 102 | + - 2: Use helper app |
| 103 | + - 3: Open in ${appName} |
| 104 | + - 4: Use system default |
| 105 | + ''; |
| 106 | + }; |
| 107 | + |
| 108 | + ask = mkOption { |
| 109 | + type = types.bool; |
| 110 | + default = true; |
| 111 | + description = '' |
| 112 | + If true, the user is asked what they want to do with the file. |
| 113 | + If false, the action is taken without user intervention. |
| 114 | + ''; |
| 115 | + }; |
| 116 | + |
| 117 | + handlers = mkOption { |
| 118 | + type = types.listOf ( |
| 119 | + types.submodule { |
| 120 | + options = { |
| 121 | + name = mkOption { |
| 122 | + type = types.nullOr types.str; |
| 123 | + default = null; |
| 124 | + description = '' |
| 125 | + The display name of the handler. |
| 126 | + ''; |
| 127 | + }; |
| 128 | + |
| 129 | + path = mkOption { |
| 130 | + type = types.nullOr types.str; |
| 131 | + default = null; |
| 132 | + description = '' |
| 133 | + The native path to the executable to be used. |
| 134 | + Choose between path or uriTemplate. |
| 135 | + ''; |
| 136 | + }; |
| 137 | + |
| 138 | + uriTemplate = mkOption { |
| 139 | + type = types.nullOr types.str; |
| 140 | + default = null; |
| 141 | + description = '' |
| 142 | + A URL to a web based application handler. |
| 143 | + The URL must be https and contain a %s to be used for substitution. |
| 144 | + Choose between path or uriTemplate. |
| 145 | + ''; |
| 146 | + }; |
| 147 | + }; |
| 148 | + } |
| 149 | + ); |
| 150 | + default = [ ]; |
| 151 | + description = '' |
| 152 | + An array of handlers with the first one being the default. |
| 153 | + If you don't want to have a default handler, use an empty object for the first handler. |
| 154 | + Only valid when action is set to 2 (Use helper app). |
| 155 | + ''; |
| 156 | + }; |
| 157 | + }; |
| 158 | +in |
| 159 | +{ |
| 160 | + imports = [ (pkgs.path + "/nixos/modules/misc/meta.nix") ]; |
| 161 | + |
| 162 | + meta.maintainers = with maintainers; [ kugland ]; |
| 163 | + |
| 164 | + options = { |
| 165 | + enable = mkOption { |
| 166 | + type = types.bool; |
| 167 | + default = false; |
| 168 | + internal = true; |
| 169 | + description = '' |
| 170 | + Whether to enable handlers configuration for this profile. |
| 171 | + ''; |
| 172 | + }; |
| 173 | + |
| 174 | + force = mkOption { |
| 175 | + type = types.bool; |
| 176 | + default = false; |
| 177 | + description = '' |
| 178 | + Whether to force replace the existing handlers configuration. |
| 179 | + ''; |
| 180 | + }; |
| 181 | + |
| 182 | + mimeTypes = mkOption { |
| 183 | + type = types.attrsOf ( |
| 184 | + types.submodule { |
| 185 | + options = commonHandlerOptions // { |
| 186 | + extensions = mkOption { |
| 187 | + type = types.listOf types.str; |
| 188 | + default = [ ]; |
| 189 | + example = [ |
| 190 | + "jpg" |
| 191 | + "jpeg" |
| 192 | + ]; |
| 193 | + description = '' |
| 194 | + List of file extensions associated with this MIME type. |
| 195 | + ''; |
| 196 | + }; |
| 197 | + }; |
| 198 | + } |
| 199 | + ); |
| 200 | + default = { }; |
| 201 | + example = literalExpression '' |
| 202 | + { |
| 203 | + "application/pdf" = { |
| 204 | + action = 2; |
| 205 | + ask = false; |
| 206 | + handlers = [ |
| 207 | + { |
| 208 | + name = "Okular"; |
| 209 | + path = "''${pkgs.okular}/bin/okular"; |
| 210 | + } |
| 211 | + ]; |
| 212 | + extensions = [ "pdf" ]; |
| 213 | + }; |
| 214 | + } |
| 215 | + ''; |
| 216 | + description = '' |
| 217 | + Attribute set mapping MIME types to their handler configurations. |
| 218 | + ''; |
| 219 | + }; |
| 220 | + |
| 221 | + schemes = mkOption { |
| 222 | + type = types.attrsOf ( |
| 223 | + types.submodule { |
| 224 | + options = commonHandlerOptions; |
| 225 | + } |
| 226 | + ); |
| 227 | + default = { }; |
| 228 | + example = literalExpression '' |
| 229 | + { |
| 230 | + mailto = { |
| 231 | + action = 2; |
| 232 | + ask = false; |
| 233 | + handlers = [ |
| 234 | + { |
| 235 | + name = "Gmail"; |
| 236 | + uriTemplate = "https://mail.google.com/mail/?extsrc=mailto&url=%s"; |
| 237 | + } |
| 238 | + ]; |
| 239 | + }; |
| 240 | + } |
| 241 | + ''; |
| 242 | + description = '' |
| 243 | + Attribute set mapping URL schemes to their handler configurations. |
| 244 | + ''; |
| 245 | + }; |
| 246 | + |
| 247 | + assertions = mkOption { |
| 248 | + type = types.listOf types.unspecified; |
| 249 | + default = allAssertions; |
| 250 | + internal = true; |
| 251 | + readOnly = true; |
| 252 | + description = '' |
| 253 | + Validation assertions for handler configuration. |
| 254 | + ''; |
| 255 | + }; |
| 256 | + |
| 257 | + settings = mkOption { |
| 258 | + type = jsonFormat.type; |
| 259 | + default = settings; |
| 260 | + internal = true; |
| 261 | + readOnly = true; |
| 262 | + description = '' |
| 263 | + Resulting handlers.json settings. |
| 264 | + ''; |
| 265 | + }; |
| 266 | + }; |
| 267 | + |
| 268 | + config = { |
| 269 | + enable = mkDefault (config.mimeTypes != { } || config.schemes != { }); |
| 270 | + }; |
| 271 | +} |
0 commit comments