!jngDrdMgndWibPCYsR:nixos.org

Nix PHP

64 Members
A room for PHP developers running on Nix22 Servers

Load older messages


SenderMessageTime
16 Aug 2024
@nebucatnetzer13:matrix.orgnebucatnetzer13 That only works if I only use phpunit for testing right? Otherwise I would have to build two buildComposerProject one for testing and one for production? 07:34:56
@drupol:matrix.orgPol When you build a project uising buildComposerProject it is inherently made for production. 07:42:15
@drupol:matrix.orgPol Since it, by default, doesn't include require-dev. I don't see the point doing it either. 07:42:40
@drupol:matrix.orgPolPerhaps I'm misunderstanding your request?07:42:51
@nebucatnetzer13:matrix.orgnebucatnetzer13Maybe, let me try to rephrase.07:43:38
@nebucatnetzer13:matrix.orgnebucatnetzer13

I would like to run phpunit in a Gitlab CI. Nix devShell will be used to provide PHP and Gitlab provides a MariaDB container.
Composer brings in some form the additional dependencies.

In an existing project we use imperative containers for the unittests.
Each containers brings a vendor directory that is based on the composer.lock from the main branch.
In the pipeline composer install then only has to install packages that have changed compared to the main branch, which often are none.
This way it's faster and we don't hammer packagist.org.

What I tried so far is a fixed output derivation which looks like the following.
In the pipeline I then simply copied the content into the build directory and rebuilt the autoloader.
It is hacky but it worked in principle, until I noticed that the outputHash changed on the runner, compared to my computer.

stdenvNoCC.mkDerivation {
  name = "composer-dependencies";
  src = composerFiles;
  nativeBuildInputs = [ php83Packages.composer ];
  buildPhase = ''
    export COMPOSER_HOME=$TMPDIR
    composer install --no-interaction --no-scripts --no-ansi --no-autoloader --no-dev
  '';
  installPhase = ''
    mkdir -p $out/
    cp -r vendor $out/
  '';
  dontFixup = true;
  outputHashAlgo = "sha256";
  outputHashMode = "recursive";
  outputHash = "sha256-sK2CvCMvlAAPdaeXLl6LwKAUJeiw9p6zXVxio6y0w0c=";
}
07:53:48
@piotrkwiecinski:matrix.orgpiotrkwiecinskiYou cannot have --no-dev and install phpunit with require-dev dependencies at the same time07:56:16
@piotrkwiecinski:matrix.orgpiotrkwiecinskiin this case you have to install phpunit separately07:56:37
@nebucatnetzer13:matrix.orgnebucatnetzer13Ah sorry I have two derivations, thats the one for production dependencies07:56:56
@nebucatnetzer13:matrix.orgnebucatnetzer13
{
  composerDependencies,
  lib,
  php83Packages,
  root,
  stdenvNoCC,
}:
let
  fs = lib.fileset;
  sourceFiles = fs.unions [
    (root + "/composer.json")
    (root + "/composer.lock")
  ];
  composerFiles = fs.toSource {
    fileset = sourceFiles;
    inherit root;
  };
in
stdenvNoCC.mkDerivation {
  name = "composer-dev-dependencies";
  src = composerFiles;
  nativeBuildInputs = [ php83Packages.composer ];
  buildPhase = ''
    cp -r ${composerDependencies}/vendor vendor
    chmod -R 755 vendor
    export COMPOSER_HOME=$TMPDIR
    composer install --no-interaction --no-scripts --no-ansi --no-autoloader
  '';
  installPhase = ''
    mkdir -p $out/
    cp -r vendor $out/
  '';
  dontFixup = true;
  outputHashAlgo = "sha256";
  outputHashMode = "recursive";
  outputHash = "sha256-oHkogPJiwXE+XBJxh0yTsFRYptLkgCG5v6ExFuef72Y=";
}
07:57:08
@nebucatnetzer13:matrix.orgnebucatnetzer13This one depends on the previous one and that's how I noticed that the outputHash changed because I didn't push the previous derivation to the cache. Therefore the runner had to built it itself and failed because the hash didn't match.07:58:35
@nebucatnetzer13:matrix.orgnebucatnetzer13However I don't need it to work exactly this way. This was just me trying to hack something together.07:59:12
@nebucatnetzer13:matrix.orgnebucatnetzer13In a Python project with poetry2nix I can do this for example: https://github.com/Nebucatnetzer/sort-of-pastebin/blob/main/flake.nix#L54-L6708:04:30
@nebucatnetzer13:matrix.orgnebucatnetzer13One is the dev environment which which I can run the tests and the other is the final application.08:05:07
@nebucatnetzer13:matrix.orgnebucatnetzer13Which I ten use like this to do the tests: https://github.com/Nebucatnetzer/sort-of-pastebin/blob/main/.github/workflows/tests.yml08:05:49
@nebucatnetzer13:matrix.orgnebucatnetzer13And when they succeed I build the container08:06:02
@nebucatnetzer13:matrix.orgnebucatnetzer13https://github.com/Nebucatnetzer/sort-of-pastebin/blob/main/.github/workflows/build_containers.yml08:06:10
@drupol:matrix.orgPol But you're using nix develop. 08:09:59
@drupol:matrix.orgPol buildComposerProject does not create a development shell (to be used with nix develop). 08:10:25
@nebucatnetzer13:matrix.orgnebucatnetzer13Is there a way to do that?08:15:32
@drupol:matrix.orgPolCreating a devshell containing your tools ?08:23:39
@drupol:matrix.orgPolYeah it's quite easy to do08:23:46
@drupol:matrix.orgPolThat's the easiest thing to do in Nix I believe.08:23:56
@nebucatnetzer13:matrix.orgnebucatnetzer13yeah but especially with the content of composer.lock08:24:36
@drupol:matrix.orgPolmmh I don't think it has been done yet08:25:05
@nebucatnetzer13:matrix.orgnebucatnetzer13ah okay, because the rest I have already working08:25:29
@drupol:matrix.orgPol But you'll find all the most used tools already in nixpkgs (phpunit, psalm, phpstan, etc etc) 08:25:42
@drupol:matrix.orgPolI know that you could do 2 derivations08:26:34
@drupol:matrix.orgPolFor example:08:26:38
@drupol:matrix.orgPol
  1. my-app -> would be made with buildComposerProject, using regular attributes. This is ready to go in prod if needed, it doesn't contain any require-dev stuff.
08:27:14

Show newer messages


Back to Room ListRoom Version: 6