summaryrefslogtreecommitdiff
path: root/custom/bar-service.nix
blob: 6e10d24b53063b031e48ea69936013f7de1a9729 (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.bar;
in {
  options.services.bar = {
    enable = mkEnableOption "the Bar Inventory System";

    user = mkOption {
      type = types.str;
      default = "bar";
      description = "User to execute the daemon under";
    };

    group = mkOption {
      type = types.str;
      default = "bar";
      description = "Group to execute the daemon under";
    };

    stateDir = mkOption {
      type = types.path;
      default = "/var/lib/bar";
      description = "Directory for the daemon to store semi-transient state (encryption keys etc.)";
    };

    port = mkOption {
      type = types.int;
      default = 8080;
      description = "Port for the daemon to listen on";
    };

    host = mkOption {
      type = types.str;
      default = "localhost";
      description = "Host to bind to";
    };

    approot = mkOption {
      type = types.str;
      default = "/";
      description = "Subdirectory of the daemon-managed site relative to HTTP /; only useful when using a reverse proxy";
    };

    ipFromHeader = mkOption {
      type = types.bool;
      default = false;
      description = "Determine IP from a HTTP header";
    };

    thermoprintBaseURL = mkOption {
      type = with types; nullOr types.str;
      default = null;
      example = "http://localhost:80/thermoprint/api";
      description = "Thermoprint base url";
    };
  };

  imports = [ ./thermoprint-service.nix ];

  config = mkIf cfg.enable {
    assertions = [
      { assertion = config.services.postgresql.enable;
        message = "bar requires PostgreSQL";
      }
    ];

    nixpkgs.config.allowUnfree = true;

    packages.thermoprint.enable = true;

    users.users."${cfg.user}" = {
      group = cfg.group;
      description = "User for the Bar Inventory System";
      isSystemUser = true;
      home = cfg.stateDir;
      createHome = true;
    };

    users.groups."${cfg.group}" = {};

    nixpkgs.overlays = [(selfPkgs: superPkgs: {
      haskell = superPkgs.haskell // {
        packages = superPkgs.haskell.packages // {
          ghc822 = superPkgs.haskell.packages.ghc822.extend (selfH: superH: {
            bar = superPkgs.callPackage ./bar { haskellPackages = selfH; };
          });
        };
      };

      inherit (selfPkgs.haskell.packages.ghc822) bar;
    })];

    systemd.services."bar" = {
      environment = {
        PORT = toString cfg.port;
        HOST = cfg.host;
        APPROOT = cfg.approot;
        IP_FROM_HEADER = toString cfg.ipFromHeader;
        TPRINT_BASEURL = mkIf (cfg.thermoprintBaseURL != null) cfg.thermoprintBaseURL;
      };

      bindsTo = [ "postgresql.service" ];
      after = [ "postgresql.service" ];
      wantedBy = [ "default.target" ];

      serviceConfig = {
        Type = "notify";
        User = cfg.user;
        Group = cfg.group;
        WorkingDirectory = cfg.stateDir;

        ExecStart = "${pkgs.bar}/bin/bar";
      };
    };
  };
}