summaryrefslogtreecommitdiff
path: root/modules/services/ofborg/default.nix
blob: 91510398deceaef4ab1a7dc3d9eb5d21c58c646f (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.ofborg;
  user = config.users.users.ofborg;
in

{
  options = {
    services.ofborg.enable = mkOption {
      type = types.bool;
      default = false;
      description = lib.mdDoc "Whether to enable the ofborg builder service.";
    };

    services.ofborg.package = mkOption {
      type = types.package;
      example = literalExpression "pkgs.ofborg";
      description = lib.mdDoc ''
        This option specifies the ofborg package to use. eg.

        (import <ofborg> {}).ofborg.rs

        $ nix-channel --add https://github.com/NixOS/ofborg/archive/released.tar.gz ofborg
        $ nix-channel --update
      '';
    };

    services.ofborg.configFile = mkOption {
      type = types.path;
      description = lib.mdDoc ''
        Configuration file to use for ofborg.

        WARNING Don't use a path literal or derivation for this,
        that would expose credentials in the store making them world readable.
      '';
    };

    services.ofborg.logFile = mkOption {
      type = types.path;
      default = "/var/log/ofborg.log";
      description = lib.mdDoc "The logfile to use for the ofborg service.";
    };
  };

  config = mkIf cfg.enable {

    assertions = [
      { assertion = elem "ofborg" config.users.knownGroups; message = "set users.knownGroups to enable ofborg group"; }
      { assertion = elem "ofborg" config.users.knownUsers; message = "set users.knownUsers to enable ofborg user"; }
    ];

    warnings = mkIf (isDerivation cfg.configFile) [
      "services.ofborg.configFile is a derivation, credentials will be world readable"
    ];

    services.ofborg.configFile = mkDefault "${user.home}/config.json";

    launchd.daemons.ofborg = {
      script = ''
        git config --global user.email "ofborg@example.com"
        git config --global user.name "OfBorg"

        exec ${cfg.package}/bin/builder "${cfg.configFile}"
      '';

      path = [ config.nix.package pkgs.bash pkgs.coreutils pkgs.curl pkgs.git ];
      environment =
        { RUST_BACKTRACE = "1";
          NIX_SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
        };

      serviceConfig.KeepAlive = true;
      serviceConfig.StandardErrorPath = cfg.logFile;
      serviceConfig.StandardOutPath = cfg.logFile;

      serviceConfig.GroupName = "ofborg";
      serviceConfig.UserName = "ofborg";
      serviceConfig.WorkingDirectory = user.home;
    };

    users.users.ofborg.uid = mkDefault 531;
    users.users.ofborg.gid = mkDefault config.users.groups.ofborg.gid;
    users.users.ofborg.home = mkDefault "/var/lib/ofborg";
    users.users.ofborg.shell = "/bin/bash";
    users.users.ofborg.description = "OfBorg service user";

    users.groups.ofborg.gid = mkDefault 531;
    users.groups.ofborg.description = "Nix group for OfBorg service";

    # FIXME: create logfiles automatically if defined.
    system.activationScripts.preActivation.text = ''
      mkdir -p '${user.home}'
      touch '${cfg.logFile}'
      chown ${toString user.uid}:${toString user.gid} '${user.home}' '${cfg.logFile}'
    '';

    system.activationScripts.postActivation.text = ''
      if ! test -f '${cfg.configFile}'; then
        echo >&2 "warning: ofborg config \"${cfg.configFile}\" does not exist"
      fi

      chmod 600 '${cfg.configFile}'
      chown ${toString user.uid}:${toString user.gid} '${cfg.configFile}'
    '';

  };
}