From 487b8f9555150f434746097045999eeb409422df Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Tue, 7 Jan 2025 09:22:56 +0100 Subject: [PATCH 1/5] Nix: Format with nixfmt --- flake.nix | 70 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/flake.nix b/flake.nix index eeab6e5..8a7d48f 100644 --- a/flake.nix +++ b/flake.nix @@ -1,31 +1,43 @@ { description = "Entry registration system for Rogaining"; - inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - inputs.utils.url = "github:numtide/flake-utils"; + utils.url = "github:numtide/flake-utils"; - inputs.composer2nixRepo = { - url = "github:svanderburg/composer2nix"; - flake = false; - }; + composer2nixRepo = { + url = "github:svanderburg/composer2nix"; + flake = false; + }; - inputs.flake-compat = { - url = "github:edolstra/flake-compat"; - flake = false; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; }; - outputs = { self, nixpkgs, utils, flake-compat, composer2nixRepo }: - utils.lib.eachDefaultSystem (system: + outputs = + { + nixpkgs, + utils, + composer2nixRepo, + ... + }: + utils.lib.eachDefaultSystem ( + system: let pkgs = nixpkgs.legacyPackages.${system}; - importComposerPackage = path: (import path { - inherit system pkgs; - noDev = true; - }).override { - executable = true; - }; + importComposerPackage = + path: + (import path { + inherit system pkgs; + noDev = true; + }).override + { + executable = true; + }; composer2nix = importComposerPackage composer2nixRepo.outPath; @@ -37,27 +49,35 @@ env NIX_PATH=nixpkgs=${nixpkgs.outPath} ${composer2nix}/bin/composer2nix -p nette/code-checker popd ''; - in { + in + { devShells = { default = let - php = pkgs.php81.withExtensions ({ enabled, all }: with all; enabled ++ [ - intl - ]); + php = pkgs.php81.withExtensions ( + { enabled, all }: + with all; + enabled + ++ [ + intl + ] + ); in - pkgs.mkShell { - nativeBuildInputs = [ + pkgs.mkShell { + nativeBuildInputs = + [ php pkgs.python3 # for create-zipball.py nette-code-checker update-php-extradeps pkgs.nodejs pkgs.phpactor - ] ++ (with php.packages; [ + ] + ++ (with php.packages; [ composer psalm ]); - }; + }; }; } ); From 088df8f307e97f4d9e3f4c98e6cafd450674c830 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Tue, 7 Jan 2025 09:04:48 +0100 Subject: [PATCH 2/5] Port to Devenv This will allow us to use in-project MySQL database instance. - Started with `nix flake init --template github:cachix/devenv` as per https://devenv.sh/guides/using-with-flakes/ - Using devenv-nixpkgs https://devenv.sh/blog/2024/03/20/devenv-10-rewrite-in-rust/#devenv-nixpkgs - Remove flake-compat as we switched to `use flake`. --- .envrc | 12 +- .github/workflows/main.yaml | 18 +-- .gitignore | 1 + default.nix | 14 --- flake.lock | 243 +++++++++++++++++++++++++++++++----- flake.nix | 121 +++++++++--------- shell.nix | 14 --- 7 files changed, 300 insertions(+), 123 deletions(-) delete mode 100644 default.nix delete mode 100644 shell.nix diff --git a/.envrc b/.envrc index 1b5ce28..dc25ec7 100644 --- a/.envrc +++ b/.envrc @@ -1,2 +1,10 @@ -use nix -watch_file flake.nix flake.lock +if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs=" +fi + +watch_file flake.nix +watch_file flake.lock +if ! use flake . --no-pure-eval +then + echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2 +fi diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index c5cd6a0..2eb2795 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -16,33 +16,37 @@ jobs: - name: Install Nix uses: cachix/install-nix-action@v25 + - uses: cachix/cachix-action@v14 + with: + name: devenv + - name: Set outputs id: vars run: | echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" - name: Install dependencies - run: nix-shell --run 'composer install --no-interaction' + run: nix develop --no-pure-eval -c composer install --no-interaction - name: Check code using Nette code checker - run: nix-shell --run 'code-checker -d app' + run: nix develop --no-pure-eval -c code-checker -d app - name: Lint coding style - run: nix-shell --run 'composer run-script cs' + run: nix develop --no-pure-eval -c composer run-script cs - name: Run static analysis - run: nix-shell --run 'composer run-script phpstan' + run: nix develop --no-pure-eval -c composer run-script phpstan - name: Lint source code - run: nix-shell --run 'composer run-script lint' + run: nix develop --no-pure-eval -c composer run-script lint - name: Run tests run: | cp .github/workflows/private.neon app/Config/private.neon - nix-shell --run 'composer test' + nix develop --no-pure-eval -c composer test - name: Build a zipball - run: nix-shell --run 'npm run dist "entries-${{ steps.vars.outputs.sha_short }}.zip"' + run: nix develop --no-pure-eval -c npm run dist "entries-${{ steps.vars.outputs.sha_short }}.zip" - name: Upload the zipball to GitHub uses: actions/upload-artifact@v4 diff --git a/.gitignore b/.gitignore index e2e0d9d..037345f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ phpstan.neon /.parcel-cache/ /.direnv/ +/.devenv/ diff --git a/default.nix b/default.nix deleted file mode 100644 index 2f2e032..0000000 --- a/default.nix +++ /dev/null @@ -1,14 +0,0 @@ -# SPDX-FileCopyrightText: 2022 Jan Tojnar -# SPDX-License-Identifier: MIT - -let - lock = builtins.fromJSON (builtins.readFile ./flake.lock); - flake-compat = builtins.fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - }; - flake = import flake-compat { - src = ./.; - }; -in -flake.defaultNix diff --git a/flake.lock b/flake.lock index cc739b9..da01f25 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,33 @@ { "nodes": { + "cachix": { + "inputs": { + "devenv": [ + "devenv" + ], + "flake-compat": [ + "devenv" + ], + "git-hooks": [ + "devenv" + ], + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1728672398, + "narHash": "sha256-KxuGSoVUFnQLB2ZcYODW7AVPAh9JqRlD5BrfsC/Q4qs=", + "owner": "cachix", + "repo": "cachix", + "rev": "aac51f698309fd0f381149214b7eee213c66ef0a", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "latest", + "repo": "cachix", + "type": "github" + } + }, "composer2nixRepo": { "flake": false, "locked": { @@ -16,14 +44,38 @@ "type": "github" } }, + "devenv": { + "inputs": { + "cachix": "cachix", + "flake-compat": "flake-compat", + "git-hooks": "git-hooks", + "nix": "nix", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1736184348, + "narHash": "sha256-LvuuwJBlZjLgjl6uzhfXP0rBveoNx7q0nz21xYb1AII=", + "owner": "cachix", + "repo": "devenv", + "rev": "07219f00c633f756d1f0cc5bb6c4c311b5c4cb0d", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { - "lastModified": 1733328505, - "narHash": "sha256-NeCCThCEP3eCl2l/+27kNNK7QrwZB1IJCrXfrbv5oqU=", + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", "owner": "edolstra", "repo": "flake-compat", - "rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", "type": "github" }, "original": { @@ -32,28 +84,181 @@ "type": "github" } }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": [ + "devenv", + "nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1712014858, + "narHash": "sha256-sB4SWl2lX95bExY2gMFG5HIzvva5AVMJd4Igm+GpZNw=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "9126214d0a59633752a136528f5f3b9aa8565b7d", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": [ + "devenv" + ], + "gitignore": "gitignore", + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-stable": [ + "devenv" + ] + }, + "locked": { + "lastModified": 1730302582, + "narHash": "sha256-W1MIJpADXQCgosJZT8qBYLRuZls2KSiKdpnTVdKBuvU=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "af8a16fe5c264f5e9e18bcee2859b40a656876cf", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "devenv", + "git-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "libgit2": { + "flake": false, + "locked": { + "lastModified": 1697646580, + "narHash": "sha256-oX4Z3S9WtJlwvj0uH9HlYcWv+x1hqp8mhXl7HsLu2f0=", + "owner": "libgit2", + "repo": "libgit2", + "rev": "45fd9ed7ae1a9b74b957ef4f337bc3c8b3df01b5", + "type": "github" + }, + "original": { + "owner": "libgit2", + "repo": "libgit2", + "type": "github" + } + }, + "nix": { + "inputs": { + "flake-compat": [ + "devenv" + ], + "flake-parts": "flake-parts", + "libgit2": "libgit2", + "nixpkgs": "nixpkgs_2", + "nixpkgs-23-11": [ + "devenv" + ], + "nixpkgs-regression": [ + "devenv" + ], + "pre-commit-hooks": [ + "devenv" + ] + }, + "locked": { + "lastModified": 1727438425, + "narHash": "sha256-X8ES7I1cfNhR9oKp06F6ir4Np70WGZU5sfCOuNBEwMg=", + "owner": "domenkozar", + "repo": "nix", + "rev": "f6c5ae4c1b2e411e6b1e6a8181cc84363d6a7546", + "type": "github" + }, + "original": { + "owner": "domenkozar", + "ref": "devenv-2.24", + "repo": "nix", + "type": "github" + } + }, "nixpkgs": { "locked": { - "lastModified": 1733376361, - "narHash": "sha256-aLJxoTDDSqB+/3orsulE6/qdlX6MzDLIITLZqdgMpqo=", + "lastModified": 1730531603, + "narHash": "sha256-Dqg6si5CqIzm87sp57j5nTaeBbWhHFaVyG7V6L8k3lY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "7ffd9ae656aec493492b44d0ddfb28e79a1ea25d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1717432640, + "narHash": "sha256-+f9c4/ZX5MWDOuB1rKoWj+lBNm0z0rs4CK47HBLxy1o=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "929116e316068c7318c54eb4d827f7d9756d5e9c", + "rev": "88269ab3044128b7c2f4c7d68448b2fb50456870", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixpkgs-unstable", + "ref": "release-24.05", "repo": "nixpkgs", "type": "github" } }, + "nixpkgs_3": { + "locked": { + "lastModified": 1733477122, + "narHash": "sha256-qamMCz5mNpQmgBwc8SB5tVMlD5sbwVIToVZtSxMph9s=", + "owner": "cachix", + "repo": "devenv-nixpkgs", + "rev": "7bd9e84d0452f6d2e63b6e6da29fe73fac951857", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "rolling", + "repo": "devenv-nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "composer2nixRepo": "composer2nixRepo", - "flake-compat": "flake-compat", - "nixpkgs": "nixpkgs", - "utils": "utils" + "devenv": "devenv", + "nixpkgs": "nixpkgs_3", + "systems": "systems" } }, "systems": { @@ -70,24 +275,6 @@ "repo": "default", "type": "github" } - }, - "utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1731533236, - "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 8a7d48f..89a6fd2 100644 --- a/flake.nix +++ b/flake.nix @@ -2,83 +2,88 @@ description = "Entry registration system for Rogaining"; inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - - utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling"; + systems.url = "github:nix-systems/default"; + devenv.url = "github:cachix/devenv"; + devenv.inputs.nixpkgs.follows = "nixpkgs"; composer2nixRepo = { url = "github:svanderburg/composer2nix"; flake = false; }; - - flake-compat = { - url = "github:edolstra/flake-compat"; - flake = false; - }; }; outputs = { nixpkgs, - utils, + devenv, + systems, composer2nixRepo, ... - }: - utils.lib.eachDefaultSystem ( - system: - let - pkgs = nixpkgs.legacyPackages.${system}; + }@inputs: - importComposerPackage = - path: - (import path { - inherit system pkgs; - noDev = true; - }).override - { - executable = true; - }; + let + forEachSystem = nixpkgs.lib.genAttrs (import systems); + in + { + devShells = forEachSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; - composer2nix = importComposerPackage composer2nixRepo.outPath; + importComposerPackage = + path: + (import path { + inherit system pkgs; + noDev = true; + }).override + { + executable = true; + }; - nette-code-checker = importComposerPackage ./.github/workflows/nix/code-checker; + composer2nix = importComposerPackage composer2nixRepo.outPath; - update-php-extradeps = pkgs.writeShellScriptBin "update-php-extradeps" '' - pushd .github/workflows/nix/code-checker - composer update - env NIX_PATH=nixpkgs=${nixpkgs.outPath} ${composer2nix}/bin/composer2nix -p nette/code-checker - popd - ''; - in - { - devShells = { - default = - let - php = pkgs.php81.withExtensions ( - { enabled, all }: - with all; - enabled - ++ [ - intl - ] - ); - in - pkgs.mkShell { - nativeBuildInputs = - [ - php + nette-code-checker = importComposerPackage ./.github/workflows/nix/code-checker; + + update-php-extradeps = pkgs.writeShellScriptBin "update-php-extradeps" '' + pushd .github/workflows/nix/code-checker + composer update + env NIX_PATH=nixpkgs=${nixpkgs.outPath} ${composer2nix}/bin/composer2nix -p nette/code-checker + popd + ''; + + php = pkgs.php81.withExtensions ( + { enabled, all }: + with all; + enabled + ++ [ + intl + ] + ); + in + { + default = devenv.lib.mkShell { + inherit inputs pkgs; + modules = [ + { + # https://devenv.sh/reference/options/ + packages = [ pkgs.python3 # for create-zipball.py nette-code-checker update-php-extradeps pkgs.nodejs pkgs.phpactor - ] - ++ (with php.packages; [ - composer - psalm - ]); - }; - }; - } - ); + php.packages.psalm + ]; + + languages.php = { + enable = true; + package = php; + }; + } + ]; + }; + } + ); + }; } diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 7f6e40e..0000000 --- a/shell.nix +++ /dev/null @@ -1,14 +0,0 @@ -# SPDX-FileCopyrightText: 2022 Jan Tojnar -# SPDX-License-Identifier: MIT - -let - lock = builtins.fromJSON (builtins.readFile ./flake.lock); - flake-compat = builtins.fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - }; - flake = import flake-compat { - src = ./.; - }; -in -flake.shellNix From 198da936364b3cf6b11030fe4593970c1626ddd0 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Tue, 7 Jan 2025 10:04:36 +0100 Subject: [PATCH 3/5] Bootstrap: Expose environment to config https://doc.nette.org/en/application/bootstrap#toc-dynamic-parameters --- app/Bootstrap.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Bootstrap.php b/app/Bootstrap.php index c3a2915..b9e51ff 100644 --- a/app/Bootstrap.php +++ b/app/Bootstrap.php @@ -13,6 +13,9 @@ public static function boot(): Configurator { $configurator->enableTracy(__DIR__ . '/../log'); $configurator->setTimeZone('UTC'); $configurator->setTempDirectory(__DIR__ . '/../temp'); + $configurator->addDynamicParameters([ + 'env' => getenv(), + ]); $configurator ->addConfig(__DIR__ . '/Config/common.neon'); $configurator From 8843db134f23bed640bf75f0ba06fd6720c66cc5 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Tue, 7 Jan 2025 10:14:13 +0100 Subject: [PATCH 4/5] www: Support cli-server sapi This is somewhat equivalent to what `www/.htaccess` does. --- www/index.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/www/index.php b/www/index.php index 466a988..b112732 100644 --- a/www/index.php +++ b/www/index.php @@ -2,6 +2,13 @@ declare(strict_types=1); +if (PHP_SAPI === 'cli-server') { + $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?: ''; + if (is_file(__DIR__ . $path)) { + return false; + } +} + require __DIR__ . '/../vendor/autoload.php'; $configurator = App\Bootstrap::boot(); From 819d5ad2b032639505e1482a7ec3e99f5a93c5f9 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Tue, 7 Jan 2025 09:58:57 +0100 Subject: [PATCH 5/5] Devenv: Add processes Now testing is as simple as ln -sf $PWD/.github/workflows/private.neon app/Config/ devenv up The development HTTP server will be running on http://localhost:8084/ TODO: Re-evaluate HTTP port selection --- .github/workflows/private.neon | 4 ++++ flake.nix | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/.github/workflows/private.neon b/.github/workflows/private.neon index 82e2514..65a5c71 100644 --- a/.github/workflows/private.neon +++ b/.github/workflows/private.neon @@ -1,2 +1,6 @@ +dbal: + driver: mysqli + unix_socket: %env.DEVENV_RUNTIME%/mysql.sock + parameters: adminPassword: heslo diff --git a/flake.nix b/flake.nix index 89a6fd2..0581d4c 100644 --- a/flake.nix +++ b/flake.nix @@ -15,6 +15,7 @@ outputs = { + self, nixpkgs, devenv, systems, @@ -26,6 +27,10 @@ forEachSystem = nixpkgs.lib.genAttrs (import systems); in { + packages = forEachSystem (system: { + devenv-up = self.devShells.${system}.default.config.procfileScript; + }); + devShells = forEachSystem ( system: let @@ -80,6 +85,20 @@ enable = true; package = php; }; + + processes.http = { + exec = "cd www; php -S localhost:8084 index.php"; + }; + + services.mysql = { + enable = true; + initialDatabases = [ + { + name = "entries"; + schema = ./install.sql; + } + ]; + }; } ]; };