about summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/simplesamlphp.nix
blob: e970266fc17dd512e50152432e2baa73d0243199 (plain) (blame)
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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.simplesamlphp;

  format = pkgs.formats.php { finalVariable = "config"; };

  generateConfig =
    opts:
    pkgs.runCommand "simplesamlphp-config" { } ''
      mkdir $out
      cp ${format.generate "config.php" opts.settings} $out/config.php
      cp ${format.generate "authsources.php" opts.authSources} $out/authsources.php
    '';
in
{
  meta = {
    maintainers = with lib.maintainers; [ nhnn ];
  };

  options.services.simplesamlphp =
    with lib;
    mkOption {
      type = types.attrsOf (
        types.submodule (
          { config, ... }:
          {
            options = {
              package = mkPackageOption pkgs "simplesamlphp" { };
              configureNginx = mkOption {
                type = types.bool;
                default = true;
                description = "Configure nginx as a reverse proxy for SimpleSAMLphp.";
              };
              phpfpmPool = mkOption {
                type = types.str;
                description = "The PHP-FPM pool that serves SimpleSAMLphp instance.";
              };
              localDomain = mkOption {
                type = types.str;
                description = "The domain serving your SimpleSAMLphp instance. This option modifies only /saml route.";
              };
              settings = mkOption {
                type = types.submodule {
                  freeformType = format.type;
                  options = {
                    baseurlpath = mkOption {
                      type = types.str;
                      example = "https://filesender.example.com/saml/";
                      description = "URL where SimpleSAMLphp can be reached.";
                    };
                  };
                };
                default = { };
                description = ''
                  Configuration options used by SimpleSAMLphp.
                  See [](https://simplesamlphp.org/docs/stable/simplesamlphp-install)
                  for available options.
                '';
              };

              authSources = mkOption {
                type = format.type;
                default = { };
                description = ''
                  Auth sources options used by SimpleSAMLphp.
                '';
              };

              libDir = mkOption {
                type = types.str;
                readOnly = true;
                description = ''
                  Path to the SimpleSAMLphp library directory.
                '';
              };
              configDir = mkOption {
                type = types.str;
                readOnly = true;
                description = ''
                  Path to the SimpleSAMLphp config directory.
                '';
              };
            };
            config = {
              libDir = "${config.package}/share/php/simplesamlphp/";
              configDir = "${generateConfig config}";
            };
          }
        )
      );
      default = { };
      description = "Instances of SimpleSAMLphp. This module is designed to work with already existing PHP-FPM pool and NGINX virtualHost.";
    };

  config = {
    services.phpfpm.pools = lib.mapAttrs' (
      phpfpmName: opts:
      lib.nameValuePair opts.phpfpmPool { phpEnv.SIMPLESAMLPHP_CONFIG_DIR = "${generateConfig opts}"; }
    ) cfg;

    services.nginx.virtualHosts = lib.mapAttrs' (
      phpfpmName: opts:
      lib.nameValuePair opts.localDomain (
        lib.mkIf opts.configureNginx {
          locations."^~ /saml/" = {
            alias = "${opts.package}/share/php/simplesamlphp/www/";
            extraConfig = ''
                location ~ ^(?<prefix>/saml)(?<phpfile>.+?\.php)(?<pathinfo>/.*)?$ {
                  include ${pkgs.nginx}/conf/fastcgi.conf;
                  fastcgi_split_path_info  ^(.+\.php)(/.+)$;
                  fastcgi_pass  unix:${config.services.phpfpm.pools.${phpfpmName}.socket};
                  fastcgi_intercept_errors on;
                  fastcgi_param SCRIPT_FILENAME $document_root$phpfile;
                  fastcgi_param SCRIPT_NAME /saml$phpfile;
                  fastcgi_param PATH_INFO $pathinfo if_not_empty;
              }
            '';
          };
        }
      )
    ) cfg;
  };
}