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

    recipeRoot = mkOption {
      type = types.path;
      default = "/var/lib/cookbook";
    };

    user = mkOption {
      type = types.str;
      default = "cook-cli";
    };

    group = mkOption {
      type = types.str;
      default = "cook-cli";
    };

    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 == "cook-cli") {
        ${cfg.user} = {
          isSystemUser = true;
          group = cfg.group;
        };
      };
      groups = mkIf (cfg.group == "cook-cli") {
        ${cfg.group} = {};
      };
    };

    systemd.services.cooklang-chef = {
      description = "A website for serving cooklang cookbooks";
      after = [ "network.target" ];
      requires = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        ExecStart = "${getExe cfg.package} server ${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.recipeRoot;
        RemoveIPC = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ];
        UMask = "0027";
        WorkingDirectory = cfg.recipeRoot;
      };
    };

    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}";
        };
      };
    };
  };
}