Skip to content

Conversation

Izorkin
Copy link
Contributor

@Izorkin Izorkin commented Sep 12, 2024

Description of changes

Allow configure and use custom mime-types.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Sep 12, 2024
Copy link
Member

@pbsds pbsds left a comment

Choose a reason for hiding this comment

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

Instead of conditioning the use of defaultMimeTypes on the custom mime-types being empty, would it make sense to make defaultMimeTypes nullable?

@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from 6a91bc2 to c223d28 Compare September 12, 2024 18:28
@Izorkin
Copy link
Contributor Author

Izorkin commented Sep 12, 2024

Instead of conditioning the use of defaultMimeTypes on the custom mime-types being empty, would it make sense to make defaultMimeTypes nullable?

If defaultMimeTypes is not disabled then new types will be added to existing types and cannot be changed.

The defaultMimeTypes parameter was added in an attempt to reassign to a new list of mime-types in the future, but failed to add the media-types package.

@pbsds
Copy link
Member

pbsds commented Sep 12, 2024

A common use case I see is that one would like to keep the default mime-types, but add one or two additional file types.

@Izorkin
Copy link
Contributor Author

Izorkin commented Sep 12, 2024

A common use case I see is that one would like to keep the default mime-types, but add one or two additional file types.

Add another parameter addMimeTypes? Or the ability to specify to use null for defaultMimeTypes?

@pbsds
Copy link
Member

pbsds commented Sep 12, 2024

This is what i envison:

In addition to defaults:

{ services.nginx = {
    mimeTypes."model/stl" = [ ".stl" ];
}; }

Replacing defaults:

{ services.nginx = {
    defaultMimeTypes = null;
    mimeTypes."model/stl" = [ ".stl" ];
}; }

with a warning in mimeTypes.description that conflicts with defaultMimeTypes are to be expected.

This is only my preference however, and maybe code-owner @RaitoBezarius have a different opinion

@pbsds
Copy link
Member

pbsds commented Sep 12, 2024

Perhaps renaming defaultMimeTypes to mimeTypeIncludeFiles, and make it a listOf path?

@ofborg ofborg bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Sep 12, 2024
@Izorkin
Copy link
Contributor Author

Izorkin commented Sep 13, 2024

Allow this variant?

diff --git a/nixos/modules/services/web-servers/nginx/default.nix b/nixos/modules/services/web-servers/nginx/default.nix
index f523833e5b66..5b425c147b6d 100644
--- a/nixos/modules/services/web-servers/nginx/default.nix
+++ b/nixos/modules/services/web-servers/nginx/default.nix
@@ -128,6 +128,10 @@ let
     }
   ''));

+  addMimeTypeConfig = lib.concatLines (flip mapAttrsToList cfg.addMimeTypes (addMimeType: extensions: ''
+    ${addMimeType} ${concatStringsSep " " extensions};
+  ''));
+
   mimeTypeConfig = lib.concatLines (flip mapAttrsToList cfg.mimeTypes (mimeType: extensions: ''
     ${mimeType} ${concatStringsSep " " extensions};
   ''));
@@ -136,7 +140,10 @@ let
       # Load mime types.
       default_type application/octet-stream;
       ${optionalString (cfg.mimeTypes == {}) "include ${cfg.defaultMimeTypes};"}
-      ${optionalString (cfg.mimeTypes != {}) "types {\n${mimeTypeConfig}\n}"}
+      ${optionalString (cfg.mimeTypes != {} || cfg.addMimeTypes != {}) "types {\n"}
+      ${optionalString (cfg.addMimeTypes != {} && cfg.mimeTypes == {}) "${addMimeTypeConfig}"}
+      ${optionalString (cfg.mimeTypes != {}) "${mimeTypeConfig}"}
+      ${optionalString (cfg.mimeTypes != {} || cfg.addMimeTypes != {}) "\n}"}
       # When recommendedOptimisation is disabled nginx fails to start because the mailmap mime.types database
       # contains 1026 entries and the default is only 1024. Setting to a higher number to remove the need to
       # overwrite it because nginx does not allow duplicated settings.
@@ -646,6 +653,19 @@ in
         '';
       };

