aboutsummaryrefslogtreecommitdiffstats
path: root/nix/module.hm.nix
blob: 1891abb6726d12f702d2aae74971e1a654a8c749 (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
{turtlePkg}: {
  config,
  pkgs,
  lib,
  ...
}: let
  cfg = config.programs.turtle;

  tomlFormat = pkgs.formats.toml {};

  inherit
    (lib)
    mkIf
    mkOption
    types
    ;
in {
  options.programs.turtle = {
    enable = lib.mkEnableOption "turtle";

    package = (lib.mkPackageOption pkgs "turtle" {}) // {default = turtlePkg;};

    settings = mkOption {
      type = with types; let
        prim = oneOf [
          bool
          int
          str
        ];
        primOrPrimAttrs = either prim (attrsOf prim);
        entry = either prim (listOf primOrPrimAttrs);
        entryOrAttrsOf = t: either entry (attrsOf t);
        entries = entryOrAttrsOf (entryOrAttrsOf entry);
      in
        attrsOf entries // {description = "Turtle configuration";};
      default = {};
      example = lib.literalExpression ''
        {
          auto_sync = true;
          sync_frequency = "5m";
          sync_address = "https://api.atuin.sh";
          search_mode = "prefix";
        }
      '';
      description = ''
        Configuration written to
        {file}`$XDG_CONFIG_HOME/turtle/config.toml`.
      '';
    };

    daemon = {
      logLevel = mkOption {
        default = null;
        type = types.nullOr (
          types.enum [
            "trace"
            "debug"
            "info"
            "warn"
            "error"
          ]
        );
        description = ''
          Verbosity of Turtle daemon logging.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = config.systemd.user.enable || config.launchd.enable;
        message = "The Turtle daemon can only be configured on systems with systemd or launchd.";
      }
    ];

    # Always add the configured `turtle` package.
    home.packages = [cfg.package];

    # If there are user-provided settings, generate the config file.
    xdg.configFile = lib.mkMerge [
      (mkIf (cfg.settings != {}) {
        "turtle/config.toml" = {
          source = tomlFormat.generate "turtle-config" cfg.settings;
        };
      })
    ];

    programs.turtle.settings.daemon = {
      systemd_socket = config.systemd.user.enable;
      socket_path = lib.mkIf (!config.systemd.user.enable) (
        lib.mkDefault "${config.xdg.dataHome}/turtle/daemon.sock"
      );
    };

    systemd.user.services.turtle-daemon = {
      Unit = {
        Description = "Turtle daemon";
        Requires = ["turtle-daemon.socket"];
      };
      Install = {
        Also = ["turtle-daemon.socket"];
        WantedBy = ["default.target"];
      };
      Service = {
        ExecStart = "${lib.getExe' cfg.package "turtle-daemon"} start";
        Environment = lib.optionals (cfg.daemon.logLevel != null) ["ATUIN_LOG=${cfg.daemon.logLevel}"];
        Restart = "on-failure";
        RestartSteps = 3;
        RestartMaxDelaySec = 6;
      };
    };

    systemd.user.sockets.turtle-daemon = {
      Unit = {
        Description = "Turtle daemon socket";
      };
      Install = {
        WantedBy = ["sockets.target"];
      };
      Socket = {
        ListenStream = "%t/turtle.sock";
        SocketMode = "0600";
        RemoveOnStop = true;
      };
    };
  };
}