--- title: Setting up a SSG on NixOs with nginx description: Using hugo we can compile a project into a static site which can be later served with Nginx date: 2024-04-24 00:00:00+0000 image: daniele-levis-pelusi-YKsqkazGaOw-unsplash.jpg categories: - Nix - Guide tags: - Nix - Nginx - Short - Hugo draft: false --- ## Overview After conducting research and finding insufficient guidance online on how to properly set up a Hugo site with nginx, I decided to create my own guide. I will provide you with step-by-step instructions on how to compile your Hugo project into a static site, which can then be served through nginx's `root` option. ## Defining the derivation to build the site ```nix # /website/default.nix { pkgs }: pkgs.stdenv.mkDerivation rec { name = "website"; version = "0.1.0"; src = /home/user/website; buildInputs = with pkgs; [ hugo ]; dontConfigure = true; buildPhase = '' cp -r $src/* . ${pkgs.hugo}/bin/hugo ''; installPhase = '' mkdir -p $out cp -r public/* $out/ ''; } ``` ## Setting up Nginx ```nix # /services/nginx.nix {...}: { services.nginx = { enable = true; virtualHosts = { "example.com" = { forceSSL = true; enableACME = true; locations."/" = { # Relative path to current file root = pkgs.callPackage ../website/default.nix { }; }; # setting error page as hugo error page extraConfig = '' error_page 404 /404.html; ''; }; }; }; } ``` ## Rebuild the system After importing this module to your `flake.nix`/`configuration.nix` you can rebuild the system and see that the site is up! ```bash sudo nixos-rebuild switch ```