Nix package deal creation: set up a not but supported font

Faheem

The Nix packages collection is giant with over 60 000 packages. Nonetheless, chances are high that typically the package deal you want just isn’t out there. You should combine it your self.

I wanted for some fonts which weren’t already current inside nixpkgs. In Nix, a font is distributed as a package deal like every other software program. One of many fonts to put in is Dancing Script. On the time of this writing, looking in nixpkgs doesn’t reveal any related match:

nix search nixpkgs dancing 

Whether or not your system is operating NixOS or simply utilizing the Nix package deal supervisor, as I do with macOS, making a Nix package deal isn’t all the time so onerous. The package deal we are going to create works for each Nix deployment. I present the instructions for Linux operating NixOS and for macOS utilizing nix-darwin. It’s definitely intimidating. You should get your palms soiled and write some code. Nonetheless, contemplating the Nix nature, the vast majority of us have already got just a few customized Nix configuration information. I’ll illustrate the method for creating and sharing a brand new Nix package deal with the group.

Fonts set up with NixOS

NixOS suggest many font packages. For a font to be seen to all of your functions, it should be registered contained in the fonts.fonts choices listing. For instance, out of your foremost configuration.nix file:

{ config, pkgs, ... }:

{
  imports =
    [
      ./david-framework-hardware.nix
    ];
  
  fonts.enableFontDir = true;
  fonts.fonts = with pkgs; [    dejavu_fonts    inter  ];  
}

Not each font is out there from nixpkgs. That is tips on how to set up a not but supported font contained in the fonts.fonts choice listing by creating our package deal. As soon as the package deal is prepared, we publish it!

Discovering an applicable instance

The NixOS documentation is complicated. It’s cut up between a number of areas. Typically it lacks fundamental info and typically it goes so deep into the main points that it’s onerous to grasp. Studying the supply code is an effective and advisable strategy to study concerning the language and to customise your atmosphere.

On each system, putting in a font is about downloading it and putting it within the applicable system location. Let’s learn how different fonts are put in. For instance, the Inter typeface. Lookup inter by means of the Nix search. There’s a source link that factors to the package deal contained in the nixpkgs repository:

{ lib, fetchzip }:

let
  model = "3.19";
in fetchzip {
  title = "inter-${model}";

  url = "https://github.com/rsms/inter/releases/obtain/v${model}/Inter-${model}.zip";

  postFetch = ''
    mkdir -p $out/share/fonts/opentype
    unzip -j $downloadedFile *.otf -d $out/share/fonts/opentype
  '';

  sha256 = "sha256-8p15thg3xyvCA/8dH2jGQoc54nzESFDyv5m47FgWrSI=";

  meta = with lib; {
    homepage = "https://rsms.me/inter/";
    description = "A typeface specifically designed for person interfaces";
    license = licenses.ofl;
    platforms = platforms.all;
    maintainers = with maintainers; [ demize dtzWill ];
  };
}

The package deal accommodates some metadata such because the package deal title, a hyperlink to the official venture, and an outline. The url and sha256 is utilized by fetchzip to obtain the font information. Each .otf file is extracted from the zip archive into the $out/share/fonts/opentype folder.

Making a package deal

Wanting on the Dancing Script repository, there isn’t any obtain out there. As an alternative of the fetchzip perform used to obtain a .zip archive, we use the fetchFromGitHub perform to obtain the snapshot of a GitHub repository.

The code is organized otherwise, with totally different properties, however it achieves the identical function. Subsequent to your configuration.nix file, create the dancing-script/default.nix file:

{ lib, fetchFromGitHub }:

let
  pname = "dancing-script";
  model = "2.0";
