How do I configure default fonts for NixOS?

Hello friends!

I’m trying to configure my default fonts for my system (for readability), but I can’t quite seem to get the syntax correct that causes the configuration file to compile. I went with the ubuntu font for the sake of a basic test.

This is as far as I got configuration wise based off of research:

 fonts =
  {
    enableDefaultPackages = true;

    packages = with pkgs; [
    nerd-fonts.ubuntu
    ];

    fontconfig =
    {
      enable = true;

      defaultFonts =
      {
        monospace = nerd-fonts.ubuntu;
        sansSerif = nerd-fonts.ubuntu;
        serif = nerd-fonts.ubuntu;

      };
    };
  };

This is the bottom part of the stack trace, if it helps at all:

        error: undefined variable 'nerd-fonts'
       at /etc/nixos/configuration.nix:130:21:
          129|         monospace = nerd-fonts.ubuntu;
          130|         sansSerif = nerd-fonts.ubuntu;
             |                     ^
          131|         serif = nerd-fonts.ubuntu;
Command 'nix-build '<nixpkgs/nixos>' --attr config.system.build.nixos-rebuild --show-trace --no-out-link' returned non-zero exit status 1.

I suspect I’m misunderstanding how to reference fonts correctly? But I can’t find any more information online about this, even looking off of the github repo for Nerd Fonts (which seems to be a decent compilation of fonts)

I’m a total newbie at this operating system, so any further information about font configuration (or directions to where I can find that info) would be appreciated!

You need to use pkgs.nerd-fonts, nerd-fonts can’t come from nowhere.

Thank you, that was the primary thing I was missing! I was able to adjust the configuration to fix some other mistakes like so:

  fonts =
  {
    enableDefaultPackages = true;

    packages = with pkgs; [
    pkgs.nerd-fonts.ubuntu
    ];

    fontconfig =
    {
      enable = true;

      defaultFonts =
      {
        monospace = [ "pkgs.nerd-fonts.ubuntu-mono" ];
        sansSerif = [ "pkgs.nerd-fonts.ubuntu-sans" ];
        serif = [ "pkgs.nerd-fonts.ubuntu" ];

      };
    };
  };

So in general, when referring to members of packages, I should be referencing them with something like pkgs.[package].[field]in most cases?

    packages = with pkgs; [
    pkgs.nerd-fonts.ubuntu
    ];

Here the with.pkgs; already puts all members of pkgs into the namespace for creating the list, so you could write

    packages = with pkgs; [
    nerd-fonts.ubuntu
    ];

But I’d write

    packages =  [
    pkgs.nerd-fonts.ubuntu
    ];

Here you aren’t accessing the packages at all.

      {
        monospace = [ "pkgs.nerd-fonts.ubuntu-mono" ];
        sansSerif = [ "pkgs.nerd-fonts.ubuntu-sans" ];
        serif = [ "pkgs.nerd-fonts.ubuntu" ];

      };

But in this case you aren’t supposed to references the packages, but the names of the fonts.

You can read the wiki page for more information on how to configure fonts

Ah, I understand! So the with keyword prefixes all the members in there with a namespace. I’ll keep the keyword there since it’s a bit easier for me to read and better to reference off of in the future.

I amended how I defined the default fonts too. I also made sure to explicitly add the sans and mono variations to my packages too, since I missed that before.

defaultFonts =
      {
        monospace = [ "ubuntu-mono" ];
        sansSerif = [ "ubuntu-sans" ];
        serif = [ "ubuntu" ];
      };