Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve testing infrastructure #61

Merged
merged 9 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: "Test"
on:
pull_request:
push:
branches:
- main
jobs:
tests:
strategy:
Expand All @@ -11,7 +13,5 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: cachix/install-nix-action@v23
- run: nix-build -A checks.$(nix-instantiate --eval -E '(builtins.currentSystem)')
- uses: cachix/install-nix-action@V28
- run: nix flake check
14 changes: 14 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
queue_rules:
- name: default
merge_conditions:
- check-success=tests (macos-latest)
- check-success=tests (ubuntu-latest)
batch_size: 5
merge_method: rebase
pull_request_rules:
- name: merge using the merge queue
conditions:
- base=main
- label~=merge-queue|dependencies
actions:
queue: {}
53 changes: 24 additions & 29 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,41 @@

let
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
systems = [ "x86_64-linux" "aarch64-linux" "i686-linux" "x86_64-darwin" ];
systems = [
"x86_64-linux"
"aarch64-linux"
"i686-linux"
"x86_64-darwin"
"aarch64-darwin"
];
in {

overlay = final: prev: {

nix-serve = with final; stdenv.mkDerivation {
name = "nix-serve-${self.lastModifiedDate}";

buildInputs = [ perl nixVersions.latest.perl-bindings perlPackages.Plack perlPackages.Starman perlPackages.DBDSQLite ];

unpackPhase = "true";

installPhase =
''
mkdir -p $out/libexec/nix-serve
cp ${./nix-serve.psgi} $out/libexec/nix-serve/nix-serve.psgi

mkdir -p $out/bin
cat > $out/bin/nix-serve <<EOF
#! ${stdenv.shell}
PERL5LIB=$PERL5LIB \
NIX_REMOTE="\''${NIX_REMOTE:-auto?path-info-cache-size=0}" \
exec ${perlPackages.Starman}/bin/starman --preload-app $out/libexec/nix-serve/nix-serve.psgi "\$@"
EOF
chmod +x $out/bin/nix-serve
'';
nix-serve = final.pkgs.callPackage ./package.nix {
inherit self;
nix = final.nixVersions.git;
};

};

packages = forAllSystems (system: {
nix-serve = (import nixpkgs { inherit system; overlays = [ self.overlay ]; }).nix-serve;
packages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
nix-serve = nixpkgs.legacyPackages.${system}.callPackage ./package.nix {
inherit self;
nix = pkgs.nixVersions.git;
};
});

defaultPackage = forAllSystems (system: self.packages.${system}.nix-serve);

checks = forAllSystems (system: {
checks = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
build = self.defaultPackage.${system};
# FIXME: add a proper test.
} // nixpkgs.lib.optionalAttrs (pkgs.stdenv.isLinux) {
nixos-test = pkgs.callPackage ./nixos-test.nix {
nix-serve = self.defaultPackage.${system};
};
Mic92 marked this conversation as resolved.
Show resolved Hide resolved
});

};
}
6 changes: 3 additions & 3 deletions nix-serve.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ my $app = sub {
return [404, ['Content-Type' => 'text/plain'], ["Incorrect NAR hash. Maybe the path has been recreated.\n"]]
unless $narHash eq "sha256:$expectedNarHash";
my $fh = new IO::Handle;
open $fh, "-|", "nix", "dump-path", "--", $storePath;
open $fh, "-|", "nix", "--extra-experimental-features", "nix-command", "dump-path", "--", $storePath;
return [200, ['Content-Type' => 'text/plain', 'Content-Length' => $narSize], $fh];
}

Expand All @@ -75,14 +75,14 @@ my $app = sub {
return [404, ['Content-Type' => 'text/plain'], ["No such path.\n"]] unless $storePath;
my ($deriver, $narHash, $time, $narSize, $refs) = $store->queryPathInfo($storePath, 1) or die;
my $fh = new IO::Handle;
open $fh, "-|", "nix", "dump-path", "--", $storePath;
open $fh, "-|", "nix", "--extra-experimental-features", "nix-command", "dump-path", "--", $storePath;
return [200, ['Content-Type' => 'text/plain', 'Content-Length' => $narSize], $fh];
}

elsif ($path =~ /^\/log\/([0-9a-z]+-[0-9a-zA-Z\+\-\.\_\?\=]+)/) {
my $storePath = "$Nix::Config::storeDir/$1";
my $fh = new IO::Handle;
open $fh, "-|", "nix", "log", $storePath;
open $fh, "-|", "nix", "--extra-experimental-features", "nix-command", "log", $storePath;
return [200, ['Content-Type' => 'text/plain' ], $fh];
}

Expand Down
26 changes: 26 additions & 0 deletions nixos-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{ hello, testers, nix-serve }:

testers.runNixOSTest {
Mic92 marked this conversation as resolved.
Show resolved Hide resolved
name = "nix-serve";
nodes.machine =
{ pkgs, ... }:
{
services.nix-serve.enable = true;
services.nix-serve.package = nix-serve;
environment.systemPackages = [
pkgs.hello
];
};
testScript =
let
pkgHash = builtins.head (builtins.match "${builtins.storeDir}/([^-]+).+" (toString hello));
in
''
start_all()
machine.wait_for_unit("nix-serve.service")
machine.wait_for_open_port(5000)
machine.succeed(
"curl --fail -g http://0.0.0.0:5000/nar/${pkgHash}.nar -o /tmp/hello.nar"
)
'';
}
35 changes: 35 additions & 0 deletions package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
stdenv,
perl,
perlPackages,
nix,
self,
}:

stdenv.mkDerivation {
name = "nix-serve-${self.lastModifiedDate}";

buildInputs = [
perl
nix.perl-bindings
perlPackages.Plack
perlPackages.Starman
perlPackages.DBDSQLite
];

unpackPhase = "true";

installPhase = ''
mkdir -p $out/libexec/nix-serve
cp ${./nix-serve.psgi} $out/libexec/nix-serve/nix-serve.psgi

mkdir -p $out/bin
cat > $out/bin/nix-serve <<EOF
#! ${stdenv.shell}
PERL5LIB=$PERL5LIB \
NIX_REMOTE="\''${NIX_REMOTE:-auto?path-info-cache-size=0}" \
exec ${perlPackages.Starman}/bin/starman --preload-app $out/libexec/nix-serve/nix-serve.psgi "\$@"
EOF
chmod +x $out/bin/nix-serve
'';
}