in fetchFromGitHub {
  title = "${pname}-${model}";
  
  proprietor = "impallari";
  repo = "DancingScript";
  rev = "f7f54bc1b8836601dae8696666bfacd306f77e34";
  sha256 = "dfFvh8h+oMhAQL9XKMrNr07VUkdQdxAsA8+q27KWWCA=";

  postFetch = ''
    tar xf $downloadedFile --strip=1
    set up -m444 -Dt $out/share/fonts/truetype fonts/ttf/*.ttf
  '';

  meta = with lib; {
    description = "Dancing Script";
    longDescription = "A energetic informal script the place the letters bounce and alter dimension barely.";
    homepage = "https://github.com/impallari/DancingScript";
    license = licenses.ofl;
    platforms = platforms.all;
    maintainers = with maintainers; [ wdavidw ];
  };
}

As soon as the GitHub repository is fetched, we set up all of the .ttf information into the $out/share/fonts/truetype folder.

Package deal declaration

Assuming that we’re enhancing a configuration.nix file collocated with the dancing-script folder, we import the brand new package deal to fonts.fonts choice listing:

{ config, pkgs, ... }:

let
  dancing-script = import ./dancing-script/default.nix {    inherit lib;    fetchFromGitHub = pkgs.fetchFromGitHub;  };in {
  imports =
    [
      ./david-framework-hardware.nix
    ];
  
  fonts.enableFontDir = true;
  fonts.fonts = with pkgs; [
    dancing-script    dejavu_fonts
    inter
  ];
  
}

Observe how we fulfill the package deal dependencies by injecting lib and fetchFromGitHub when calling import. A shorter and simpler strategy makes use of callPackage:

{ config, pkgs, ... }:

let
  dancing-script = pkgs.callPackage ./dancing-script/default.nix {  };in {
  imports =
    [
      ./david-framework-hardware.nix
    ];
  
  fonts.enableFontDir = true;
  fonts.fonts = with pkgs; [
    dancing-script    dejavu_fonts
    inter
  ];
  
}

Working nixos-rebuild change (or darwin-rebuild change on macOS) masses the font within the system. On Linux, fc-list lists the fonts out there within the system:

fc-list | grep dancing/nix/retailer/fm8y81bjhcy8p4cp32230xr78807x0ii-dancing-script-2.000/share/fonts/truetype/DancingScript-Daring.ttf: Dancing Script:model=Daring
/nix/retailer/fm8y81bjhcy8p4cp32230xr78807x0ii-dancing-script-2.000/share/fonts/truetype/DancingScript-Common.ttf: Dancing Script:model=Common

Package deal integration with nixpkgs

Now that our package deal is working, the subsequent step is to share our work with the group.

Begin by forking the nixpkgs repository and clone the fork in your machine:

git clone origin https://github.com/wdavidw/nixpkgs.git
cd nixpkgs

Insights on tips on how to contribute to the venture are written in CONTRIBUTING.md. Making a merge request to suggest a brand new package deal contain 3 commits:

  • creating the package deal Nix file
  • updating pkgs/top-level/all-packages.nix to register the package deal
  • updating the maintainers/maintainer-list.nix to register your self except already current

Inside pkgs/knowledge/fonts, create a brand new dancing-script folder and import the default.nix file current above.

Replace the all-packages.nix and maintainer-list.nix information as properly. Their content material is self-explanatory. Additionally, there may be some form of order inside these two information however it isn’t strictly enforced.

The previous register our new package deal:

  
  dancing-script = callPackage ../knowledge/fonts/dancing-script { };
  

The later register you as a contributor:

  
  wdavidw = {
    title = "David Worms";
    electronic mail = "david@adaltas.com";
    github = "wdavidw";
    githubId = 46896;
  };
  

Your githubId worth is uncovered by the GitHub API at https://api.github.com/customers/{username}, changing {username} along with your GitHub deal with.

The dancing-script package deal is now registered in nixpkgs.

Testing packages from nixpkgs

Earlier than submitting the pull request, it’s doable to affiliate and take a look at our Nix configuration with the native nixpkgs repository. Run the nixos-rebuild change command (or darwin-rebuild change on macOS) with the additional -I argument to reconfigure the machine with the native nixpkgs packages together with our newest addition.

First, we replace and simplify our configuration.nix file accordingly:

{ config, pkgs, ... }:

{
  imports =
    [
      ./david-framework-hardware.nix
    ];
  
  
  fonts.enableFontDir = true;
  fonts.fonts = with pkgs; [
    dancing-script
    dejavu_fonts
    inter
  ];
  
}

From the nixpkgs listing, the command to reconfigure the machine utilizing the native nixpkgs repository is:

nixos-rebuild change -I nixpkgs=.

Package deal publication

The method to share the modifications with the group is commonplace GitOps.

Adjustments are dedicated with:

git add 
  maintainers/maintainer-list.nix 
  pkgs/knowledge/fonts/dancing-script/default.nix 
  pkgs/top-level/all-packages.nix
git commit -m 'dancing-script: init at 2.0'
git push origin grasp

The commit message respects the rules present in CONTRIBUTING.md. Go to your GitHub repository and create the pull request.

Conclusion

The ultimate consequence was printed simply earlier than this text. The merge request is seen on-line as “PR #166057, dancing-script: init at 2.0”. It’s merged withing 24 hours and I’m now an official NixOS maintainer!

Leave a Comment