aboutsummaryrefslogtreecommitdiff
path: root/modules/apps/sproxy-web.nix
blob: 4634958b6a504a7c44392b1489f8c81bd64a906d (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
{ config, pkgs, lib, ... }:

let

  inherit (lib)
    concatStrings hasPrefix mkEnableOption mkIf mkOption
    optionalString types ;
  inherit (types)
    int nullOr path str ;

  cfg = config.nixsap.apps.sproxy-web;

  ExecStart = concatStrings [
    "${pkgs.sproxy-web}/bin/sproxy-web"
    (optionalString (cfg.connectionString != null) " -c '${cfg.connectionString}'")
    (if (cfg.port != null)
      then " -p ${toString cfg.port}"
      else " -s '${cfg.socket}'")
  ];

in {
  options.nixsap.apps.sproxy-web = {
    enable = mkEnableOption "Sproxy Web";
    user = mkOption {
      description = "User to run as";
      default = "sproxy-web";
      type = str;
    };
    connectionString = mkOption {
      description = "PostgreSQL connection string";
      type = str;
      example = "user=sproxy-web dbname=sproxy port=6001";
    };
    pgPassFile = mkOption {
      description = "postgreSQL password file (secret)";
      default = null;
      type = nullOr path;
    };
    socket = mkOption {
      description = "UNIX socket to listen on. Ignored when TCP port is set";
      default = "/tmp/sproxy-web.sock";
      type = path;
    };
    port = mkOption {
      description = "TCP port to listen on (insecure)";
      type = nullOr int;
      default = null;
    };
  };

  config = mkIf cfg.enable {
    nixsap.system.users.daemons = [ cfg.user ];
    nixsap.deployment.keyrings.${cfg.user} = [ cfg.pgPassFile ];
    systemd.services.sproxy-web = {
      description = "Web interface to Sproxy database";
      wantedBy = [ "multi-user.target" ];
      wants = [ "keys.target" ];
      after = [ "keys.target" "network.target" "local-fs.target" ];
      serviceConfig = {
        inherit ExecStart;
        Restart = "on-failure";
        User = cfg.user;
      };
      environment.PGPASSFILE = cfg.pgPassFile;
    };
  };
}