fork/home-manager

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

Xavier Lambeindarkman: add moduleb25161c6

master
3.0 KiB115 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.services.darkman;
7
8  yamlFormat = pkgs.formats.yaml { };
9
10  scriptsOptionType = kind:
11    mkOption {
12      type = types.attrsOf (types.oneOf [ types.path types.lines ]);
13      default = { };
14      example = literalExpression ''
15        {
16          gtk-theme = '''
17            ''${pkgs.dconf}/bin/dconf write \
18                /org/gnome/desktop/interface/color-scheme "'prefer-${kind}'"
19          ''';
20          my-python-script = pkgs.writers.writePython3 "my-python-script" { } '''
21            print('Do something!')
22          ''';
23        }
24      '';
25      description = ''
26        Scripts to run when switching to "${kind} mode".
27
28        Multiline strings are interpreted as Bash shell scripts and a shebang is
29        not required.
30      '';
31    };
32
33  generateScripts = folder:
34    mapAttrs' (k: v: {
35      name = "${folder}/${k}";
36      value = {
37        source = if builtins.isPath v || isDerivation v then
38          v
39        else
40          pkgs.writeShellScript (hm.strings.storeFileName k) v;
41      };
42    });
43in {
44  meta.maintainers = [ maintainers.xlambein ];
45
46  options.services.darkman = {
47    enable = mkEnableOption ''
48      darkman, a tool that automatically switches dark-mode on and off based on
49      the time of the day'';
50
51    package = mkPackageOption pkgs "darkman" { };
52
53    settings = mkOption {
54      type = types.submodule { freeformType = yamlFormat.type; };
55      example = literalExpression ''
56        {
57          lat = 52.3;
58          lng = 4.8;
59          usegeoclue = true;
60        }
61      '';
62      description = ''
63        Settings for the {command}`darkman` command. See
64        <https://darkman.whynothugo.nl/#CONFIGURATION> for details.
65      '';
66    };
67
68    darkModeScripts = scriptsOptionType "dark";
69
70    lightModeScripts = scriptsOptionType "light";
71  };
72
73  config = mkIf cfg.enable {
74    assertions = [
75      (hm.assertions.assertPlatform "services.darkman" pkgs platforms.linux)
76    ];
77
78    home.packages = [ cfg.package ];
79
80    xdg.configFile = {
81      "darkman/config.yaml" = mkIf (cfg.settings != { }) {
82        source = yamlFormat.generate "darkman-config.yaml" cfg.settings;
83      };
84    };
85
86    xdg.dataFile = mkMerge [
87      (mkIf (cfg.darkModeScripts != { })
88        (generateScripts "dark-mode.d" cfg.darkModeScripts))
89      (mkIf (cfg.lightModeScripts != { })
90        (generateScripts "light-mode.d" cfg.lightModeScripts))
91    ];
92
93    systemd.user.services.darkman = {
94      Unit = {
95        Description = "Darkman system service";
96        Documentation = "man:darkman(1)";
97        PartOf = [ "graphical-session.target" ];
98        BindsTo = [ "graphical-session.target" ];
99        X-Restart-Triggers =
100          [ "${config.xdg.configFile."darkman/config.yaml".source}" ];
101      };
102
103      Service = {
104        Type = "dbus";
105        BusName = "nl.whynothugo.darkman";
106        ExecStart = "${getExe cfg.package} run";
107        Restart = "on-failure";
108        TimeoutStopSec = 15;
109        Slice = "background.slice";
110      };
111
112      Install.WantedBy = mkDefault [ "graphical-session.target" ];
113    };
114  };
115}