initial version

This commit is contained in:
Robin Appelman 2025-10-25 15:23:20 +02:00
commit 1eceb2ce73
8 changed files with 428 additions and 0 deletions

View file

@ -0,0 +1,16 @@
name: "CI"
on:
pull_request:
push:
jobs:
checks:
runs-on: nix
steps:
- uses: actions/checkout@v4
- uses: https://codeberg.org/icewind/attic-action@v1
with:
name: link
instance: https://cache.icewind.link
authToken: "${{ secrets.ATTIC_TOKEN }}"
- run: nix flake check --keep-going

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
result

26
README.md Normal file
View file

@ -0,0 +1,26 @@
# flakelight-php
An opinionated php module for
[flakelight](https://github.com/nix-community/flakelight).
## Features
Included checks:
- php-cs-checker
## Usage
```nix
{
inputs = {
nixpkgs.url = "nixpkgs/nixos-25.05";
flakelight-php = {
url = "path:/home/robin/Projects/flakelight-php";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {flakelight-php, ...}:
flakelight-php ./. {};
}
```

47
composer-meta.nix Normal file
View file

@ -0,0 +1,47 @@
{
lib,
allVersions,
}: let
inherit (lib) splitString split trim map substring stringToCharacters foldr elemAt filter isString;
inherit (lib.lists) findFirstIndex;
inherit (lib.strings) charToInt compareVersions;
in
composerJson: let
versionComparesForOperations = {
"=" = i: i == 0;
">" = i: i > 0;
"<" = i: i < 0;
">=" = i: i >= 0;
"<=" = i: i <= 0;
};
isInt = c: let
ascii = charToInt c;
zero = charToInt "0";
nine = charToInt "9";
in
ascii >= zero && ascii <= nine;
splitConstraint = constraint: let
trimmed = trim constraint;
startIndex = findFirstIndex isInt 0 (stringToCharacters constraint);
in [(substring 0 startIndex trimmed) (substring startIndex (-1) trimmed)];
matchesSingleConstraint = constraint: version: let
parts = splitConstraint constraint;
cmp = elemAt parts 0;
against = elemAt parts 1;
in
versionComparesForOperations.${cmp} (compareVersions version against);
splitOrParts = expr: map trim (splitString "||" expr);
splitAndParts = expr: filter isString (split "[, ]+" expr);
foldAnd = foldr (a: b: a && b) true;
foldOr = foldr (a: b: a || b) false;
matchesAnd = expression: version: foldAnd (map (ex: matchesSingleConstraint ex version) (splitAndParts expression));
matchesConstraint = expression: version: foldOr (map (ex: matchesAnd ex version) (splitOrParts expression));
phpConstraint = composerJson.require.php;
matches = matchesConstraint phpConstraint;
in {
phpVersions = filter matches allVersions;
}

119
flake.lock generated Normal file
View file

@ -0,0 +1,119 @@
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1747046372,
"narHash": "sha256-CIVLLkVgvHYbgI2UpXvIIBJ12HWgX+fjA8Xf8PUmqCY=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flakelight": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1756730985,
"narHash": "sha256-Uv5lLUZfFxQv6RHi1TqLTKso0j0eUVMQQwud29LTV/s=",
"owner": "nix-community",
"repo": "flakelight",
"rev": "950121d809b75c32e73684b32ccba8d4e8a67703",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "flakelight",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1761173472,
"narHash": "sha256-m9W0dYXflzeGgKNravKJvTMR4Qqa2MVD11AwlGMufeE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c8aa8cc00a5cb57fada0851a038d35c08a36a2bb",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-25.05",
"type": "indirect"
}
},
"phps": {
"inputs": {
"flake-compat": "flake-compat",
"nixpkgs": [
"nixpkgs"
],
"utils": "utils"
},
"locked": {
"lastModified": 1760894074,
"narHash": "sha256-z2EYR3CA5PWTkUr5WkDni2CZQ326msHLPghJLx11pZ0=",
"owner": "fossar",
"repo": "nix-phps",
"rev": "d2807871f18ab2150b1e46b7ee126fe0bf200d4c",
"type": "github"
},
"original": {
"owner": "fossar",
"repo": "nix-phps",
"type": "github"
}
},
"root": {
"inputs": {
"flakelight": "flakelight",
"nixpkgs": "nixpkgs",
"phps": "phps"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"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",
"version": 7
}

54
flake.nix Normal file
View file

@ -0,0 +1,54 @@
# mill-scale -- A php module for flakelight
# Copyright (C) 2025 Robin Appelman <robin@icewind.nl>
# SPDX-License-Identifier: MIT
{
description = "A php module for flakelite";
inputs = {
nixpkgs.url = "nixpkgs/nixos-25.05";
flakelight = {
url = "github:nix-community/flakelight";
inputs.nixpkgs.follows = "nixpkgs";
};
phps = {
url = "github:fossar/nix-phps";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
flakelight,
phps,
...
}:
flakelight ./. {
imports = [flakelight.flakelightModules.extendFlakelight];
withOverlays = [phps.overlays.default];
formatters = pkgs:
with pkgs; {
"*.nix" = pkgs.lib.getExe alejandra;
};
flakelightModule = {lib, ...}: {
imports = [./flakelight-php.nix];
};
checks = {
nixTests = pkgs:
(import ./nix-tests.nix pkgs) (pkgs: let
composerMeta = pkgs.callPackage ./composer-meta.nix {
allVersions = ["7.3" "7.4" "8.0" "8.1" "8.2" "8.3" "8.4"];
};
in {
testPhpVersionsGt = {
expr = (composerMeta {require.php = ">=8.1";}).phpVersions;
expected = ["8.1" "8.2" "8.3" "8.4"];
};
testPhpVersionsAnd = {
expr = (composerMeta {require.php = ">8.0 <8.4";}).phpVersions;
expected = ["8.1" "8.2" "8.3"];
};
testPhpVersionsAndOr = {
expr = (composerMeta {require.php = ">8.0 <8.4 || =7.4";}).phpVersions;
expected = ["7.4" "8.1" "8.2" "8.3"];
};
});
};
};
}

138
flakelight-php.nix Normal file
View file

@ -0,0 +1,138 @@
# mill-scale -- Another rust module for flakelight
# Copyright (C) 2024 Robin Appelman <robin@icewind.nl>
# SPDX-License-Identifier: MIT
{
lib,
src,
config,
flakelight,
inputs,
...
}: let
inherit (builtins) elem readFile pathExists match any concatLists fromJSON attrNames head;
inherit (lib) getExe map mkDefault mkIf mkMerge mkOption warnIf optionalAttrs types optionalString genAttrs hasInfix makeSearchPathOutput;
inherit (lib.fileset) fileFilter toSource unions;
inherit (flakelight.types) fileset function optFunctionTo;
phpVersions = {
"5.6" = pkgs: pkgs.php56;
"7.0" = pkgs: pkgs.php70;
"7.1" = pkgs: pkgs.php71;
"7.2" = pkgs: pkgs.php72;
"7.3" = pkgs: pkgs.php73;
"7.4" = pkgs: pkgs.php74;
"8.0" = pkgs: pkgs.php80;
"8.1" = pkgs: pkgs.php81;
"8.2" = pkgs: pkgs.php82;
"8.3" = pkgs: pkgs.php83;
"8.4" = pkgs: pkgs.php84;
};
filteredSrc = toSource {
root = src;
fileset = unions (config.extraPaths ++ [config.fileset]);
};
composerJson = fromJSON (readFile config.composerJson);
composerMeta =
(import ./composer-meta.nix {
inherit lib;
allVersions = attrNames phpVersions;
})
composerJson;
hasPsalm = pathExists config.psalmConfig;
hasPhpCsFixer = pathExists config.phpCsFixerConfig;
buildPhp = pkgs:
(phpVersions.${head composerMeta.phpVersions} pkgs).buildEnv {
extensions = {
enabled,
all,
}:
enabled ++ (config.phpExtensions all);
};
in
warnIf (! builtins ? readFileType) "Unsupported Nix version in use."
{
options = {
composerJson = mkOption {
type = with types; path;
default = src + /composer.json;
};
composerLock = mkOption {
type = with types; path;
default = src + /composer.lock;
};
psalmConfig = mkOption {
type = with types; path;
default = src + /psalm.xml;
};
phpCsFixerConfig = mkOption {
type = with types; path;
default = src + /.php-cs-fixer.dist.php;
};
extraPaths = mkOption {
type = with types; listOf path;
default = [];
};
fileset = mkOption {
type = fileset;
default =
fileFilter
(file:
file.hasExt "php"
|| match "snapshot__.*\.snap" file.name != null
|| elem file.name [".php-cs-fixer.dist.php" "psalm.xml" "package.json" "package.lock"])
src;
};
phpExtensions = mkOption {
type = function;
default = all: [];
description = "extra php extensions to enable";
};
tools = mkOption {
type = function;
default = pkgs: [];
description = "extra packages to make available in the dev shells";
};
};
config = mkMerge [
(mkIf (pathExists config.composerJson) {
withOverlays = [
(final: prev: {
minPhp = buildPhp final;
inherit (final.minPhp.packages) php-cs-fixer composer;
})
];
description = mkIf (composerMeta ? description) composerMeta.description;
license = mkIf (composerMeta ? license) (mkDefault composerMeta.license);
pname = composerMeta.name;
outputs = {
lib.phpVersions = composerMeta.phpVersions;
};
checks = {pkgs, ...}: (optionalAttrs hasPsalm {
psalm = pkgs.runCommand "psalm-check" {} "exit 1;";
});
})
{
devShells = {
default = {
packages = pkgs: (config.tools pkgs) ++ (with pkgs; [minPhp php-cs-fixer composer]);
};
};
formatters = pkgs:
with pkgs; ({
"*.nix" = getExe alejandra;
}
// optionalAttrs hasPhpCsFixer {
"*.php" = "${getExe php-cs-fixer} fix";
});
}
];
}

27
nix-tests.nix Normal file
View file

@ -0,0 +1,27 @@
pkgs: tests: let
formatValue = val:
if (builtins.isList val || builtins.isAttrs val)
then builtins.toJSON val
else builtins.toString val;
resultToString = {
name,
expected,
result,
}: ''
${name} failed: expected ${formatValue expected}, but got ${
formatValue result
}
'';
results = pkgs.lib.runTests (tests pkgs);
in
if results != []
then builtins.throw (builtins.concatStringsSep "\n" (map resultToString results))
## TODO: The derivation below is preferable but "nix flake check" hangs with it:
## (it's preferable because "examples/many-failures" would then show all errors.)
# pkgs.runCommand "nix-flake-tests-failure" { } ''
# cat <<EOF
# ${builtins.concatStringsSep "\n" (map resultToString results)}
# EOF
# exit 1
# ''
else pkgs.runCommand "nix-flake-tests-success" {} "echo > $out"