Skip to content

Commit b741c38

Browse files
hydra: add configurePostgres option
1 parent cb0c5e3 commit b741c38

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

modules/hydra.nix

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
{ config, lib, libS, ... }:
22

33
let
4-
cfg = config.services.hydra.ldap;
4+
cfg = config.services.hydra;
5+
cfgl = cfg.ldap;
56
inherit (config.security) ldap;
67
in
78
{
89
options = {
9-
services.hydra.ldap = {
10-
enable = lib.mkEnableOption ''
11-
login only via LDAP.
12-
The bind user password must be placed at `/var/lib/hydra/ldap-password.conf` in the format `bindpw = "PASSWORD"
13-
It is recommended to use a password without special characters because the perl config parser has weird escaping rule like that comment characters `#` must be escape with backslash
14-
'';
15-
16-
roleMappings = lib.mkOption {
17-
type = with lib.types; listOf (attrsOf str);
18-
example = [{ hydra-admins = "admins"; }];
19-
default = [ ];
20-
description = "Map LDAP groups to hydra permissions. See upstream doc, especially role_mapping.";
10+
services.hydra = {
11+
configurePostgres = lib.mkOption {
12+
type = lib.types.bool;
13+
default = false;
14+
example = true;
15+
description = "Whether to configure and create a local PostgreSQL database.";
2116
};
2217

23-
userGroup = libS.ldap.mkUserGroupOption;
18+
ldap = {
19+
enable = lib.mkEnableOption ''
20+
login only via LDAP.
21+
The bind user password must be placed at `/var/lib/hydra/ldap-password.conf` in the format `bindpw = "PASSWORD"
22+
It is recommended to use a password without special characters because the perl config parser has weird escaping rule
23+
like that comment characters `#` must be escape with backslash
24+
'';
25+
26+
roleMappings = lib.mkOption {
27+
type = with lib.types; listOf (attrsOf str);
28+
example = [{ hydra-admins = "admins"; }];
29+
default = [ ];
30+
description = "Map LDAP groups to hydra permissions. See upstream doc, especially role_mapping.";
31+
};
32+
33+
userGroup = libS.ldap.mkUserGroupOption;
34+
};
2435
};
2536
};
2637

27-
config.services.hydra.extraConfig = lib.mkIf cfg.enable /* xml */ ''
38+
config.services.hydra.extraConfig = lib.mkIf cfgl.enable /* xml */ ''
2839
# https://hydra.nixos.org/build/196107287/download/1/hydra/configuration.html#using-ldap-as-authentication-backend-optional
2940
<ldap>
3041
<config>
@@ -48,7 +59,7 @@ in
4859
sslversion = tlsv1_3
4960
</start_tls_options>
5061
user_basedn = "${ldap.userBaseDN}"
51-
user_filter = "${ldap.searchFilterWithGroupFilter cfg.userGroup (ldap.userFilter "%s")}"
62+
user_filter = "${ldap.searchFilterWithGroupFilter cfgl.userGroup (ldap.userFilter "%s")}"
5263
user_scope = one
5364
user_field = ${ldap.userField}
5465
<user_search_options>
@@ -72,15 +83,15 @@ in
7283
# Allow all users in the dev group to restart jobs and cancel builds
7384
# dev = restart-jobs
7485
# dev = cancel-build
75-
${lib.concatStringsSep "\n" (lib.concatMap (lib.mapAttrsToList (name: value: "${name} = ${value}")) cfg.roleMappings)}
86+
${lib.concatStringsSep "\n" (lib.concatMap (lib.mapAttrsToList (name: value: "${name} = ${value}")) cfgl.roleMappings)}
7687
</role_mapping>
7788
</ldap>
7889
'';
7990

8091
config.services.portunus.seedSettings.groups = [
81-
(lib.mkIf (cfg.userGroup != null) {
92+
(lib.mkIf (cfgl.userGroup != null) {
8293
long_name = "Hydra Users";
83-
name = cfg.userGroup;
94+
name = cfgl.userGroup;
8495
permissions = { };
8596
})
8697
] ++ lib.flatten (map lib.attrValues (map
@@ -89,5 +100,13 @@ in
89100
name = ldapGroup;
90101
permissions = { };
91102
}))
92-
cfg.roleMappings));
103+
cfgl.roleMappings));
104+
105+
config.services.postgresql = lib.mkIf cfg.configurePostgres {
106+
ensureDatabases = [ "hydra" ];
107+
ensureUsers = [ {
108+
name = "hydra";
109+
ensureDBOwnership = true;
110+
} ];
111+
};
93112
}

0 commit comments

Comments
 (0)