Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/phpfpm: fix apply global phpOptions #72767

Merged
merged 1 commit into from Nov 11, 2019
Merged

Conversation

Izorkin
Copy link
Contributor

@Izorkin Izorkin commented Nov 4, 2019

Motivation for this change

Fixed apply of parameter config.services.phpfpm.phpOptions

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nix-review --run "nix-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.
Notify maintainers

cc @

@Izorkin
Copy link
Contributor Author

Izorkin commented Nov 4, 2019

cc @etu @aanderse

Copy link
Member

@aanderse aanderse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we renaming? I like the names the way they are. If the documentation isn't clear enough can we please just update the documentation instead?

nixos/modules/services/web-servers/phpfpm/default.nix Outdated Show resolved Hide resolved
@Izorkin
Copy link
Contributor Author

Izorkin commented Nov 4, 2019

The name of the option immediately shows that it applies to all pools.
Before:

  services.phpfpm = {
    phpOptions = ''
      date.timezone = "Europe/Moscow"
      error_log = /var/log/phpfpm.log
      session.save_handler = memcached
      session.save_path = "127.0.0.1:11211"
    '';
    settings = {
      "rlimit_core" = "unlimited";
      "rlimit_files" = "131072";
    };
    pools = {
      "site-01" = {
        phpPackage = pkgs.php;
        user = "wwwrun";
        group = "wwwrun";
        phpOptions = ''
          error_log = /var/log/phpfpm-site-01.log
        '';
        settings = {
          "listen.owner" = "${cfgUser01}";
          "listen.group" = "${cfgGroup01}";
          "listen.mode" = "0600";
        };
      };
    };
  };

After:

  services.phpfpm = {
    globalPhpOptions = ''
      date.timezone = "Europe/Moscow"
      error_log = /var/log/phpfpm.log
      session.save_handler = memcached
      session.save_path = "127.0.0.1:11211"
    '';
    globalSettings = {
      "rlimit_core" = "unlimited";
      "rlimit_files" = "131072";
    };
    pools = {
      "site-01" = {
        phpPackage = pkgs.php;
        user = "wwwrun";
        group = "wwwrun";
        phpOptions = ''
          error_log = /var/log/phpfpm-site-01.log
        '';
        settings = {
          "listen.owner" = "${cfgUser01}";
          "listen.group" = "${cfgGroup01}";
          "listen.mode" = "0600";
        };
      };
    };
  };

And here it is easy to deteсt which parameters belong to global:

  fpmCfgFile = pool: poolOpts: pkgs.writeText "phpfpm-${pool}.conf" ''
    [global]
    ${concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${toStr v}") cfg.globalSettings)}
    ${optionalString (cfg.extraConfig != null) cfg.extraConfig}

    [${pool}]
    ${concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${toStr v}") poolOpts.settings)}
    ${concatStringsSep "\n" (mapAttrsToList (n: v: "env[${n}] = ${toStr v}") poolOpts.phpEnv)}
    ${optionalString (poolOpts.extraConfig != null) poolOpts.extraConfig}
  '';

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


Copy link
Contributor

@etu etu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as @aanderse.

I don't see the point in this rename and I think we shouldn't do it.

Maybe it's possible to do if we do a big refactor or something at the same time so we actually gain some more benefits from the module. But as far as I can see from reading the diff here... we don't really change anything that much really...

@Izorkin Izorkin changed the title nixos/phpfpm: fix and rename options nixos/phpfpm: fix apply global phpOptions Nov 5, 2019
@Izorkin
Copy link
Contributor Author

Izorkin commented Nov 5, 2019

Ok. Updated PR.

Copy link
Member

@aanderse aanderse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Izorkin please try this instead:

~/nixpkgs> git diff
diff --git a/nixos/modules/services/web-servers/phpfpm/default.nix b/nixos/modules/services/web-servers/phpfpm/default.nix
index 4ab7e3f0c0a..f33a740abeb 100644
--- a/nixos/modules/services/web-servers/phpfpm/default.nix
+++ b/nixos/modules/services/web-servers/phpfpm/default.nix
@@ -69,8 +69,6 @@ let
 
         phpOptions = mkOption {
           type = types.lines;
-          default = cfg.phpOptions;
-          defaultText = "config.services.phpfpm.phpOptions";
           description = ''
             "Options appended to the PHP configuration file <filename>php.ini</filename> used for this PHP-FPM pool."
           '';
@@ -137,6 +135,7 @@ let
       config = {
         socket = if poolOpts.listen == "" then "${runtimeDir}/${name}.sock" else poolOpts.listen;
         group = mkDefault poolOpts.user;
+        phpOptions = mkBefore cfg.phpOptions;
 
         settings = mapAttrs (name: mkDefault){
           listen = poolOpts.socket;

This would allow you to customize much further, more flexibility.

services.phpfpm.phpOptions = ''
  ; changes to apply to every php.ini file
'';
services.phpfpm.pools.pool1.phpOptions = ''
  ; changes which will be appended onto services.phpfpm.phpOptions for this pool
'';
services.phpfpm.pools.pool2.phpOptions = mkForce ''
  ; changes which will overwrite services.phpfpm.phpOptions for this pool
'';

@Izorkin
Copy link
Contributor Author

Izorkin commented Nov 5, 2019

Thanks for this variant. Updated PR.

@Izorkin
Copy link
Contributor Author

Izorkin commented Nov 5, 2019

This code:

        settings = mapAttrs (name: mkDefault){
          listen = poolOpts.socket;
          user = poolOpts.user;
          group = poolOpts.group;

generates a file in this form

[test-01]
group = wwwrun
listen = 9000
listen.allowed_clients = 127.0.0.1, 129.0.0.223, 129.0.0.48
listen.backlog = 65536
listen.group = wwwrun
listen.mode = 0660
listen.owner = wwwrun
pm = dynamic
pm.max_children = 20
pm.max_requests = 400
pm.max_spare_servers = 12
pm.min_spare_servers = 2
pm.start_servers = 4
user = wwwrun

How to make lines displayed first:

[test-01]
listen = 9000
user = wwwrun
group = wwwrun
listen = 9000
listen.allowed_clients = 127.0.0.1, 129.0.0.223, 129.0.0.48
listen.backlog = 65536
listen.group = wwwrun
listen.mode = 0660
listen.owner = wwwrun
pm = dynamic
pm.max_children = 20
pm.max_requests = 400
pm.max_spare_servers = 12
pm.min_spare_servers = 2
pm.start_servers = 4

@aanderse aanderse requested a review from etu November 10, 2019 14:12
@infinisil
Copy link
Member

For future reference: Attribute sets in Nix are sorted alphabetically and there's no nice and easy way to change that for something like this.

@aanderse aanderse merged commit d68d23b into NixOS:master Nov 11, 2019
@Izorkin Izorkin deleted the phpfpm-fix branch November 12, 2019 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants