|
| 1 | +{ config, pkgs, lib, ... }: |
| 2 | + |
| 3 | +let |
| 4 | + |
| 5 | + cfg = config.services.hydra-dev; |
| 6 | + |
| 7 | + baseDir = "/var/lib/hydra"; |
| 8 | + |
| 9 | + localDB = "dbi:Pg:dbname=hydra;user=hydra;"; |
| 10 | + |
| 11 | + haveLocalDB = cfg.dbi == localDB; |
| 12 | + |
| 13 | +in |
| 14 | + |
| 15 | +{ |
| 16 | + config = lib.mkIf cfg.enable { |
| 17 | + |
| 18 | + systemd.tmpfiles.rules = [ |
| 19 | + "d ${baseDir} 0750 hydra hydra" |
| 20 | + "d ${cfg.gcRootsDir} 2775 hydra hydra" |
| 21 | + ]; |
| 22 | + |
| 23 | + users.groups.hydra = { }; |
| 24 | + |
| 25 | + users.users.hydra = |
| 26 | + { description = "Hydra"; |
| 27 | + group = "hydra"; |
| 28 | + home = baseDir; |
| 29 | + isSystemUser = true; |
| 30 | + useDefaultShell = true; |
| 31 | + }; |
| 32 | + |
| 33 | + nix.settings = { |
| 34 | + keep-outputs = true; |
| 35 | + keep-derivations = true; |
| 36 | + }; |
| 37 | + |
| 38 | + systemd.services.hydra-init = |
| 39 | + { wantedBy = [ "multi-user.target" ]; |
| 40 | + |
| 41 | + after = lib.mkIf haveLocalDB [ |
| 42 | + # user won't exist until setup is done |
| 43 | + "postgresql-setup.service" |
| 44 | + # hydra-init accesses postgres |
| 45 | + "postgresql.service" |
| 46 | + ]; |
| 47 | + environment = { |
| 48 | + HYDRA_DBI = "${cfg.dbi};application_name=hydra-init"; |
| 49 | + HYDRA_CONFIG = "${baseDir}/hydra.conf"; |
| 50 | + HYDRA_DATA = baseDir; |
| 51 | + PGPASSFILE = "${baseDir}/pgpass"; |
| 52 | + }; |
| 53 | + path = [ pkgs.util-linux ]; |
| 54 | + preStart = '' |
| 55 | + ${lib.optionalString haveLocalDB '' |
| 56 | + echo "create extension if not exists pg_trgm" | runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/psql hydra |
| 57 | + ''} |
| 58 | +
|
| 59 | + if [ ! -e ${cfg.gcRootsDir} ]; then |
| 60 | + # Move legacy roots directory. |
| 61 | + if [ -e /nix/var/nix/gcroots/per-user/hydra/hydra-roots ]; then |
| 62 | + mv /nix/var/nix/gcroots/per-user/hydra/hydra-roots ${cfg.gcRootsDir} |
| 63 | + fi |
| 64 | + fi |
| 65 | +
|
| 66 | + # Move legacy hydra-www roots. |
| 67 | + if [ -e /nix/var/nix/gcroots/per-user/hydra-www/hydra-roots ]; then |
| 68 | + find /nix/var/nix/gcroots/per-user/hydra-www/hydra-roots/ -type f \ |
| 69 | + | xargs -r mv -f -t ${cfg.gcRootsDir}/ |
| 70 | + rmdir /nix/var/nix/gcroots/per-user/hydra-www/hydra-roots |
| 71 | + fi |
| 72 | + ''; |
| 73 | + serviceConfig = { |
| 74 | + ExecStart = "${cfg.package}/bin/hydra-init"; |
| 75 | + PermissionsStartOnly = true; |
| 76 | + User = "hydra"; |
| 77 | + Type = "oneshot"; |
| 78 | + RemainAfterExit = true; |
| 79 | + }; |
| 80 | + }; |
| 81 | + |
| 82 | + services.postgresql = lib.mkIf haveLocalDB { |
| 83 | + enable = true; |
| 84 | + ensureDatabases = [ "hydra" ]; |
| 85 | + ensureUsers = [ |
| 86 | + { |
| 87 | + name = "hydra"; |
| 88 | + ensureDBOwnership = true; |
| 89 | + } |
| 90 | + ]; |
| 91 | + identMap = '' |
| 92 | + hydra-users hydra hydra |
| 93 | + hydra-users root hydra |
| 94 | + # The postgres user is used to create the pg_trgm extension for the hydra database |
| 95 | + hydra-users postgres postgres |
| 96 | + ''; |
| 97 | + |
| 98 | + authentication = '' |
| 99 | + local hydra all ident map=hydra-users |
| 100 | + ''; |
| 101 | + }; |
| 102 | + }; |
| 103 | +} |
0 commit comments