fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
7.7 KiB254 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.kodi;
7
8  stylesheetCommonHeader = ''
9    <?xml version="1.0"?>
10    <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
11    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
12    <xsl:strip-space elements="*"/>
13  '';
14
15  stylesheetCommonFooter = "</xsl:stylesheet>";
16
17  stylesheetNestedTags = ''
18    <xsl:template match="attr[attrs]">
19      <xsl:variable name="elementName" select="@name"/>
20      <xsl:element name="{$elementName}">
21        <xsl:apply-templates select="attrs" />
22      </xsl:element>
23    </xsl:template>
24    <xsl:template match="attr[list[attrs]]">
25    <xsl:variable name="elementName" select="@name"/>
26    <xsl:for-each select="list/attrs">
27      <xsl:element name="{$elementName}">
28        <xsl:apply-templates select="." />
29      </xsl:element>
30    </xsl:for-each>
31    </xsl:template>
32    <xsl:template match="attr[not(attrs|list)]">
33      <xsl:variable name="elementName" select="@name"/>
34      <xsl:element name="{$elementName}">
35      <xsl:if test="$elementName='path'">
36        <!-- needed in sources.xml but will be used for all "path" tags -->
37        <xsl:attribute name="pathversion">1</xsl:attribute>
38      </xsl:if>
39        <xsl:value-of select="*/@value" />
40      </xsl:element>
41    </xsl:template>
42  '';
43
44  stylesheetTagsAsSettingWithId = ''
45    <xsl:template match='attr'>
46      <setting>
47        <xsl:attribute name="id">
48          <xsl:value-of select="@name" />
49        </xsl:attribute>
50        <xsl:value-of select="*/@value" />
51      </setting>
52    </xsl:template>
53  '';
54
55  stylesheetAdvancedSettingsRootTag = ''
56    <xsl:template match='/'>
57      <xsl:comment> Generated by Home Manager. </xsl:comment>
58      <advancedsettings version="1.0">
59        <xsl:apply-templates/>
60      </advancedsettings>
61    </xsl:template>
62  '';
63
64  stylesheetSourcesRootTag = ''
65    <xsl:template match='/'>
66      <xsl:comment> Generated by Home Manager. </xsl:comment>
67      <sources>
68        <xsl:apply-templates/>
69      </sources>
70    </xsl:template>
71  '';
72
73  stylesheetAddonSettingsRootTag = ''
74    <xsl:template match='/'>
75      <xsl:comment> Generated by Home Manager. </xsl:comment>
76      <settings version="2">
77        <xsl:apply-templates/>
78      </settings>
79    </xsl:template>
80  '';
81
82  attrsetToXml = attrs: name: stylesheet:
83    pkgs.runCommand name {
84      # Package splicing for libxslt does not work correctly leading to errors
85      # when cross-compiling. Use the version from buildPackages explicitly to
86      # fix this.
87      nativeBuildInputs = [ pkgs.buildPackages.libxslt.bin ];
88      xml = builtins.toXML attrs;
89      passAsFile = [ "xml" ];
90    } ''
91      xsltproc ${stylesheet} - < "$xmlPath" > "$out"
92    '';
93
94  attrsetToAdvancedSettingsXml = attrs: name:
95    let
96      stylesheet = builtins.toFile "stylesheet.xsl" ''
97        ${stylesheetCommonHeader}
98        ${stylesheetAdvancedSettingsRootTag}
99        ${stylesheetNestedTags}
100        ${stylesheetCommonFooter}
101      '';
102    in attrsetToXml attrs name stylesheet;
103
104  attrsetToSourcesXml = attrs: name:
105    let
106      stylesheet = builtins.toFile "stylesheet.xsl" ''
107        ${stylesheetCommonHeader}
108        ${stylesheetSourcesRootTag}
109        ${stylesheetNestedTags}
110        ${stylesheetCommonFooter}
111      '';
112    in attrsetToXml attrs name stylesheet;
113
114  attrsetToAddonSettingsXml = attrs: name:
115    let
116      stylesheet = builtins.toFile "stylesheet.xsl" ''
117        ${stylesheetCommonHeader}
118        ${stylesheetAddonSettingsRootTag}
119        ${stylesheetTagsAsSettingWithId}
120        ${stylesheetCommonFooter}
121      '';
122    in attrsetToXml attrs name stylesheet;
123
124in {
125  meta.maintainers = [ hm.maintainers.dwagenk ];
126
127  options.programs.kodi = {
128    enable = mkEnableOption "Kodi";
129
130    package = mkOption {
131      type = types.package;
132      default = pkgs.kodi;
133      defaultText = literalExpression "pkgs.kodi";
134      example = literalExpression
135        "pkgs.kodi.withPackages (exts: [ exts.pvr-iptvsimple ])";
136      description = ''
137        The `kodi` package to use.
138        Can be used to specify extensions.
139      '';
140    };
141
142    datadir = mkOption {
143      type = types.path;
144      default = "${config.home.homeDirectory}/.kodi";
145      defaultText =
146        literalExpression ''"''${config.home.homeDirectory}/.kodi"'';
147      example = literalExpression ''"''${config.xdg.dataHome}/kodi"'';
148      description = "Directory to store configuration and metadata.";
149    };
150
151    settings = mkOption {
152      type = with types;
153        let
154          valueType = either str (attrsOf valueType) // {
155            description = "attribute sets of strings";
156          };
157        in nullOr valueType;
158      default = null;
159      example = literalExpression ''
160        { videolibrary.showemptytvshows = "true"; }
161      '';
162      description = ''
163        Configuration to write to the `advancedsettings.xml`
164        file in kodis userdata directory. Settings specified here will be
165        immutable from inside kodi and be hidden from the GUI settings dialog.
166
167        See <https://kodi.wiki/view/Advancedsettings.xml> as
168        reference for how settings need to be specified.
169
170        The innermost attributes must be of type str.
171      '';
172    };
173
174    sources = mkOption {
175      type = with types;
176        let
177          valueType = oneOf [ str (attrsOf valueType) (listOf valueType) ] // {
178            description = "attribute sets or lists of strings";
179          };
180        in nullOr valueType;
181      default = null;
182      example = literalExpression ''
183        {
184          video = {
185            default = "movies";
186            source = [
187              { name = "videos"; path = "/path/to/videos"; allowsharing = "true"; }
188              { name = "movies"; path = "/path/to/movies"; allowsharing = "true"; }
189            ];
190          };
191        }
192      '';
193      description = ''
194        Contents to populate the file `sources.xml` in kodis
195        userdata directory.
196
197        See <https://kodi.wiki/view/Sources.xml> as
198        reference for how sources need to be specified.
199
200        Kodi will still show the dialogs to modify sources in the GUI and they
201        appear to be mutable. This however is not the case and the sources will
202        stay as specified via Home Manager.
203
204        The innermost attributes must be of type str.
205      '';
206    };
207
208    addonSettings = mkOption {
209      type = with types; nullOr (attrsOf (attrsOf str));
210      default = null;
211      example = literalExpression ''
212        { "service.xbmc.versioncheck".versioncheck_enable = "false"; }
213      '';
214      description = ''
215        Attribute set with the plugin namespace as toplevel key and the plugins
216        settings as lower level key/value pairs.
217
218        Kodi will still show the settings of plugins configured via this
219        mechanism in the GUI and they appear to be mutable. This however is
220        not the case and the settings will stay as specified via Home Manager.
221      '';
222    };
223  };
224
225  config = mkIf cfg.enable (mkMerge [
226    {
227      assertions = [
228        (lib.hm.assertions.assertPlatform "programs.kodi" pkgs
229          lib.platforms.linux)
230      ];
231
232      home.packages = [ cfg.package ];
233      home.sessionVariables = { KODI_DATA = cfg.datadir; };
234    }
235
236    (mkIf (cfg.settings != null) {
237      home.file."${cfg.datadir}/userdata/advancedsettings.xml".source =
238        attrsetToAdvancedSettingsXml cfg.settings "kodi-advancedsettings.xml";
239    })
240
241    (mkIf (cfg.sources != null) {
242      home.file."${cfg.datadir}/userdata/sources.xml".source =
243        attrsetToSourcesXml cfg.sources "kodi-sources.xml";
244    })
245
246    (mkIf (cfg.addonSettings != null) {
247      home.file = mapAttrs' (k: v:
248        attrsets.nameValuePair
249        ("${cfg.datadir}/userdata/addon_data/${k}/settings.xml") {
250          source = attrsetToAddonSettingsXml v "kodi-addon-${k}-settings.xml";
251        }) cfg.addonSettings;
252    })
253  ]);
254}