about summary refs log tree commit diff stats
path: root/modules/by-name/ts/tskm/module.nix
blob: f0d6a50d10d5f8a3e9134f1dc7279f0966ec82c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# nixos-config - My current NixOS configuration
#
# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of my nixos-config.
#
# You should have received a copy of the License along with this program.
# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
{
  lib,
  config,
  pkgs,
  ...
}: let
  cfg = config.soispha.programs.tskm;

  allProjectNames = lib.lists.flatten (lib.attrsets.mapAttrsToList mkProject cfg.projects);

  projectList = builtins.concatStringsSep "\n" allProjectNames;
  mkProject = name: value: let
    subprojects = lib.attrsets.mapAttrsToList (newName: mkProject "${name}.${newName}") value.subprojects;
  in
    [name] ++ subprojects;

  firefoxProfiles = builtins.listToAttrs (lib.imap0 (index: name:
    lib.attrsets.nameValuePair name {
      inherit name;
      # Add one here, so that we can have the default profile at id 0.
      id = index + 1;
    })
  allProjectNames);

  contexts =
    builtins.concatStringsSep "\n"
    (lib.lists.flatten
      (lib.attrsets.mapAttrsToList mkContext cfg.projects));
  mkContext = name: value: let
    subprojects =
      lib.attrsets.mapAttrsToList
      (newName: newValue: let
        finalValue =
          if newValue.prefix == ""
          then newValue // {inherit (value) prefix;}
          else newValue;
      in
        mkContext "${name}.${newName}" finalValue)
      value.subprojects;
    contextName = builtins.replaceStrings ["."] ["_"] name;
  in
    [
      ''
        context.${contextName}.rc.neorg_path=${value.prefix}/index.norg
        context.${contextName}.read=project:${name}
        context.${contextName}.write=project:${name}
      ''
    ]
    ++ subprojects;

  contextsFile =
    pkgs.writeText "contexts-file" contexts;

  projectType = lib.types.submodule {
    options = {
      name = lib.mkOption {
        type = lib.types.str;
        example = "timesinks";
        description = "The name of this project";
      };

      prefix = lib.mkOption {
        type = lib.types.str;
        default = "";
        example = "programming/zig";
        description = ''
          A prefix to append to the project neorg path.
          This can be used to group similar project's neorg directories together. '
        '';
      };

      subprojects = lib.mkOption {
        type = lib.types.attrsOf projectType;
        description = ''
          A list of subprojects.
          These can also contain subprojects.
        '';
        default = {};
      };
    };
  };
in {
  options.soispha.programs.tskm = {
    enable = lib.mkEnableOption "tskm";

    projects = lib.mkOption {
      type = lib.types.attrsOf projectType;
    };
  };

  config = lib.mkIf cfg.enable {
    soispha.programs = {
      firefox.profiles = firefoxProfiles;
      taskwarrior = {
        includeFiles = {
          tskm-contexts = contextsFile;
        };
        hooks = {
          enforce-projects =
            config.lib.taskwarrior.mkHook "on-add" [
              pkgs.jq
              pkgs.gnugrep
              pkgs.tskm
            ]
            ./taskwarrior_hooks/enforce-projects.sh;
        };
      };
    };

    home-manager.users.soispha = {
      home.sessionVariables = {
        # TODO: Remove this hard-coded path with a reference. <2025-04-04>
        "TSKM_PROJECT_FILE" = "/home/soispha/repos/nix/config/modules/common/projects.json";
      };

      programs.nixvim = {
        plugins.neorg.settings.load."core.dirman".config.workspaces = {
          tskm = "~/.local/share/tskm/notes";
        };
      };

      xdg.configFile."tskm/projects.list" = {
        text = projectList;
      };
    };
  };
}