Skip to content

Commit 1c1787b

Browse files
Ma27Mic92
authored andcommitted
Switch to new Nix bindings, update Nix for that
Implements support for Nix's new Perl bindings[1]. The current state basically does `openStore()`, but always uses `auto` and doesn't support stores at other URIs. Even though the stores are cached inside the Perl implementation, I decided to instantiate those once in the Nix helper module. That way store openings aren't cluttered across the entire codebase. Also, there are two stores used later on - MACHINE_LOCAL_STORE for `auto`, BINARY_CACHE_STORE for the one from `store_uri` in `hydra.conf` - and using consistent names should make the intent clearer then. This doesn't contain any behavioral changes, i.e. the build product availability issue from #1352 isn't fixed. This patch only contains the migration to the new API. [1] NixOS/nix#9863
1 parent c11bfdd commit 1c1787b

File tree

18 files changed

+50
-58
lines changed

18 files changed

+50
-58
lines changed

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hydra-eval-jobs/hydra-eval-jobs.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void worker(
185185
!experimentalFeatureSettings.isEnabled(Xp::CaDerivations));
186186

187187
if (drv->querySystem() == "unknown")
188-
throw EvalError("derivation must have a 'system' attribute");
188+
state.error<EvalError>("derivation must have a 'system' attribute").debugThrow();
189189

190190
auto drvPath = state.store->printStorePath(drv->requireDrvPath());
191191

