fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
3.6 KiB123 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.taskwarrior;
8
9  formatValue = value:
10    if isBool value then
11      if value then "true" else "false"
12    else if isList value then
13      concatMapStringsSep "," formatValue value
14    else
15      toString value;
16
17  formatLine = key: value: "${key}=${formatValue value}";
18
19  formatSet = key: values:
20    (concatStringsSep "\n"
21      (mapAttrsToList (subKey: subValue: formatPair "${key}.${subKey}" subValue)
22        values));
23
24  formatPair = key: value:
25    if isAttrs value then formatSet key value else formatLine key value;
26
27  homeConf = "${config.xdg.configHome}/task/home-manager-taskrc";
28  userConf = "${config.xdg.configHome}/task/taskrc";
29in {
30  options = {
31    programs.taskwarrior = {
32      enable = mkEnableOption "Task Warrior";
33
34      config = mkOption {
35        type = types.attrsOf types.anything;
36        default = { };
37        example = literalExpression ''
38          {
39            confirmation = false;
40            report.minimal.filter = "status:pending";
41            report.active.columns = [ "id" "start" "entry.age" "priority" "project" "due" "description" ];
42            report.active.labels  = [ "ID" "Started" "Age" "Priority" "Project" "Due" "Description" ];
43            taskd = {
44              certificate = "/path/to/cert";
45              key = "/path/to/key";
46              ca = "/path/to/ca";
47              server = "host.domain:53589";
48              credentials = "Org/First Last/cf31f287-ee9e-43a8-843e-e8bbd5de4294";
49            };
50          }
51        '';
52        description = ''
53          Key-value configuration written to
54          {file}`$XDG_CONFIG_HOME/task/taskrc`.
55        '';
56      };
57
58      dataLocation = mkOption {
59        type = types.str;
60        default = "${config.xdg.dataHome}/task";
61        defaultText = "$XDG_DATA_HOME/task";
62        description = ''
63          Location where Task Warrior will store its data.
64
65          Home Manager will attempt to create this directory.
66        '';
67      };
68
69      colorTheme = mkOption {
70        type = with types; nullOr (either str path);
71        default = null;
72        example = "dark-blue-256";
73        description = ''
74          Either one of the default provided theme as string, or a
75          path to a theme configuration file.
76        '';
77      };
78
79      extraConfig = mkOption {
80        type = types.lines;
81        default = "";
82        description = ''
83          Additional content written at the end of
84          {file}`$XDG_CONFIG_HOME/task/taskrc`.
85        '';
86      };
87
88      package = mkPackageOption pkgs "taskwarrior" { };
89    };
90  };
91
92  config = mkIf cfg.enable {
93    home.packages = [ cfg.package ];
94
95    home.file."${homeConf}".text = ''
96      data.location=${cfg.dataLocation}
97      ${optionalString (cfg.colorTheme != null) (if isString cfg.colorTheme then
98        "include ${cfg.colorTheme}.theme"
99      else
100        "include ${cfg.colorTheme}")}
101
102      ${concatStringsSep "\n" (mapAttrsToList formatPair cfg.config)}
103
104      ${cfg.extraConfig}
105    '';
106
107    home.activation.regenDotTaskRc = hm.dag.entryAfter [ "writeBoundary" ] ''
108      $VERBOSE_ECHO "Ensuring generated taskwarrior config included in taskrc"
109
110      if [[ ! -s "${userConf}" ]]; then
111        # Ensure file's existence
112        if [[ -v DRY_RUN ]]; then
113          $DRY_RUN_CMD echo "include ${homeConf}" ">" "${userConf}"
114        else
115          echo "include ${homeConf}" > "${userConf}"
116        fi
117      elif ! grep -qF "include ${homeConf}" ${escapeShellArg userConf}; then
118        # Add include statement for Home Manager generated config.
119        $DRY_RUN_CMD sed -i '1i include ${homeConf}' ${escapeShellArg userConf}
120      fi
121    '';
122  };
123}