+      addMimeTypes = mkOption {
+        type = with types; submodule {
+          freeformType = types.attrsOf (types.listOf str);
+        };
+        default = {};
+        example = literalExpression ''
+          "model/stl" = [ "stl" ];
+        '';
+        description = ''
+          Enabling custom mime types to an existing list {option}`services.nginx.defaultMimeTypes`.
+        '';
+      };
+
       mimeTypes = mkOption {
         type = with types; submodule {
           freeformType = types.attrsOf (types.listOf str);

@Izorkin
Copy link
Contributor Author

Izorkin commented Sep 13, 2024

Perhaps renaming defaultMimeTypes to mimeTypeIncludeFiles, and make it a listOf path?

Convert the ${pkgs.mailcap}/etc/nginx/mime.types file to this form?

  mimeTypes = {
    "application/A2L"= [ "a2l" ];
    "application/AML"= [ "aml" ];
    "application/andrew-inset"= [ "ez" ];
    ...

@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from c223d28 to f432ef3 Compare September 13, 2024 13:32
@github-actions github-actions bot added 8.has: documentation This PR adds or changes documentation 8.has: changelog This PR adds or changes release notes labels Sep 13, 2024
@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from 322f08f to 883421c Compare September 13, 2024 13:44
@pbsds
Copy link
Member

pbsds commented Sep 16, 2024

I don't like having both mimeTypes and addMimeTypes, i think the types.nullOr types.path idea makes more sense. But a problem i see we now run into is that

types {
    foo/bar baz;
    foo/qux lux;
}

and

types {
    foo/bar baz;
}
types {
    foo/qux lux;
}

are not equivalent, since the second types block overwrites the first one rather than merge with it. Please correct me if I am wrong about this.

That means the initial version of the PR was more correct, but still a major UX footgun.
So the choice is either to go with the prior approach and add a loud warning, or engineer some mimetype merging tool.

Convert the ${pkgs.mailcap}/etc/nginx/mime.types file to this form?

That would mean essentially vendoring the mailcap mimetypes list to avoid IFD, which puts its maintanance burden on us. (This could be aided with a test to check for equivalence with the one in mailcap.)

What about perhaps a scripted approach? Meaning a script that extracts the content of ${pkgs.mailcap}/etc/nginx/mime.types and joins it with the proposed services.nginx.mimeTypes? As a start I made sed -zE 's/^types\s*\{(.*)\}\s*$/\1/', but one problem with it is that it does not error if it finds no match...

@Izorkin
Copy link
Contributor Author

Izorkin commented Sep 26, 2024

types {
foo/bar baz;
}
types {
foo/qux lux;
}

In this variant, both types are added.

What about perhaps a scripted approach? Meaning a script that extracts the content of ${pkgs.mailcap}/etc/nginx/mime.types and joins it with the proposed services.nginx.mimeTypes? As a start I made sed -zE 's/^types\s*{(.)}\s$/\1/', but one problem with it is that it does not error if it finds no match...

I tried to do something similar here:
#203433

@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from 883421c to ec8c8fa Compare September 26, 2024 16:49
@Izorkin
Copy link
Contributor Author

Izorkin commented Sep 26, 2024

Removed parametr 'addMimeTypes'.

A common use case I see is that one would like to keep the default mime-types, but add one or two additional file types.

Сould try using an variant like this. Tested it, it works.

  commonHttpConfig = ''
    types {
      foo/qux lux;
    }
  '';

I don't know any other simple options for changing mime-types yet.

@Izorkin Izorkin requested a review from pbsds September 26, 2024 16:58
@Izorkin Izorkin force-pushed the add-nginx-mime-types branch 2 times, most recently from dfecc89 to 03675ef Compare September 26, 2024 17:38
@Izorkin Izorkin removed the request for review from pbsds September 28, 2024 10:51
@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. labels Jul 13, 2025
@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from 2ebb777 to 8b24a2b Compare July 13, 2025 07:28
@Izorkin Izorkin requested review from pbsds, SuperSandro2000, Ma27 and fpletz and removed request for fpletz, pbsds and SuperSandro2000 July 13, 2025 07:38
@Ma27
Copy link
Member

Ma27 commented Jul 19, 2025

In this variant, both types are added.

I don't get it. If that's the case, what's wrong with @pbsds's suggestion from #341414 (comment)

@Izorkin
Copy link
Contributor Author

Izorkin commented Jul 19, 2025

I don't get it. If that's the case, what's wrong with @pbsds's suggestion from #341414 (comment)

To reduce the likelihood of conflicts in mime parameters. This is an alternative method of managing mime types, which allows you to specify the minimum required for the site to work.

@Ma27
Copy link
Member

Ma27 commented Jul 20, 2025

To reduce the likelihood of conflicts in mime parameters

Well, for that you may set the default mime types to null or not in the proposal, right?

Having this explicit seems less surprising to me.

@Izorkin
Copy link
Contributor Author

Izorkin commented Jul 21, 2025

Well, for that you may set the default mime types to null or not in the proposal, right?

Something like this?
#341414 (comment)

@Ma27
Copy link
Member

Ma27 commented Jul 21, 2025

As mentioned above, I'm referring to #341414 (comment).

@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from 8b24a2b to c76a5ef Compare July 21, 2025 07:18
@Izorkin
Copy link
Contributor Author

Izorkin commented Jul 21, 2025

Update PR.

@Izorkin Izorkin changed the title nixos/nginx: add option mimeTypes nixos/nginx: add option addMimeTypes Jul 21, 2025
@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from c76a5ef to f4e3296 Compare July 21, 2025 07:20
@Izorkin
Copy link
Contributor Author

Izorkin commented Jul 21, 2025

But I would prefer the previous variant :(

@Izorkin Izorkin force-pushed the add-nginx-mime-types branch 3 times, most recently from bc92f3b to e420326 Compare July 21, 2025 07:47
@Izorkin Izorkin force-pushed the add-nginx-mime-types branch from e420326 to 014acd0 Compare July 21, 2025 08:14
@nixpkgs-ci nixpkgs-ci bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 31, 2025
@nixpkgs-ci nixpkgs-ci bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Aug 11, 2025
@@ -766,6 +776,23 @@ in
'';
};

addMimeTypes = mkOption {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this still addMimeTypes?

This isn't necessarily additive.

description = ''
Enabling custom mime types to an existing list {option}`services.nginx.defaultMimeTypes`.
This option does not allow overriding existing mime-types in
{option}`services.nginx.defaultMimeTypes` and may cause duplicate mime-types.
Copy link
Member

Choose a reason for hiding this comment

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

The description needs an update: this behavior can be changed by setting defaultMimeTypes to null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants