Jump to content

Hugo

From NixOS Wiki
Revision as of 15:55, 17 May 2025 by SigmaSquadron (talk | contribs) (Imported from the old wiki. (Except off-topic parts))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Hugo is one of the most popular open-source static site generators.

The easiest way to use Hugo on NixOS is to add Hugo to the environment.systemPackages attribute:

❄︎ configuration.nix
{ pkgs, ... }: {
  environment.systemPackages = [
    pkgs.git
    pkgs.hugo
  ];
}
🟆︎
Tip: In order to affect your NixOS system by your nix-language-specific changes you must first evaluate it:
$ nixos-rebuild switch --use-remote-sudo

You can now follow the Hugo Quick Start guide, as not much is different from running Hugo on other Linux distributions.

Development Shell

You may, however, want to limit the installation to your Hugo-specific projects. This makes it possible for you or others to download your repository and work with its dependencies, since they are fully specified within the project.

❄︎ shell.nix
{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShellNoCC {
    packages = [
      pkgs.hugo
      # Other dependencies for your project.
    ];
  }

To avoid having to type nix-shell or nix develop to access the development shell, consider enabling nix-direnv.

Theming

Nix can be used to deterministically import themes by pinning them to a specific revision and linking the resulting derivation:

❄︎ shell.nix
{ pkgs ? import <nixpkgs> {} }: let
  hugo-theme = pkgs.fetchFromGitHub {
    owner = "vaga";
    repo = "hugo-theme-m10c";
    rev = "862c6e941be9bc46ce8adc6a2fa9e984ba647d6f";
    hash = "sha256-wcJSGjL/u43hPLblPmUhusqiMmadVBWJhihRinRXqzg=";
  };
in
  pkgs.mkShellNoCC {
    packages = [
      pkgs.hugo
      # Other dependencies for your project.
    ];

    shellHook = ''
      mkdir -p themes
      ln -snf "${hugo-theme}" themes/m10c
    '';
  }

The theme can be activated using hugo new site . --force, after creating a hugo.toml similar to the following:

≡︎ hugo.toml
baseURL = 'https://example.com/'
languageCode = 'en-us'
title = 'example-site'
theme = 'm10c'