16 Aug 2024 |
nebucatnetzer13 | 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 |
Pol | When you build a project uising buildComposerProject it is inherently made for production. | 07:42:15 |
Pol | Since it, by default, doesn't include require-dev . I don't see the point doing it either. | 07:42:40 |
Pol | Perhaps I'm misunderstanding your request? | 07:42:51 |
nebucatnetzer13 | Maybe, let me try to rephrase. | 07:43:38 |
nebucatnetzer13 | 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 | You cannot have --no-dev and install phpunit with require-dev dependencies at the same time | 07:56:16 |
piotrkwiecinski | in this case you have to install phpunit separately | 07:56:37 |
nebucatnetzer13 | Ah sorry I have two derivations, thats the one for production dependencies | 07:56:56 |
nebucatnetzer13 | {
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 | This 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 | However I don't need it to work exactly this way. This was just me trying to hack something together. | 07:59:12 |
nebucatnetzer13 | In a Python project with poetry2nix I can do this for example:
https://github.com/Nebucatnetzer/sort-of-pastebin/blob/main/flake.nix#L54-L67 | 08:04:30 |
nebucatnetzer13 | One is the dev environment which which I can run the tests and the other is the final application. | 08:05:07 |
nebucatnetzer13 | Which I ten use like this to do the tests:
https://github.com/Nebucatnetzer/sort-of-pastebin/blob/main/.github/workflows/tests.yml | 08:05:49 |
nebucatnetzer13 | And when they succeed I build the container | 08:06:02 |
nebucatnetzer13 | https://github.com/Nebucatnetzer/sort-of-pastebin/blob/main/.github/workflows/build_containers.yml | 08:06:10 |
Pol | But you're using nix develop . | 08:09:59 |
Pol | buildComposerProject does not create a development shell (to be used with nix develop ). | 08:10:25 |
nebucatnetzer13 | Is there a way to do that? | 08:15:32 |
Pol | Creating a devshell containing your tools ? | 08:23:39 |
Pol | Yeah it's quite easy to do | 08:23:46 |
Pol | That's the easiest thing to do in Nix I believe. | 08:23:56 |
nebucatnetzer13 | yeah but especially with the content of composer.lock | 08:24:36 |
Pol | mmh I don't think it has been done yet | 08:25:05 |
nebucatnetzer13 | ah okay, because the rest I have already working | 08:25:29 |
Pol | But you'll find all the most used tools already in nixpkgs (phpunit, psalm, phpstan, etc etc) | 08:25:42 |
Pol | I know that you could do 2 derivations | 08:26:34 |