aboutsummaryrefslogtreecommitdiffstats
path: root/modules/office/calcurse/default.nix
blob: 3296f95e7994ee56ff8cad72aad0f819d0d927fa (plain)
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
# Text-based calendar and scheduling application
{
  config,
  lib,
  pkgs,
  ...
}:
let
  calcurseSync = pkgs.writeShellScriptBin "calcurse-sync" (builtins.readFile ./calcurse-sync.sh);

  calcursePackage = pkgs.symlinkJoin {
    name = "calcurse-wrapped";
    paths = [
      pkgs.bash
      pkgs.calcurse
    ]
    ++ lib.optionals config.development.nixvim.enable [ config.development.nixvim.package ]
    ++ lib.optionals config.office.calcurse.sync.enable [ calcurseSync ];
    nativeBuildInputs = [ pkgs.makeWrapper ];
    postBuild = ''
      ${lib.optionalString config.development.nixvim.enable ''
        wrapProgram $out/bin/calcurse --set PAGER "nvim"
      ''}
      ${lib.optionalString config.office.calcurse.sync.enable ''
        wrapProgram $out/bin/calcurse-sync \
          --prefix PATH : ${
            lib.makeBinPath [
              config.development.git.package
              pkgs.coreutils
              pkgs.gnused
            ]
          } \
          ${lib.optionalString (config.office.calcurse.sync.remote != null) ''
            --set CALCURSE_SYNC_REMOTE ${lib.escapeShellArg config.office.calcurse.sync.remote}
          ''}
      ''}
    '';
    meta = {
      mainProgram = "calcurse";
    };
  };
in
{
  options.office.calcurse = {
    enable = lib.mkEnableOption "Enables calcurse.";
    package = lib.mkOption {
      type = lib.types.package;
      readOnly = true;
      default = calcursePackage;
      description = "The calcurse package to use.";
    };
    sync = {
      enable = lib.mkEnableOption "Enables git-backed syncing of the calcurse data directory (builds calcurse-sync, installs the sync hooks, and runs an initial 'calcurse-sync init' on activation).";
      remote = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = null;
        description = "Git remote URL for the calcurse data directory. Use an https:// URL if 'credential' is configured. When set, calcurse-sync uses it automatically on first init instead of prompting.";
      };
      credential = {
        username = lib.mkOption {
          type = lib.types.nullOr lib.types.str;
          default = null;
          description = "Username for HTTPS git authentication against the calcurse remote.";
        };
        passwordGopassPath = lib.mkOption {
          type = lib.types.nullOr lib.types.str;
          default = null;
          description = "gopass entry path holding the password or token used for HTTPS git authentication against the calcurse remote. E.g. 'git/calcurse-sync'.";
        };
      };
    };
  };

  config = lib.mkIf config.office.calcurse.enable (
    lib.mkMerge [
      {
        home.packages = [ config.office.calcurse.package ];
        xdg.configFile."calcurse/conf" = {
          source = ./conf;
          force = true;
        };
        xdg.configFile."calcurse/keys" = {
          source = ./keys;
          force = true;
        };
      }
      (lib.mkIf config.office.calcurse.sync.enable {
        xdg.configFile."calcurse/hooks/pre-load" = {
          source = ./hooks/pre-load;
          executable = true;
          force = true;
        };
        xdg.configFile."calcurse/hooks/post-save" = {
          source = ./hooks/post-save;
          executable = true;
          force = true;
        };

        programs.git.includes = lib.mkIf config.development.git.enable [
          {
            condition = "gitdir:${config.xdg.dataHome}/calcurse/";
            contents = {
              user = {
                name = "Calcurse of ${config.home.username}";
                email = "${config.home.username}@calcurse.localhost";
              };
              commit.gpgSign = false;
              tag.gpgSign = false;
            }
            // lib.optionalAttrs (config.office.calcurse.sync.credential.passwordGopassPath != null) {
              credential.helper = "!f() { echo username=${lib.escapeShellArg config.office.calcurse.sync.credential.username}; echo password=\"$(${config.security.gopass.package}/bin/gopass show -o ${lib.escapeShellArg config.office.calcurse.sync.credential.passwordGopassPath})\"; }; f";
            };
          }
        ];

        home.activation.calcurseSyncInit = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
          run ${config.office.calcurse.package}/bin/calcurse-sync init || true
        '';
      })
    ]
  );
}