<commit_before>{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.phpfpm;
  enabled = cfg.pools != {};

  poolConfigs = (mapAttrs mapPool cfg.pools);

  mapPool = n: p: {
    phpPackage = p.phpPackage;
    phpOptions = p.phpOptions;
    userPool = p.user;
    groupPool = p.group;
  };

  fpmCfgFile = pool: conf: pkgs.writeText "phpfpm-${pool}.conf" ''
    [global]
    error_log = syslog
    daemonize = no
    ${cfg.globalExtraConfig}

    [${pool}]
    listen = /run/phpfpm-${pool}/${cfg.pools.${pool}.socketName}.sock
    ${cfg.pools.${pool}.extraConfig}
  '';

  phpIni = pool: pkgs.runCommand "php.ini" {
    inherit (pool) phpPackage phpOptions;
    preferLocalBuild = true;
    nixDefaults = ''
      sendmail_path = "/run/wrappers/bin/sendmail -t -i"
    '';
    passAsFile = [ "nixDefaults" "phpOptions" ];
  } ''
    cat $phpPackage/etc/php.ini $nixDefaultsPath $phpOptionsPath > $out
  '';

in {
  options = {
    services.phpfpm = {
      globalExtraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Global extra configuration that should be put in the global section of
          the PHP-FPM configuration file. Do not specify the options
          <literal>error_log</literal> or
          <literal>daemonize</literal> here, since they are generated by NixOS.
        '';
      };

      pools = mkOption {
        default = {};
        type = types.attrsOf (types.submodule {
          options = {
            socketName = mkOption {
              type = types.str;
              example = "php-fpm";
              description = ''
                The address on which to accept FastCGI requests.
              '';
            };

            phpPackage = mkOption {
              type = types.package;
              default = fpmCfg.phpPackage;
              defaultText = "config.services.phpfpm.phpPackage";
              description = ''
                The PHP package to use for running this PHP-FPM pool.
              '';
            };

            phpOptions = mkOption {
              type = types.lines;
              default = fpmCfg.phpOptions;
              defaultText = "config.services.phpfpm.phpOptions";
              description = ''
                "Options appended to the PHP configuration file <filename>php.ini</filename> used for this PHP-FPM pool."
              '';
            };

            user = mkOption {
              type = types.string;
              default = "phpfpm";
              description = "User account under which phpfpm runs.";
            };

            group = mkOption {
              type = types.string;
              default = "phpfpm";
              description = "Group account under which phpfpm runs.";
            };

            extraConfig = mkOption {
              type = types.lines;
              example = ''
                pm = dynamic
                pm.max_children = 75
                pm.start_servers = 10
                pm.min_spare_servers = 5
                pm.max_spare_servers = 20
                pm.max_requests = 500
              '';<commit_msg>nixos/phpfpm: Remove usage of undefined variable fpmCfg<commit_after>{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.phpfpm;
  enabled = cfg.pools != {};

  poolConfigs = (mapAttrs mapPool cfg.pools);

  mapPool = n: p: {
    phpPackage = p.phpPackage;
    phpOptions = p.phpOptions;
    userPool = p.user;
    groupPool = p.group;
  };

  fpmCfgFile = pool: conf: pkgs.writeText "phpfpm-${pool}.conf" ''
    [global]
    error_log = syslog
    daemonize = no
    ${cfg.globalExtraConfig}

    [${pool}]
    listen = /run/phpfpm-${pool}/${cfg.pools.${pool}.socketName}.sock
    ${cfg.pools.${pool}.extraConfig}
  '';

  phpIni = pool: pkgs.runCommand "php.ini" {
    inherit (pool) phpPackage phpOptions;
    preferLocalBuild = true;
    nixDefaults = ''
      sendmail_path = "/run/wrappers/bin/sendmail -t -i"
    '';
    passAsFile = [ "nixDefaults" "phpOptions" ];
  } ''
    cat $phpPackage/etc/php.ini $nixDefaultsPath $phpOptionsPath > $out
  '';

in {
  options = {
    services.phpfpm = {
      globalExtraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Global extra configuration that should be put in the global section of
          the PHP-FPM configuration file. Do not specify the options
          <literal>error_log</literal> or
          <literal>daemonize</literal> here, since they are generated by NixOS.
        '';
      };

      pools = mkOption {
        default = {};
        type = types.attrsOf (types.submodule {
          options = {
            socketName = mkOption {
              type = types.str;
              example = "php-fpm";
              description = ''
                The address on which to accept FastCGI requests.
              '';
            };

            phpPackage = mkOption {
              type = types.package;
              default = pkgs.php;
              defaultText = "pkgs.php";
              description = ''
                The PHP package to use for running this PHP-FPM pool.
              '';
            };

            phpOptions = mkOption {
              type = types.lines;
              default = "";
              description = ''
                Options appended to the PHP configuration file <filename>php.ini</filename> used for this PHP-FPM pool.
              '';
            };

            user = mkOption {
              type = types.string;
              default = "phpfpm";
              description = "User account under which phpfpm runs.";
            };

            group = mkOption {
              type = types.string;
              default = "phpfpm";
              description = "Group account under which phpfpm runs.";
            };

            extraConfig = mkOption {
              type = types.lines;
              example = ''
                pm = dynamic
                pm.max_children = 75
                pm.start_servers = 10
                pm.min_spare_servers = 5
                pm.max_spare_servers = 20
                pm.max_requests = 500
              '';
{'fraction_non_alphanumeric': 0.106645056726094, 'fraction_numerical': 0.0032414910858995136, 'mean_word_length': 1.5489467162329615, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4935195', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1706, 'n_words': 423}