Skip to content

Commit aedf068

Browse files
authored
Merge pull request #1586 from obsidiansystems/module-cleanup
nixosModules: Clean up services in a few ways
2 parents a2ed6df + 8f92e6c commit aedf068

7 files changed

Lines changed: 184 additions & 159 deletions

File tree

nixos-modules/check-space.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
spaceleft=$(($(stat -f -c '%a' /nix/store) * $(stat -f -c '%S' /nix/store)))
2+
spacestopstart() {
3+
service=$1
4+
minFreeGB=$2
5+
if [ $spaceleft -lt $(($minFreeGB * 1024**3)) ]; then
6+
if [ $(systemctl is-active $service) == active ]; then
7+
echo "stopping $service due to lack of free space..."
8+
systemctl stop $service
9+
date > /var/lib/hydra/.$service-stopped-minspace
10+
fi
11+
else
12+
if [ $spaceleft -gt $(( ($minFreeGB + 10) * 1024**3)) -a \
13+
-r /var/lib/hydra/.$service-stopped-minspace ] ; then
14+
rm /var/lib/hydra/.$service-stopped-minspace
15+
echo "restarting $service due to newly available free space..."
16+
systemctl start $service
17+
fi
18+
fi
19+
}

nixos-modules/default.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ rec {
88
lib.mkDefault flakePackages.${pkgs.stdenv.hostPlatform.system}.hydra;
99
};
1010

11+
postgresql = ./postgresql.nix;
12+
1113
queue-runner = { pkgs, lib, ... }: {
1214
_file = ./default.nix;
1315
imports = [ ./queue-runner-module.nix ];
@@ -43,6 +45,12 @@ rec {
4345
services.hydra-dev.hydraURL = "http://hydra.example.org";
4446
services.hydra-dev.notificationSender = "admin@hydra.example.org";
4547

48+
services.hydra-queue-runner-dev.enable = true;
49+
50+
services.hydra-queue-builder-dev.enable = true;
51+
services.hydra-queue-builder-dev.queueRunnerAddr = "http://[::1]:50051";
52+
systemd.services.hydra-queue-builder-dev.after = [ "hydra-queue-runner-dev.service" ];
53+
4654
systemd.services.hydra-send-stats.enable = false;
4755

4856
services.postgresql.enable = true;

nixos-modules/linux-builder-module.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ in
157157
HOME = "/run/hydra-queue-builder";
158158
};
159159

160+
path = [ config.nix.package ];
161+
160162
serviceConfig = {
161163
Type = "notify";
162164
Restart = "always";
@@ -271,7 +273,6 @@ in
271273
];
272274
nix = {
273275
settings = {
274-
allowed-users = [ "hydra-queue-builder" ];
275276
trusted-users = [ "hydra-queue-builder" ];
276277
experimental-features = [ "nix-command" ];
277278
};

nixos-modules/postgresql.nix

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

nixos-modules/queue-runner-module.nix

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ in
211211
);
212212
};
213213

214+
minimumDiskFree = lib.mkOption {
215+
type = lib.types.int;
216+
default = 0;
217+
description = ''
218+
Threshold of minimum disk space (GiB) to determine if the queue runner should run or not.
219+
'';
220+
};
221+
214222
package = lib.mkOption {
215223
type = lib.types.package;
216224
default = pkgs.callPackage ./. { };
@@ -224,8 +232,10 @@ in
224232

225233
requires = [ "nix-daemon.socket" ];
226234
after = [
235+
# sets up database, queue-runner crashes if schema is incorrect
236+
"hydra-init.service"
237+
# queue-runner may need to connect to another machine
227238
"network.target"
228-
"postgresql.service"
229239
];
230240
wantedBy = [ "multi-user.target" ];
231241
reloadTriggers = [ config.environment.etc."hydra/queue-runner.toml".source ];
@@ -317,6 +327,20 @@ in
317327
};
318328
};
319329

330+
# If there is less than a certain amount of free disk space, stop
331+
# the queue to prevent builds from failing or aborting.
332+
# Leaves a tag file indicating this reason; if the tag file exists
333+
# and disk space is above the threshold + 10GB, the queue will be
334+
# restarted; starting it if it is already started is not harmful.
335+
systemd.services.hydra-queue-runner-check-space =
336+
{ script =
337+
''
338+
${builtins.readFile ./check-space.sh}
339+
spacestopstart hydra-queue-runner-dev ${toString cfg.minimumDiskFree}
340+
'';
341+
startAt = "*:0/5";
342+
};
343+
320344
environment.etc."hydra/queue-runner.toml".source = format.generate "queue-runner.toml" (
321345
lib.filterAttrsRecursive (_: v: v != null) cfg.settings
322346
);
@@ -325,6 +349,10 @@ in
325349
"d /var/lib/hydra/build-logs/ 0755 hydra-queue-runner hydra -"
326350
];
327351

352+
services.postgresql.identMap = ''
353+
hydra-users hydra-queue-runner hydra
354+
'';
355+
328356
users = {
329357
groups.hydra = { };
330358
users.hydra-queue-runner = {

0 commit comments

Comments
 (0)