summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/web-apps/lubelogger.nix
blob: 7bb7bad2cd98a172c89cd228e8f101ab16cf5f69 (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
123
124
125
126
127
128
129
130
131
132
133
134
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.lubelogger;
in
{
  meta.maintainers = with lib.maintainers; [
    bct
    lyndeno
  ];

  options = {
    services.lubelogger = {
      enable = lib.mkEnableOption "LubeLogger, a self-hosted, open-source, web-based vehicle maintenance and fuel milage tracker";

      package = lib.mkPackageOption pkgs "lubelogger" { };

      dataDir = lib.mkOption {
        description = "Path to LubeLogger config and metadata inside of `/var/lib/`.";
        default = "lubelogger";
        type = lib.types.str;
      };

      port = lib.mkOption {
        description = "The TCP port LubeLogger will listen on.";
        default = 5000;
        type = lib.types.port;
      };

      user = lib.mkOption {
        description = "User account under which LubeLogger runs.";
        default = "lubelogger";
        type = lib.types.str;
      };

      group = lib.mkOption {
        description = "Group under which LubeLogger runs.";
        default = "lubelogger";
        type = lib.types.str;
      };

      openFirewall = lib.mkOption {
        description = "Open ports in the firewall for the LubeLogger web interface.";
        default = false;
        type = lib.types.bool;
      };

      settings = lib.mkOption {
        type = with lib.types; attrsOf str;
        default = { };
        example = {
          LUBELOGGER_ALLOWED_FILE_EXTENSIONS = "";
          LUBELOGGER_LOGO_URL = "";
        };
        description = ''
          Additional configuration for LubeLogger, see <https://docs.lubelogger.com/Environment%20Variables> for supported values.
        '';
      };

      environmentFile = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
        example = "/run/secrets/lubelogger";
        description = ''
          Path to a file containing extra LubeLogger config options in the systemd `EnvironmentFile` format.
          Refer to the [documentation] for supported options.

          [documentation]: https://docs.lubelogger.com/Advanced/Environment%20Variables

          This can be used to pass secrets to LubeLogger without putting them in the Nix store.

          For example, to set an SMTP password, point `environmentFile` at a file containing:
          ```
          MailConfig__Password=<pass>
          ```
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.lubelogger = {
      description = "LubeLogger";

      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      environment = {
        Kestrel__Endpoints__Http__Url = "http://localhost:${toString cfg.port}";
      }
      // cfg.settings;

      serviceConfig = {
        Type = "simple";
        User = cfg.user;
        Group = cfg.group;
        StateDirectory = cfg.dataDir;
        WorkingDirectory = "/var/lib/${cfg.dataDir}";
        ExecStart = lib.getExe cfg.package;
        EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
        Restart = "on-failure";

        CapabilityBoundingSet = [ "" ];
        DeviceAllow = [ "" ];
        PrivateDevices = true;
        PrivateTmp = true;
        ProtectHome = true;
        RestrictAddressFamilies = [
          "AF_UNIX"
          "AF_INET"
          "AF_INET6"
        ];
        RestrictNamespaces = true;
      };
    };

    users.users = lib.mkIf (cfg.user == "lubelogger") {
      lubelogger = {
        isSystemUser = true;
        group = cfg.group;
        home = "/var/lib/${cfg.dataDir}";
      };
    };

    users.groups = lib.mkIf (cfg.group == "lubelogger") { lubelogger = { }; };

    networking.firewall = lib.mkIf cfg.openFirewall { allowedTCPPorts = [ cfg.port ]; };
  };
}