@@ -208,7 +208,7 @@ static void worker(
208208
if (a && state.forceBool(*a->value, a->pos, "while evaluating the `_hydraAggregate` attribute")) {
209209
auto a = v->attrs->get(state.symbols.create("constituents"));
210210
if (!a)
211-
throw EvalError("derivation must have a ‘constituents’ attribute");
211+
state.error<EvalError>("derivation must have a ‘constituents’ attribute").debugThrow();
212212

213213
NixStringContext context;
214214
state.coerceToString(a->pos, *a->value, context, "while evaluating the `constituents` attribute", true, false);
@@ -274,7 +274,7 @@ static void worker(
274274
else if (v->type() == nNull)
275275
;
276276

277-
else throw TypeError("attribute '%s' is %s, which is not supported", attrPath, showType(*v));
277+
else state.error<TypeError>("attribute '%s' is %s, which is not supported", attrPath, showType(*v)).debugThrow();
278278

279279
} catch (EvalError & e) {
280280
auto msg = e.msg();

src/hydra-evaluator/hydra-evaluator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class JobsetId {
3838
friend bool operator!= (const JobsetId & lhs, const JobsetName & rhs);
3939

4040
std::string display() const {
41-
return str(format("%1%:%2% (jobset#%3%)") % project % jobset % id);
41+
return boost::str(boost::format("%1%:%2% (jobset#%3%)") % project % jobset % id);
4242
}
4343
};
4444
bool operator==(const JobsetId & lhs, const JobsetId & rhs)

src/hydra-queue-runner/queue-monitor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ bool State::getQueuedBuilds(Connection & conn,
298298
try {
299299
createBuild(build);
300300
} catch (Error & e) {
301-
e.addTrace({}, hintfmt("while loading build %d: ", build->id));
301+
e.addTrace({}, HintFmt("while loading build %d: ", build->id));
302302
throw;
303303
}
304304

src/lib/Hydra/Base/Controller/NixChannel.pm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use strict;
44
use warnings;
55
use base 'Hydra::Base::Controller::REST';
66
use List::SomeUtils qw(any);
7-
use Nix::Store;
87
use Hydra::Helper::Nix;
98
use Hydra::Helper::CatalystUtils;
109

@@ -30,7 +29,7 @@ sub getChannelData {
3029
my $outputs = {};
3130
foreach my $output (@outputs) {
3231
my $outPath = $output->get_column("outpath");
33-
next if $checkValidity && !isValidPath($outPath);
32+
next if $checkValidity && !$MACHINE_LOCAL_STORE->isValidPath($outPath);
3433
$outputs->{$output->get_column("outname")} = $outPath;
3534
push @storePaths, $outPath;
3635
# Put the system type in the manifest (for top-level

src/lib/Hydra/Controller/Build.pm

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use File::Basename;
1010
use File::LibMagic;
1111
use File::stat;
1212
use Data::Dump qw(dump);
13-
use Nix::Store;
14-
use Nix::Config;
1513
use List::SomeUtils qw(all);
1614
use Encode;
1715
use JSON::PP;
@@ -83,9 +81,9 @@ sub build_GET {
8381
# false because `$_->path` will be empty
8482
$c->stash->{available} =
8583
$c->stash->{isLocalStore}
86-
? all { $_->path && isValidPath($_->path) } $build->buildoutputs->all
84+
? all { $_->path && $MACHINE_LOCAL_STORE->isValidPath($_->path) } $build->buildoutputs->all
8785
: 1;
88-
$c->stash->{drvAvailable} = isValidPath $build->drvpath;
86+
$c->stash->{drvAvailable} = $MACHINE_LOCAL_STORE->isValidPath($build->drvpath);
8987

9088
if ($build->finished && $build->iscachedbuild) {
9189
my $path = ($build->buildoutputs)[0]->path or undef;
@@ -309,7 +307,7 @@ sub output : Chained('buildChain') PathPart Args(1) {
309307
error($c, "This build is not finished yet.") unless $build->finished;
310308
my $output = $build->buildoutputs->find({name => $outputName});
311309
notFound($c, "This build has no output named ‘$outputName") unless defined $output;
312-
gone($c, "Output is no longer available.") unless isValidPath $output->path;
310+
gone($c, "Output is no longer available.") unless $MACHINE_LOCAL_STORE->isValidPath($output->path);
313311

314312
$c->response->header('Content-Disposition', "attachment; filename=\"build-${\$build->id}-${\$outputName}.nar.bz2\"");
315313
$c->stash->{current_view} = 'NixNAR';
@@ -426,15 +424,15 @@ sub getDependencyGraph {
426424
};
427425
$$done{$path} = $node;
428426
my @refs;
429-
foreach my $ref (queryReferences($path)) {
427+
foreach my $ref ($MACHINE_LOCAL_STORE->queryReferences($path)) {
430428
next if $ref eq $path;
431429
next unless $runtime || $ref =~ /\.drv$/;
432430
getDependencyGraph($self, $c, $runtime, $done, $ref);
433431
push @refs, $ref;
434432
}
435433
# Show in reverse topological order to flatten the graph.
436434
# Should probably do a proper BFS.
437-
my @sorted = reverse topoSortPaths(@refs);
435+
my @sorted = reverse $MACHINE_LOCAL_STORE->topoSortPaths(@refs);
438436
$node->{refs} = [map { $$done{$_} } @sorted];
439437
}
440438

@@ -447,7 +445,7 @@ sub build_deps : Chained('buildChain') PathPart('build-deps') {
447445
my $build = $c->stash->{build};
448446
my $drvPath = $build->drvpath;
449447

450-
error($c, "Derivation no longer available.") unless isValidPath $drvPath;
448+
error($c, "Derivation no longer available.") unless $MACHINE_LOCAL_STORE->isValidPath($drvPath);
451449

452450
$c->stash->{buildTimeGraph} = getDependencyGraph($self, $c, 0, {}, $drvPath);
453451

@@ -462,7 +460,7 @@ sub runtime_deps : Chained('buildChain') PathPart('runtime-deps') {
462460

463461
requireLocalStore($c);
464462

465-
error($c, "Build outputs no longer available.") unless all { isValidPath($_) } @outPaths;
463+
error($c, "Build outputs no longer available.") unless all { $MACHINE_LOCAL_STORE->isValidPath($_) } @outPaths;
466464

467465
my $done = {};
468466
$c->stash->{runtimeGraph} = [ map { getDependencyGraph($self, $c, 1, $done, $_) } @outPaths ];
@@ -482,7 +480,7 @@ sub nix : Chained('buildChain') PathPart('nix') CaptureArgs(0) {
482480
if (isLocalStore) {
483481
foreach my $out ($build->buildoutputs) {
484482
notFound($c, "Path " . $out->path . " is no longer available.")
485-
unless isValidPath($out->path);
483+
unless $MACHINE_LOCAL_STORE->isValidPath($out->path);
486484
}
487485
}
488486

src/lib/Hydra/Controller/Root.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ sub narinfo :Path :Args(StrMatch[NARINFO_REGEX]) {
396396
my ($hash) = $narinfo =~ NARINFO_REGEX;
397397

398398
die("Hash length was not 32") if length($hash) != 32;
399-
my $path = queryPathFromHashPart($hash);
399+
my $path = $MACHINE_LOCAL_STORE->queryPathFromHashPart($hash);
400400

401401
if (!$path) {
402402
$c->response->status(404);

src/lib/Hydra/Helper/Nix.pm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ our @EXPORT = qw(
4040
registerRoot
4141
restartBuilds
4242
run
43+
$MACHINE_LOCAL_STORE
4344
);
4445

46+
our $MACHINE_LOCAL_STORE = Nix::Store->new();
47+
4548

4649
sub getHydraHome {
4750
my $dir = $ENV{"HYDRA_HOME"} or die "The HYDRA_HOME directory does not exist!\n";
@@ -494,7 +497,7 @@ sub restartBuilds {
494497
$builds = $builds->search({ finished => 1 });
495498

496499
foreach my $build ($builds->search({}, { columns => ["drvpath"] })) {
497-
next if !isValidPath($build->drvpath);
500+
next if !$MACHINE_LOCAL_STORE->isValidPath($build->drvpath);
498501
registerRoot $build->drvpath;
499502
}
500503

src/lib/Hydra/Plugin/BazaarInput.pm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use Digest::SHA qw(sha256_hex);
77
use File::Path;
88
use Hydra::Helper::Exec;
99
use Hydra::Helper::Nix;
10-
use Nix::Store;
1110

1211
sub supportedInputTypes {
1312
my ($self, $inputTypes) = @_;
@@ -38,9 +37,9 @@ sub fetchInput {
3837
(my $cachedInput) = $self->{db}->resultset('CachedBazaarInputs')->search(
3938
{uri => $uri, revision => $revision});
4039

41-
addTempRoot($cachedInput->storepath) if defined $cachedInput;
40+
$MACHINE_LOCAL_STORE->addTempRoot($cachedInput->storepath) if defined $cachedInput;
4241

43-
if (defined $cachedInput && isValidPath($cachedInput->storepath)) {
42+
if (defined $cachedInput && $MACHINE_LOCAL_STORE->isValidPath($cachedInput->storepath)) {
4443
$storePath = $cachedInput->storepath;
4544
$sha256 = $cachedInput->sha256hash;
4645
} else {
@@ -58,7 +57,7 @@ sub fetchInput {
5857
($sha256, $storePath) = split ' ', $stdout;
5958

6059
# FIXME: time window between nix-prefetch-bzr and addTempRoot.
61-
addTempRoot($storePath);
60+
$MACHINE_LOCAL_STORE->addTempRoot($storePath);
6261

6362
$self->{db}->txn_do(sub {
6463
$self->{db}->resultset('CachedBazaarInputs')->create(

src/lib/Hydra/Plugin/DarcsInput.pm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use Digest::SHA qw(sha256_hex);
77
use File::Path;
88
use Hydra::Helper::Exec;
99
use Hydra::Helper::Nix;
10-
use Nix::Store;
1110

1211
sub supportedInputTypes {
1312
my ($self, $inputTypes) = @_;
@@ -58,7 +57,7 @@ sub fetchInput {
5857
{uri => $uri, revision => $revision},
5958
{rows => 1});
6059

61-
if (defined $cachedInput && isValidPath($cachedInput->storepath)) {
60+
if (defined $cachedInput && $MACHINE_LOCAL_STORE->isValidPath($cachedInput->storepath)) {
6261
$storePath = $cachedInput->storepath;
6362
$sha256 = $cachedInput->sha256hash;
6463
$revision = $cachedInput->revision;
@@ -75,8 +74,8 @@ sub fetchInput {
7574
die "darcs changes --count failed" if $? != 0;
7675

7776
system "rm", "-rf", "$tmpDir/export/_darcs";
78-
$storePath = addToStore("$tmpDir/export", 1, "sha256");
79-
$sha256 = queryPathHash($storePath);
77+
$storePath = $MACHINE_LOCAL_STORE->addToStore("$tmpDir/export", 1, "sha256");
78+
$sha256 = $MACHINE_LOCAL_STORE->queryPathHash($storePath);
8079
$sha256 =~ s/sha256://;
8180

8281
$self->{db}->txn_do(sub {

0 commit comments

Comments
 (0)