aboutsummaryrefslogtreecommitdiff
path: root/modules/nixos/cooklang-chef.nix
blob: 65af966e3885dd5307c0765c87408826dfd09697 (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
{ config, pkgs, lib, ... }:
with builtins // lib;
let
  cfg = config.services.cooklang-chef;
  args = escapeShellArgs ([
    "--port"
    (toString cfg.port)
    "--path"
    cfg.recipeRoot
  ] ++ cfg.extraArgs);
in {
  options.services.cooklang-chef = {
    enable = mkEnableOption "cooklang-chef webserver";
    package = mkPackageOption pkgs "cooklang-chef" {};

    recipeRoot = mkOption {
      type = types.path;
      default = "${cfg.dataDir}/cookbook";
    };

    user = mkOption {
      type = types.str;
      default = "cooklang-chef";
    };

    group = mkOption {
      type = types.str;
      default = "cooklang-chef";
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/cooklang-chef";
      description = ''
        Home directory for ${cfg.user}.
      '';
    };

    port = mkOption {
      type = types.port;
      default = 9080;
    };

    extraArgs = mkOption {
      type = with types; listOf str;
      default = [];
    };

    nginx = {
      enable = mkEnableOption "nginx reverse proxy";
      virtualHost = mkOption {
        type = types.str;
        example = mdDoc ''
          cook.example.com
        '';
      };
      location = mkOption {
        type = types.str;
        default = "/";
      };
    };
  };

  config = mkIf cfg.enable {
    users = {
      users = mkIf (cfg.user == "cooklang-chef") {
        ${cfg.user} = {
          isSystemUser = true;
          group = cfg.group;
          home = cfg.dataDir;
        };
      };
      groups = mkIf (cfg.group == "cooklang-chef") {
        ${cfg.group} = {};
      };
    };

    systemd.services.cooklang-chef = {
      description = "A website for serving cooklang cookbooks";
      after = [ "network.target" ];
      requires = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      unitConfig.RequiresMountsFor = cfg.recipeRoot;
      serviceConfig = {
        StateDirectory = "cooklang-chef cooklang-chef/.config";
        StateDirectoryMode = "0750";
        ExecStart = "${getExe cfg.package} serve ${args}";
        User = cfg.user;
        Group = cfg.group;
        IPAddressAllow = "localhost";
        IPAddressDeny = "any";
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = true;
        PrivateUsers = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        ReadWritePaths = cfg.dataDir;
        RemoveIPC = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ];
        UMask = "0027";
        WorkingDirectory = cfg.dataDir;
      };
    };

    services.nginx = mkIf cfg.nginx.enable {
      enable = mkDefault true;
      virtualHosts.${cfg.nginx.virtualHost} = {
        locations.${cfg.nginx.location} = {
          proxyPass = "http://127.0.0.1:${toString cfg.port}";
        };
      };
    };
  };
}