From 69e7fc80ed0c61a7bf2aebec08aeaee3e63258cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=B7=F0=90=91=91=F0=90=91=B4=F0=90=91=95=F0=90=91=91?= =?UTF-8?q?=F0=90=91=A9=F0=90=91=A4?= Date: Sat, 13 Sep 2025 23:37:55 +0700 Subject: [PATCH 1/3] add EditorConfig --- .editorconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f2cba85 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# top-most EditorConfig file +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.nix] +indent_style = space +indent_width = 2 From 2f0311b408e333cc7866c2600e421f75fd060bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=B7=F0=90=91=91=F0=90=91=B4=F0=90=91=95=F0=90=91=91?= =?UTF-8?q?=F0=90=91=A9=F0=90=91=A4?= Date: Sun, 14 Sep 2025 00:52:26 +0700 Subject: [PATCH 2/3] doc the dist --- flake-module.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/flake-module.nix b/flake-module.nix index f96057c..e3b884e 100644 --- a/flake-module.nix +++ b/flake-module.nix @@ -20,7 +20,12 @@ in { tests = mkOption { type = types.attrsOf (types.submodule { options = { - dist = mkOption { type = types.path; }; + dist = mkOption { + type = types.path; + description = '' + Path to static directory to test. Great for static site generators. + ''; + }; categories = mkOption { default = { }; From 31251f1c4ff56fdcc3920498156505f2c7b1b9e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=B7=F0=90=91=91=F0=90=91=B4=F0=90=91=95=F0=90=91=91?= =?UTF-8?q?=F0=90=91=A9=F0=90=91=A4?= Date: Sat, 13 Sep 2025 23:50:12 +0700 Subject: [PATCH 3/3] factor out nixos-test.nix --- flake-module.nix | 112 +++-------------------------------------------- nixos-test.nix | 98 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 105 deletions(-) create mode 100644 nixos-test.nix diff --git a/flake-module.nix b/flake-module.nix index e3b884e..a946dc4 100644 --- a/flake-module.nix +++ b/flake-module.nix @@ -73,112 +73,14 @@ in { config = { perSystem = { config, pkgs, ... }: - let + { checks = lib.mapAttrs (name: test: - pkgs.nixosTest { - inherit name; - nodes = { - server = { pkgs, config, ... }: { - virtualisation.cores = 4; - - environment.variables.CHROME_PATH = - lib.getExe pkgs.ungoogled-chromium; - - services.h2o = { - enable = true; - settings = { - compress = "ON"; - http2-reprioritize-blocking-assets = "ON"; - ssl-offload = "kernel"; - }; - hosts."acme.test" = { - tls = { - recommendations = "modern"; - policy = "force"; - quic = { }; - identity = [ - { - key-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.key.pem"; - certificate-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.cert.pem"; - } - ]; - extraSettings = { - # when using common ACME certs, disable talking to CA - ocsp-update-interval = 0; - }; - }; - settings = { - paths."/" = { - "file.dir" = test.dist; - }; - }; - }; - }; - - networking = { - firewall = { - allowedTCPPorts = [ config.services.h2o.defaultTLSListenPort ]; - allowedUDPPorts = [ config.services.h2o.defaultTLSListenPort ]; - }; - extraHosts = '' - 127.0.0.1 acme.test - ''; - }; - # Required to make TLS work - security.pki.certificates = [ - (builtins.readFile (pkgs.path + "/nixos/tests/common/acme/server/ca.cert.pem")) - ]; - }; - }; - - testScript = { nodes, ... }: '' - import json - import os - - start_all() - server.wait_for_open_port(${builtins.toString nodes.server.services.h2o.defaultTLSListenPort}) - server.wait_for_unit("h2o.service") - - report_path = "/tmp/lighthouse-report.json" - - server.succeed("CI=1 ${lib.getExe config.lighthouse.package} https://acme.test --output json --output-path {} --only-categories accessibility,best-practices,performance,seo --skip-audits valid-source-maps --chrome-flags=\"--headless --no-sandbox\"".format(report_path)) - - server.copy_from_vm(report_path) - - with open("{}/lighthouse-report.json".format(os.environ["out"]), "r") as f: - report = json.load(f) - categories = report["categories"] - - performance_score = categories["performance"]["score"] - assert performance_score >= ${ - toString test.categories.performance - }, "performance score should be at least ${ - toString test.categories.performance - }, but it was {}".format(performance_score) - - accessibility_score = categories["accessibility"]["score"] - assert accessibility_score >= ${ - toString test.categories.accessibility - }, "accessibility score should be at least ${ - toString test.categories.accessibility - }, but it was {}".format(accessibility_score) - - seo_score = categories["seo"]["score"] - assert seo_score >= ${ - toString test.categories.seo - }, "seo score should be at least ${ - toString test.categories.seo - }, but it was {}".format(seo_score) - - best_practices_score = categories["best-practices"]["score"] - assert best_practices_score >= ${ - toString test.categories.bestPractices - }, "best-practices score should be at least ${ - toString (test.categories.bestPractices) - }, but it was {}".format(best_practices_score) - ''; - }) config.lighthouse.tests; - in { inherit checks; }; + pkgs.callPackage ./nixos-test.nix { + inherit name test; + google-lighthouse = config.lighthouse.package; + } + ) config.lighthouse.tests; + }; }; } diff --git a/nixos-test.nix b/nixos-test.nix new file mode 100644 index 0000000..a1bb58b --- /dev/null +++ b/nixos-test.nix @@ -0,0 +1,98 @@ +{ + lib, + nixosTest, + google-lighthouse, + name, + test, +}: + +nixosTest { + inherit name; + nodes = { + server = + { pkgs, config, ... }: + { + virtualisation.cores = 4; + + environment.variables.CHROME_PATH = lib.getExe pkgs.ungoogled-chromium; + + services.h2o = { + enable = true; + settings = { + compress = "ON"; + http2-reprioritize-blocking-assets = "ON"; + ssl-offload = "kernel"; + }; + hosts."acme.test" = { + tls = { + recommendations = "modern"; + policy = "force"; + quic = { }; + identity = [ + { + key-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.key.pem"; + certificate-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.cert.pem"; + } + ]; + extraSettings = { + # when using common ACME certs, disable talking to CA + ocsp-update-interval = 0; + }; + }; + settings = { + paths."/" = { + "file.dir" = test.dist; + }; + }; + }; + }; + + networking = { + firewall = { + allowedTCPPorts = [ config.services.h2o.defaultTLSListenPort ]; + allowedUDPPorts = [ config.services.h2o.defaultTLSListenPort ]; + }; + extraHosts = '' + 127.0.0.1 acme.test + ''; + }; + # Required to make TLS work + security.pki.certificates = [ + (builtins.readFile (pkgs.path + "/nixos/tests/common/acme/server/ca.cert.pem")) + ]; + }; + }; + + testScript = + { nodes, ... }: # python + '' + import json + import os + + start_all() + server.wait_for_open_port(${builtins.toString nodes.server.services.h2o.defaultTLSListenPort}) + server.wait_for_unit("h2o.service") + + report_path = "/tmp/lighthouse-report.json" + + server.succeed("CI=1 ${lib.getExe google-lighthouse} https://acme.test --output json --output-path {} --only-categories accessibility,best-practices,performance,seo --skip-audits valid-source-maps --chrome-flags=\"--headless --no-sandbox\"".format(report_path)) + + server.copy_from_vm(report_path) + + with open("{}/lighthouse-report.json".format(os.environ["out"]), "r") as f: + report = json.load(f) + categories = report["categories"] + + performance_score = categories["performance"]["score"] + assert performance_score >= ${toString test.categories.performance}, "performance score should be at least ${toString test.categories.performance}, but it was {}".format(performance_score) + + accessibility_score = categories["accessibility"]["score"] + assert accessibility_score >= ${toString test.categories.accessibility}, "accessibility score should be at least ${toString test.categories.accessibility}, but it was {}".format(accessibility_score) + + seo_score = categories["seo"]["score"] + assert seo_score >= ${toString test.categories.seo}, "seo score should be at least ${toString test.categories.seo}, but it was {}".format(seo_score) + + best_practices_score = categories["best-practices"]["score"] + assert best_practices_score >= ${toString test.categories.bestPractices}, "best-practices score should be at least ${toString (test.categories.bestPractices)}, but it was {}".format(best_practices_score) + ''; +}