Merge pull request #76583 from aanderse/httpd-locations

nixos/httpd: add locations option to virtualHosts
This commit is contained in:
Aaron Andersen 2020-01-29 21:01:35 -05:00 committed by GitHub
commit 596e0fcb39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 7 deletions

View File

@ -179,6 +179,28 @@ let
then hostOpts.documentRoot then hostOpts.documentRoot
else pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out" 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 in
'' ''
${optionalString mainCfg.logPerVirtualHost '' ${optionalString mainCfg.logPerVirtualHost ''
@ -217,12 +239,6 @@ let
RedirectPermanent / ${hostOpts.globalRedirect} RedirectPermanent / ${hostOpts.globalRedirect}
''} ''}
${
let makeFileConf = elem: ''
Alias ${elem.urlPath} ${elem.file}
'';
in concatMapStrings makeFileConf hostOpts.servedFiles
}
${ ${
let makeDirConf = elem: '' let makeDirConf = elem: ''
Alias ${elem.urlPath} ${elem.dir}/ Alias ${elem.urlPath} ${elem.dir}/
@ -235,6 +251,7 @@ let
in concatMapStrings makeDirConf hostOpts.servedDirs in concatMapStrings makeDirConf hostOpts.servedDirs
} }
${mkLocations hostOpts.locations}
${hostOpts.extraConfig} ${hostOpts.extraConfig}
'' ''
; ;
@ -606,6 +623,11 @@ in
} }
]; ];
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") { users.users = optionalAttrs (mainCfg.user == "wwwrun") {
wwwrun = { wwwrun = {
group = mainCfg.group; group = mainCfg.group;

View File

@ -0,0 +1,54 @@
{ 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.
'';
};
};
}

View File

@ -1,6 +1,6 @@
{ config, lib, name, ... }: { config, lib, name, ... }:
let let
inherit (lib) mkOption types; inherit (lib) literalExample mkOption nameValuePair types;
in in
{ {
options = { options = {
@ -175,6 +175,12 @@ in
]; ];
description = '' description = ''
This option provides a simple way to serve individual, static files. 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>
''; '';
}; };
@ -231,5 +237,30 @@ in
''; '';
}; };
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);
}; };
} }