nixos/httpd: add services.httpd.virtualHosts.<name>.locations option to match nginx

Changed files
+114 -7
nixos
modules
services
+28 -6
nixos/modules/services/web-servers/apache-httpd/default.nix
···
then hostOpts.documentRoot
else pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out"
;
+
+
mkLocations = locations: concatStringsSep "\n" (map (config: ''
+
<Location ${config.location}>
+
${optionalString (config.proxyPass != null) ''
+
<IfModule mod_proxy.c>
+
ProxyPass ${config.proxyPass}
+
ProxyPassReverse ${config.proxyPass}
+
</IfModule>
+
''}
+
${optionalString (config.index != null) ''
+
<IfModule mod_dir.c>
+
DirectoryIndex ${config.index}
+
</IfModule>
+
''}
+
${optionalString (config.alias != null) ''
+
<IfModule mod_alias.c>
+
Alias "${config.alias}"
+
</IfModule>
+
''}
+
${config.extraConfig}
+
</Location>
+
'') (sortProperties (mapAttrsToList (k: v: v // { location = k; }) locations)));
in
''
${optionalString mainCfg.logPerVirtualHost ''
···
''}
${
-
let makeFileConf = elem: ''
-
Alias ${elem.urlPath} ${elem.file}
-
'';
-
in concatMapStrings makeFileConf hostOpts.servedFiles
-
}
-
${
let makeDirConf = elem: ''
Alias ${elem.urlPath} ${elem.dir}/
<Directory ${elem.dir}>
···
in concatMapStrings makeDirConf hostOpts.servedDirs
}
+
${mkLocations hostOpts.locations}
${hostOpts.extraConfig}
''
;
···
'';
}
];
+
+
warnings =
+
mapAttrsToList (name: hostOpts: ''
+
Using config.services.httpd.virtualHosts."${name}".servedFiles is deprecated and will become unsupported in a future release. Your configuration will continue to work as is but please migrate your configuration to config.services.httpd.virtualHosts."${name}".locations before the 20.09 release of NixOS.
+
'') (filterAttrs (name: hostOpts: hostOpts.servedFiles != []) mainCfg.virtualHosts);
users.users = optionalAttrs (mainCfg.user == "wwwrun") {
wwwrun = {
+54
nixos/modules/services/web-servers/apache-httpd/location-options.nix
···
+
{ config, lib, name, ... }:
+
let
+
inherit (lib) mkOption types;
+
in
+
{
+
options = {
+
+
proxyPass = mkOption {
+
type = with types; nullOr str;
+
default = null;
+
example = "http://www.example.org/";
+
description = ''
+
Sets up a simple reverse proxy as described by <link xlink:href="https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html#simple" />.
+
'';
+
};
+
+
index = mkOption {
+
type = with types; nullOr str;
+
default = null;
+
example = "index.php index.html";
+
description = ''
+
Adds DirectoryIndex directive. See <link xlink:href="https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryindex" />.
+
'';
+
};
+
+
alias = mkOption {
+
type = with types; nullOr path;
+
default = null;
+
example = "/your/alias/directory";
+
description = ''
+
Alias directory for requests. See <link xlink:href="https://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias" />.
+
'';
+
};
+
+
extraConfig = mkOption {
+
type = types.lines;
+
default = "";
+
description = ''
+
These lines go to the end of the location verbatim.
+
'';
+
};
+
+
priority = mkOption {
+
type = types.int;
+
default = 1000;
+
description = ''
+
Order of this location block in relation to the others in the vhost.
+
The semantics are the same as with `lib.mkOrder`. Smaller values have
+
a greater priority.
+
'';
+
};
+
+
};
+
}
+32 -1
nixos/modules/services/web-servers/apache-httpd/per-server-options.nix
···
{ config, lib, name, ... }:
let
-
inherit (lib) mkOption types;
+
inherit (lib) literalExample mkOption nameValuePair types;
in
{
options = {
···
];
description = ''
This option provides a simple way to serve individual, static files.
+
+
<note><para>
+
This option has been deprecated and will be removed in a future
+
version of NixOS. You can achieve the same result by making use of
+
the <literal>locations.&lt;name&gt;.alias</literal> option.
+
</para></note>
'';
};
···
xlink:href='http://www.robotstxt.org/'/> for details.
'';
};
+
+
locations = mkOption {
+
type = with types; attrsOf (submodule (import ./location-options.nix));
+
default = {};
+
example = literalExample ''
+
{
+
"/" = {
+
proxyPass = "http://localhost:3000";
+
};
+
"/foo/bar.png" = {
+
alias = "/home/eelco/some-file.png";
+
};
+
};
+
'';
+
description = ''
+
Declarative location config. See <link
+
xlink:href="https://httpd.apache.org/docs/2.4/mod/core.html#location"/> for details.
+
'';
+
};
+
+
};
+
+
config = {
+
+
locations = builtins.listToAttrs (map (elem: nameValuePair elem.urlPath { alias = elem.file; }) config.servedFiles);
};
}