Skip to content

Instantly share code, notes, and snippets.

@Mic92
Last active September 28, 2018 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/48a44c576e1df6ce85cadce7d1341ed4 to your computer and use it in GitHub Desktop.
Save Mic92/48a44c576e1df6ce85cadce7d1341ed4 to your computer and use it in GitHub Desktop.
Show pretty-printed nginx configuration on nixos.
#!/usr/bin/env nix-shell
#!nix-shell -p nginx-config-formatter python3 -i python3
import os
import re
import shutil
import subprocess
import sys
from tempfile import TemporaryDirectory
def nginx_config() -> str:
out = subprocess.check_output(["systemctl", "cat", "nginx"])
match = re.search(r"-c (\S+-nginx\.conf)", out.decode("utf-8"))
if not match:
print("Could not find nginx.conf in nginx.service", file=sys.stderr)
sys.exit(1)
return match.group(1)
def main():
config_path = nginx_config()
with TemporaryDirectory() as temp_dir:
temp_path = os.path.join(temp_dir, "nginx.conf")
with open(temp_path, "wb+") as temp_file, \
open(config_path, "rb") as config_file:
shutil.copyfileobj(config_file, temp_file)
temp_file.flush()
subprocess.check_call(["nginxfmt", temp_file.name])
editor = os.environ.get("EDITOR", "vim")
subprocess.check_call([editor, temp_file.name] + sys.argv[1:])
if __name__ == "__main__":
main()
@Mic92
Copy link
Author

Mic92 commented Sep 28, 2018

If you are using vim you can directly jump to syntax errors:

$ ./nix-config +<LINE_NUMBER>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment