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

Nginx formatter #34985

Closed
wants to merge 3 commits into from
Closed

Nginx formatter #34985

wants to merge 3 commits into from

Conversation

Baughn
Copy link
Contributor

@Baughn Baughn commented Feb 14, 2018

Motivation for this change

This makes nginx.conf human-readable.

While nominally the file is not meant for human consumption, in practice I commonly need to manually inspect the output of complex nginx configurations to verify that it actually looks the way I want. Being able to read it is therefore useful.

Before formatting: http://ix.io/K9v
After formatting: http://ix.io/K9k

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option build-use-sandbox in nix.conf on non-NixOS)
  • 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 nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Tested on a live system (https://madoka.brage.info), using cherry-pick against nixos-unstable release.
  • Fits CONTRIBUTING.md.

@grahamc
Copy link
Member

grahamc commented Feb 14, 2018

@GrahamcOfBorg test nginx

@GrahamcOfBorg
Copy link

Success on x86_64-linux (full log)

Partial log (click to expand)

syncing
webserver: running command: sync
webserver# [    8.165974] dhcpcd[781]: eth0: soliciting an IPv6 router
webserver: exit status 0
test script finished in 9.13s
cleaning up
killing webserver (pid 593)
vde_switch: EOF on stdin, cleaning up and exiting
vde_switch: Could not remove ctl dir '/tmp/nix-build-vm-test-run-nginx.drv-0/vde1.ctl': Directory not empty
/nix/store/rgn11hzfg47r61lgx1l63fs5d272r3z5-vm-test-run-nginx

@GrahamcOfBorg
Copy link

Success on aarch64-linux (full log)

Partial log (click to expand)

webserver: exit status 1
syncing
webserver: running command: sync
webserver: exit status 0
test script finished in 17.67s
cleaning up
killing webserver (pid 627)
vde_switch: EOF on stdin, cleaning up and exiting
vde_switch: Could not remove ctl dir '/build/vde1.ctl': Directory not empty
/nix/store/fmv3v3gjzli3zacza0dyg0i9jrgk34z9-vm-test-run-nginx

@grahamc
Copy link
Member

grahamc commented Feb 14, 2018

Very cool!

@nh2
Copy link
Contributor

nh2 commented Feb 15, 2018

Could we add an option for it so it can be turned off?

I want to use it myself, but I know that others (e362a3d#commitcomment-20787179) would like to not have to hard-depend on Python for changing nginx configs.

@Mic92
Copy link
Member

Mic92 commented Feb 15, 2018

Maybe make it opt-in? Everybody who cares about it, can temporary turn it on or leave it on all the time.

This reverts commit 6e12406, and causes the
nginx test to fail.
This fixes the nginx test failure.
@Baughn
Copy link
Contributor Author

Baughn commented Feb 15, 2018

I've added an option.

I'd prefer to not default it to false, as anyone attempting to debug their nginx configs is unlikely to guess that the option exists, given that most other NixOS config files don't have this functionality.

(Which is a problem in itself, but not in scope of this PR.)

@Baughn
Copy link
Contributor Author

Baughn commented Feb 15, 2018

@GrahamcOfBorg test nginx

1 similar comment
@joachifm
Copy link
Contributor

@GrahamcOfBorg test nginx

@GrahamcOfBorg
Copy link

Success on x86_64-linux (full log)

Partial log (click to expand)

webserver: running command: sync
webserver# [  OK  ] Listening on Load/Save RF Kill Switch Status /dev/rfkill Watch.
webserver# [    5.383236] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input4
webserver: exit status 0
test script finished in 6.12s
cleaning up
killing webserver (pid 593)
vde_switch: EOF on stdin, cleaning up and exiting
vde_switch: Could not remove ctl dir '/build/vde1.ctl': Directory not empty
/nix/store/7aww8iip618n3arzlaicg9zvwpwmgnki-vm-test-run-nginx

@GrahamcOfBorg
Copy link

Success on aarch64-linux (full log)

Partial log (click to expand)

webserver: exit status 1
syncing
webserver: running command: sync
webserver: exit status 0
test script finished in 17.96s
cleaning up
killing webserver (pid 627)
vde_switch: EOF on stdin, cleaning up and exiting
vde_switch: Could not remove ctl dir '/build/vde1.ctl': Directory not empty
/nix/store/vqbpglkrynbzh0xilj8vfhhww7200dzn-vm-test-run-nginx

@joachifm joachifm requested a review from globin February 17, 2018 09:29
@Mic92
Copy link
Member

Mic92 commented Feb 21, 2018

@Baughn ok. In the unlikely case of bugs in the formatter, it could then at least be disabled.


configFileFormatted = pkgs.runCommand "nginx.conf" {
inherit configFileUnformatted;
passAsFile = [ "configFileUnformatted" ];
Copy link
Member

Choose a reason for hiding this comment

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

I do not think that these two lines are needed because the reference to configFileUnformatted in the multi-line string below is directly to the store path that writeText creates a few lines further down.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Baughn I can confirm the above two lines are not necessary.

Could you remove them? I would love to have this merged.

Copy link
Member

@timokau timokau left a comment

Choose a reason for hiding this comment

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

See other review (just marking as changes requested).

@infinisil
Copy link
Member

I'm really not in favor of this. Both pulling in python and having a new option sound like bad solutions to the problem. When we are the ones who generate such a crippled Nix file, we should also be responsible for fixing it!

The solution I'm proposing is to generate the file in a way similar to what I've done in #45470. That is, having a config option with a type of a nix value representing the configuration file in a reasonable way, then converting this to a list of lines and concatenate them all.

As a side effect this has the advantage that users are able to override default settings easily. A slight problem with this is that verbatim options can't be formatted this way, but if such a config option exists, users may not even need many verbatim options anymore anyways. Also it's a great integration into the module system with almost no cost.

As an example, this is how the start of the config as specified in nginx/default.nix could look like:

{
  config = {
    user = "${cfg.user} ${cfg.group}";
    error_log = cfg.logError;
    daemon = false;
    
    # No need for null check, null values automatically have no effect
    events = cfg.eventsConfig;
    
    http = mkIf cfg.recommendedOptimisation {
      sendfile = true;
      tcp_nopush = true;
      tcp_nodelay = true;
      keepalive_timeout = 65;
      types_hash_max_size = 2048;
    } // {
      # A list generates a key-value entry for every element
      include = [
        "${cfg.package}/conf/mime.types"
        "${cfg.package}/conf/fastcgi.conf"
        "${cfg.package}/conf/uwsgi_params"
      ];
      resolver = mkIf (cfg.resolver.addresses != [])
        "${toString cfg.resolver.addresses} ${optionalString (cfg.resolver.valid != "") "valid=${cfg.resolver.valid}"}";
      
      ssl_protocols = cfg.sslProtocols;
    };
  };
}

@aanderse
Copy link
Member

Maybe this should be closed as #57979 was merged?

@timokau timokau closed this Apr 10, 2019
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

10 participants