From 4349affc1a5cd93fb0f38b138457c910f73884fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Fri, 5 Jul 2024 14:23:51 +0200 Subject: [PATCH 1/4] flake: update --- flake.lock | 224 +++++++++++++++++------------------------------------ 1 file changed, 69 insertions(+), 155 deletions(-) diff --git a/flake.lock b/flake.lock index 6c8591e..7f65563 100644 --- a/flake.lock +++ b/flake.lock @@ -1,36 +1,33 @@ { "nodes": { - "devshell": { + "crab-fit": { "flake": false, "locked": { - "lastModified": 1663445644, - "narHash": "sha256-+xVlcK60x7VY1vRJbNUEAHi17ZuoQxAIH4S4iUFUGBA=", - "owner": "numtide", - "repo": "devshell", - "rev": "e3dc3e21594fe07bdb24bdf1c8657acaa4cb8f66", + "lastModified": 1690982468, + "narHash": "sha256-jy8BrJSHukRenPbZHw4nPx3cSi7E2GSg//WOXDh90mY=", + "owner": "GRA0007", + "repo": "crab.fit", + "rev": "628f9eefc300bf1ed3d6cc3323332c2ed9b8a350", "type": "github" }, "original": { - "owner": "numtide", - "repo": "devshell", + "owner": "GRA0007", + "repo": "crab.fit", "type": "github" } }, "dream2nix": { "inputs": { - "devshell": "devshell", - "flake-compat": "flake-compat", - "flake-parts": "flake-parts", - "nix-unit": "nix-unit", "nixpkgs": "nixpkgs", - "pre-commit-hooks": "pre-commit-hooks" + "purescript-overlay": "purescript-overlay", + "pyproject-nix": "pyproject-nix" }, "locked": { - "lastModified": 1697064937, - "narHash": "sha256-yZ5OlQkPnNUDnEzUY/Su2gu52i1+w7h5HHK/kBk5oUg=", + "lastModified": 1718908638, + "narHash": "sha256-Z2TjBnWCtODqt8oiL35lWFJAkEQKTQF4J3WN5NeG+OA=", "owner": "nix-community", "repo": "dream2nix", - "rev": "1caf997f694c3144ed8fa6752c4af36d1e68851c", + "rev": "5e523a4e419a31dedae61443b9f78397e64eeea9", "type": "github" }, "original": { @@ -41,7 +38,9 @@ }, "dreampkgs": { "inputs": { + "crab-fit": "crab-fit", "dream2nix": "dream2nix", + "logchecker": "logchecker", "nixpkgs": [ "dreampkgs", "dream2nix", @@ -49,206 +48,121 @@ ] }, "locked": { - "lastModified": 1697460745, - "narHash": "sha256-NcHqso6zClPIFPImLp2KgCCR6pIjPR5Mg7+E0ab0dag=", - "owner": "marijanp", + "lastModified": 1719730046, + "narHash": "sha256-29g2YltjCkziAcsLq9wtNjZ3EC+gUTrhf73WUEDNiSM=", + "owner": "nix-community", "repo": "dreampkgs", - "rev": "fe55c40a0fb1652d5ef5cc74d9d86d0249c6d244", + "rev": "dde9480b9112374b465a6a2c1d6e94474c8b3988", "type": "github" }, "original": { - "owner": "marijanp", - "ref": "add-lighthouse", + "owner": "nix-community", "repo": "dreampkgs", "type": "github" } }, - "flake-compat": { + "logchecker": { "flake": false, "locked": { - "lastModified": 1673956053, - "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", - "owner": "edolstra", - "repo": "flake-compat", - "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "lastModified": 1693771389, + "narHash": "sha256-o0Pw/w+NKeL6haB+gObTK2xMJ78ZuOj7lmoJruaAuWQ=", + "owner": "OPSnet", + "repo": "Logchecker", + "rev": "e71120ed6e39b69b69d6e30e0d2d4badef8fd1be", "type": "github" }, "original": { - "owner": "edolstra", - "repo": "flake-compat", + "owner": "OPSnet", + "ref": "0.11.1", + "repo": "Logchecker", "type": "github" } }, - "flake-parts": { - "inputs": { - "nixpkgs-lib": [ - "dreampkgs", - "dream2nix", - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1675933616, - "narHash": "sha256-/rczJkJHtx16IFxMmAWu5nNYcSXNg1YYXTHoGjLrLUA=", - "owner": "hercules-ci", - "repo": "flake-parts", - "rev": "47478a4a003e745402acf63be7f9a092d51b83d7", - "type": "github" - }, - "original": { - "owner": "hercules-ci", - "repo": "flake-parts", - "type": "github" - } - }, - "flake-utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1689068808, - "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "nix-github-actions": { - "inputs": { - "nixpkgs": [ - "dreampkgs", - "dream2nix", - "nix-unit", - "nixpkgs" - ] - }, + "nixpkgs": { "locked": { - "lastModified": 1688870561, - "narHash": "sha256-4UYkifnPEw1nAzqqPOTL2MvWtm3sNGw1UTYTalkTcGY=", - "owner": "nix-community", - "repo": "nix-github-actions", - "rev": "165b1650b753316aa7f1787f3005a8d2da0f5301", + "lastModified": 1718742829, + "narHash": "sha256-+H7PnuwEDDDxUMa3ItAcSRDK5+jfMJap/zHiuACyIfc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "37a45fb6993f14555f50b18fbcf4945b82a35707", "type": "github" }, "original": { - "owner": "nix-community", - "repo": "nix-github-actions", + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", "type": "github" } }, - "nix-unit": { + "purescript-overlay": { "inputs": { - "flake-parts": [ - "dreampkgs", - "dream2nix", - "flake-parts" - ], - "nix-github-actions": "nix-github-actions", "nixpkgs": [ "dreampkgs", "dream2nix", "nixpkgs" ], - "treefmt-nix": "treefmt-nix" + "slimlock": "slimlock" }, "locked": { - "lastModified": 1694670962, - "narHash": "sha256-HvMq0TJGYSx37zHm4j2d+JUZx4/6X7xKEt/0DeCiwjQ=", - "owner": "adisbladis", - "repo": "nix-unit", - "rev": "3ed2378bddad85257fc508a291408f9ed9673d01", + "lastModified": 1696022621, + "narHash": "sha256-eMjFmsj2G1E0Q5XiibUNgFjTiSz0GxIeSSzzVdoN730=", + "owner": "thomashoneyman", + "repo": "purescript-overlay", + "rev": "047c7933abd6da8aa239904422e22d190ce55ead", "type": "github" }, "original": { - "owner": "adisbladis", - "repo": "nix-unit", + "owner": "thomashoneyman", + "repo": "purescript-overlay", "type": "github" } }, - "nixpkgs": { + "pyproject-nix": { + "flake": false, "locked": { - "lastModified": 1696604326, - "narHash": "sha256-YXUNI0kLEcI5g8lqGMb0nh67fY9f2YoJsILafh6zlMo=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "87828a0e03d1418e848d3dd3f3014a632e4a4f64", + "lastModified": 1702448246, + "narHash": "sha256-hFg5s/hoJFv7tDpiGvEvXP0UfFvFEDgTdyHIjDVHu1I=", + "owner": "davhau", + "repo": "pyproject.nix", + "rev": "5a06a2697b228c04dd2f35659b4b659ca74f7aeb", "type": "github" }, "original": { - "id": "nixpkgs", - "ref": "nixos-unstable", - "type": "indirect" + "owner": "davhau", + "ref": "dream2nix", + "repo": "pyproject.nix", + "type": "github" } }, - "pre-commit-hooks": { + "root": { "inputs": { - "flake-utils": "flake-utils", + "dreampkgs": "dreampkgs", "nixpkgs": [ "dreampkgs", - "dream2nix", "nixpkgs" ] - }, - "locked": { - "lastModified": 1646153636, - "narHash": "sha256-AlWHMzK+xJ1mG267FdT8dCq/HvLCA6jwmx2ZUy5O8tY=", - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "b6bc0b21e1617e2b07d8205e7fae7224036dfa4b", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, - "root": { - "inputs": { - "dreampkgs": "dreampkgs" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" } }, - "treefmt-nix": { + "slimlock": { "inputs": { "nixpkgs": [ "dreampkgs", "dream2nix", - "nix-unit", + "purescript-overlay", "nixpkgs" ] }, "locked": { - "lastModified": 1689620039, - "narHash": "sha256-BtNwghr05z7k5YMdq+6nbue+nEalvDepuA7qdQMAKoQ=", - "owner": "numtide", - "repo": "treefmt-nix", - "rev": "719c2977f958c41fa60a928e2fbc50af14844114", + "lastModified": 1688610262, + "narHash": "sha256-Wg0ViDotFWGWqKIQzyYCgayeH8s4U1OZcTiWTQYdAp4=", + "owner": "thomashoneyman", + "repo": "slimlock", + "rev": "b5c6cdcaf636ebbebd0a1f32520929394493f1a6", "type": "github" }, "original": { - "owner": "numtide", - "repo": "treefmt-nix", + "owner": "thomashoneyman", + "repo": "slimlock", "type": "github" } } From 38e17d1e25555186bf6ecb108c512d0b52135d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Fri, 5 Jul 2024 14:24:19 +0200 Subject: [PATCH 2/4] remove mdDoc, format --- flake-module.nix | 227 ++++++++++++++++++++++++----------------------- 1 file changed, 116 insertions(+), 111 deletions(-) diff --git a/flake-module.nix b/flake-module.nix index a3c83d5..fabfcd7 100644 --- a/flake-module.nix +++ b/flake-module.nix @@ -1,139 +1,144 @@ -{ self, lib, flake-parts-lib, ... }: +{ lib, flake-parts-lib, ... }: let - inherit (flake-parts-lib) - mkPerSystemOption; - inherit (lib) - mdDoc - mkOption - types - warn; -in -{ + inherit (flake-parts-lib) mkPerSystemOption; + inherit (lib) mkOption types warn; +in { options = { - perSystem = mkPerSystemOption - ({ config, self', inputs', pkgs, system, ... }: { - options.lighthouse = mkOption { - default = warn "lighthouse-flake module was imported but not used" { }; - description = mdDoc '' - **lighthouse-flake**: Creates a Google lighthouse test-run for each of the defined test attributes, - which can be configured by setting the to be tested dist directory and specifying the criteria. - Checks **succeed** or **fail** depending on whether the defined **minimal score** for each of the criteria - is reached. - ''; - type = types.submodule { - options = { - package = mkOption { - type = types.package; - }; + perSystem = mkPerSystemOption ({ ... }: { + options.lighthouse = mkOption { + default = warn "lighthouse-flake module was imported but not used" { }; + description = '' + **lighthouse-flake**: Creates a Google lighthouse test-run for each of the defined test attributes, + which can be configured by setting the to be tested dist directory and specifying the criteria. + Checks **succeed** or **fail** depending on whether the defined **minimal score** for each of the criteria + is reached. + ''; + type = types.submodule { + options = { + package = mkOption { type = types.package; }; - tests = mkOption { - type = types.attrsOf (types.submodule { - options = { - dist = mkOption { - type = types.path; - }; + tests = mkOption { + type = types.attrsOf (types.submodule { + options = { + dist = mkOption { type = types.path; }; - categories = mkOption { - default = { }; - type = types.submodule { - options = { - performance = mkOption { - type = types.numbers.between 0 1; - default = 0.9; - description = mdDoc '' - The minimal score that should be reached for *performance*. - ''; - }; - accessibility = mkOption { - type = types.numbers.between 0 1; - default = 0.9; - description = mdDoc '' - The minimal score that should be reached for *accessibility*. - ''; - }; - seo = mkOption { - type = types.numbers.between 0 1; - default = 0.9; - description = mdDoc '' - The minimal score that should be reached for *SEO*. - ''; - }; - bestPractices = mkOption { - type = types.numbers.between 0 1; - default = 0.9; - description = mdDoc '' - The minimal score that should be reached for *best practices*. - ''; - }; + categories = mkOption { + default = { }; + type = types.submodule { + options = { + performance = mkOption { + type = types.numbers.between 0 1; + default = 0.9; + description = '' + The minimal score that should be reached for *performance*. + ''; + }; + accessibility = mkOption { + type = types.numbers.between 0 1; + default = 0.9; + description = '' + The minimal score that should be reached for *accessibility*. + ''; + }; + seo = mkOption { + type = types.numbers.between 0 1; + default = 0.9; + description = '' + The minimal score that should be reached for *SEO*. + ''; + }; + bestPractices = mkOption { + type = types.numbers.between 0 1; + default = 0.9; + description = '' + The minimal score that should be reached for *best practices*. + ''; }; }; }; }; - }); - }; + }; + }); }; }; }; - }); + }; + }); }; config = { - perSystem = { config, self', inputs', pkgs, ... }: + perSystem = { config, pkgs, ... }: let - dist = config.lighthouse.dist; server-port = 8080; - checks = lib.mapAttrs - (name: test: - pkgs.nixosTest { - inherit name; - nodes = { - server = { config, pkgs, ... }: { - environment.variables.CHROME_PATH = "${pkgs.ungoogled-chromium}/bin/chromium"; - systemd.services.lighthouse-test-server = { - wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; - description = "lighthouse-test-server"; - serviceConfig = { - DynamicUser = true; - ExecStart = ''${pkgs.simple-http-server}/bin/simple-http-server -c=js,css,svg,html -i -p ${builtins.toString server-port} -- ${test.dist}''; - }; + checks = lib.mapAttrs (name: test: + pkgs.nixosTest { + inherit name; + nodes = { + server = { pkgs, ... }: { + environment.variables.CHROME_PATH = + lib.getExe pkgs.ungoogled-chromium; + systemd.services.lighthouse-test-server = { + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + description = "lighthouse-test-server"; + serviceConfig = { + DynamicUser = true; + ExecStart = "${ + lib.getExe pkgs.simple-http-server + } -c=js,css,svg,html -i -p ${ + builtins.toString server-port + } -- ${test.dist}"; }; }; }; + }; - testScript = '' - import json - import os + testScript = '' + import json + import os - start_all() - server.wait_for_open_port(${builtins.toString server-port}) + start_all() + server.wait_for_open_port(${builtins.toString server-port}) - report_path = "/tmp/lighthouse-report.json" - server.succeed("CI=1 ${config.lighthouse.package}/bin/lighthouse http://localhost:${builtins.toString server-port} --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 was {}".format(performance_score) + report_path = "/tmp/lighthouse-report.json" + server.succeed("CI=1 ${config.lighthouse.package}/bin/lighthouse http://localhost:${ + builtins.toString server-port + } --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"] - accessibility_score = categories["accessibility"]["score"] - assert accessibility_score >= ${toString test.categories.accessibility}, "accessibility score should be at least ${toString test.categories.accessibility}, but was {}".format(accessibility_score) + performance_score = categories["performance"]["score"] + assert performance_score >= ${ + toString test.categories.performance + }, "performance score should be at least ${ + toString test.categories.performance + }, but was {}".format(performance_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) + accessibility_score = categories["accessibility"]["score"] + assert accessibility_score >= ${ + toString test.categories.accessibility + }, "accessibility score should be at least ${ + toString test.categories.accessibility + }, but was {}".format(accessibility_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)}" - ''; - } - ) - config.lighthouse.tests; - in - { - inherit checks; - }; + 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) + }" + ''; + }) config.lighthouse.tests; + in { inherit checks; }; }; } From 289d4cd4615892f9c75d8d5a5242ac9830c7471e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Fri, 5 Jul 2024 14:26:03 +0200 Subject: [PATCH 3/4] add template and test --- .github/workflows/check.yml | 41 +++++++++++++++++++++++++++++++ flake.nix | 18 ++++++++------ templates/default/dist/index.html | 13 ++++++++++ templates/default/dist/robots.txt | 2 ++ templates/default/flake.nix | 29 ++++++++++++++++++++++ 5 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/check.yml create mode 100644 templates/default/dist/index.html create mode 100644 templates/default/dist/robots.txt create mode 100644 templates/default/flake.nix diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..2f0261a --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,41 @@ +name: check +on: + pull_request: + branches: [main] + push: + branches: [main] + +jobs: + check: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ ubuntu-latest ] + steps: + - uses: actions/checkout@v3 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + + - name: System Info + run: | + uname -a + nix --version + + - name: template + run: | + git config --global init.defaultBranch main + + mkdir example-project + cd example-project + + nix flake init -t ..#default + + git init + git add -A + + # Override the nixpkgs input of the template to use the current branch, + # since that is the branch of interest we want to test + nix flake lock --override-input lighthouse-flake "github:marijanp/lighthouse-flake/$GITHUB_REF" + + nix flake check -L + diff --git a/flake.nix b/flake.nix index 524e914..5d88735 100644 --- a/flake.nix +++ b/flake.nix @@ -2,14 +2,16 @@ inputs = { dreampkgs.url = "github:nix-community/dreampkgs"; }; - outputs = { self, dreampkgs, ... }: - { - flakeModule = { - imports = [ ./flake-module.nix ]; - perSystem = { system, ... }: - { - lighthouse.package = dreampkgs.packages.${system}.lighthouse; - }; + outputs = { nixpkgs, dreampkgs, ... }: { + templates.default = { + path = ./templates/default; + description = "lighthouse-flake flake-parts template"; + }; + flakeModule = { + imports = [ ./flake-module.nix ]; + perSystem = { system, ... }: { + lighthouse.package = dreampkgs.packages.${system}.lighthouse; }; }; + }; } diff --git a/templates/default/dist/index.html b/templates/default/dist/index.html new file mode 100644 index 0000000..a753158 --- /dev/null +++ b/templates/default/dist/index.html @@ -0,0 +1,13 @@ + + + + Hello World + + + + + +

Hello World

+

42

+ + diff --git a/templates/default/dist/robots.txt b/templates/default/dist/robots.txt new file mode 100644 index 0000000..c2a49f4 --- /dev/null +++ b/templates/default/dist/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Allow: / diff --git a/templates/default/flake.nix b/templates/default/flake.nix new file mode 100644 index 0000000..8140f35 --- /dev/null +++ b/templates/default/flake.nix @@ -0,0 +1,29 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; + flake-parts.url = "github:hercules-ci/flake-parts"; + lighthouse-flake.url = "github:marijanp/lighthouse-flake"; + }; + + outputs = inputs@{ self, flake-parts, lighthouse-flake, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ "x86_64-linux" ]; + imports = [ lighthouse-flake.flakeModule ]; + perSystem = { config, self', inputs', pkgs, system, ... }: { + lighthouse = { + tests = { + "test-frontend" = { + dist = ./dist; + categories = { + performance = 0.95; + accessibility = 0.95; + seo = 1.0; + bestPractices = 0.95; + }; + }; + "test-frontend-other" = { dist = ./dist; }; + }; + }; + }; + }; +} From a56ce5a2c9c1dca346ec3d1258b8edc9c359efb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Fri, 5 Jul 2024 14:29:14 +0200 Subject: [PATCH 4/4] flake: add formatter --- flake.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flake.nix b/flake.nix index 5d88735..d8f40e0 100644 --- a/flake.nix +++ b/flake.nix @@ -1,8 +1,10 @@ { inputs = { dreampkgs.url = "github:nix-community/dreampkgs"; + nixpkgs.follows = "dreampkgs/nixpkgs"; }; outputs = { nixpkgs, dreampkgs, ... }: { + formatter."x86_64-linux" = nixpkgs.legacyPackages."x86_64-linux".nixfmt; templates.default = { path = ./templates/default; description = "lighthouse-flake flake-parts template";