4o1x5.dev/content/post/guides/nix/setting-up-a-hugo-site-using-nix/index.en.md
2005 84df8d1671 remote deployments blog:
added a whole guide for getting started with nix remote deployments
fixed shortcodes view (still needs work)
2024-05-07 22:40:10 +02:00

1.7 KiB

title description date image categories tags draft
Setting up a SSG on NixOs with nginx Using hugo we can compile a project into a static site which can be later served with Nginx 2024-04-24 00:00:00+0000 daniele-levis-pelusi-YKsqkazGaOw-unsplash.jpg
Nix
Guide
Sysadmin
Nix
Nginx
Short
Hugo
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

# /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

# /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!

sudo nixos-rebuild switch