Skip to content

Instantly share code, notes, and snippets.

@danbst
Created April 8, 2017 07:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbst/f1e81358d5dd0ba9c763a950e91a25d0 to your computer and use it in GitHub Desktop.
Save danbst/f1e81358d5dd0ba9c763a950e91a25d0 to your computer and use it in GitHub Desktop.
Extending NixOS submodules
# this example adds an option `.tomcatProxy` in namespace
# services.nginx.virtualHosts.*.locations.*
# that acts as alternative to specifying actual nginx config in .extraConfig
#
# execute with
/*
cat $(grep -o "/nix/store/[a-z0-9]*-nginx.conf" \
$(NIX_PATH=nixos-config=./example.nix:$NIX_PATH \
nix-build --no-out-link -E '
with import <nixpkgs/nixos> {};
config.system.build.toplevel
')/etc/systemd/system/nginx.service)
*/
# Thanks to @nbp (https://github.com/NixOS/nixpkgs/issues/24653#issuecomment-292684727)
{ lib, config, pkgs, ...}:
let
locationOptions = with lib; { config, ...}: {
options.tomcatProxy = mkOption {
type = types.bool;
default = false;
};
config.extraConfig = mkIf config.tomcatProxy ''
proxy_set_header Content-Type "application/x-www-form-urlencoded;charset=utf-8";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
'';
};
in {
options = with lib; with types; {
services.nginx.virtualHosts = mkOption {
type = attrsOf (submodule {
options.locations = mkOption {
type = attrsOf (submodule locationOptions);
};
});
};
};
config = {
fileSystems."/".device = "nodev";
boot.loader.grub.devices = [ "nodev" ];
services.nginx.enable = true;
services.nginx.virtualHosts."example.com" = {
locations."/" = {
proxyPass = "http://app.container:8080/servlet";
tomcatProxy = true;
};
};
};
}
events {
}
http {
include /nix/store/z5fxncldz06hdq4jsb6xsh3d248r8i4p-nginx-1.10.1/conf/mime.types;
include /nix/store/z5fxncldz06hdq4jsb6xsh3d248r8i4p-nginx-1.10.1/conf/fastcgi.conf;
ssl_protocols TLSv1.2;
ssl_ciphers EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL;
client_max_body_size 10m;
server_tokens off;
server {
listen 80;
listen [::]:80;
server_name example.com ;
location / {
proxy_pass http://app.container:8080/servlet;
proxy_set_header Content-Type "application/x-www-form-urlencoded;charset=utf-8";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment