Skip to content

Commit

Permalink
nerd-fonts: add individual font packages
Browse files Browse the repository at this point in the history
  • Loading branch information
rc-zb committed Nov 8, 2024
1 parent 604551f commit efb304d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pkgs/data/fonts/nerd-fonts/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
lib,
stdenvNoCC,
fetchzip,
}:

let
releaseVersion = lib.trivial.importJSON ./release-version.json;
makeNerdFont =
{
pname,
version,
description,
license,
url,
sha256,
}:
stdenvNoCC.mkDerivation {
inherit pname version;

src = fetchzip {
inherit url sha256;
stripRoot = false;
};

installPhase = ''
runHook preInstall
dst_opentype=$out/share/fonts/opentype/${pname}
dst_truetype=$out/share/fonts/truetype/${pname}
find -name \*.otf -exec mkdir -p $dst_opentype \; -exec cp -p {} $dst_opentype \;
find -name \*.ttf -exec mkdir -p $dst_truetype \; -exec cp -p {} $dst_truetype \;
runHook postInstall
'';

passthru.updateScript = ./update.sh;

meta = {
inherit description license;
homepage = "https://nerdfonts.com/";
changelog = "https://github.com/ryanoasis/nerd-fonts/blob/v${releaseVersion}/changelog.md";
maintainers = with lib.maintainers; [ rc-zb ];
};
};
in

builtins.listToAttrs (
map (font: lib.attrsets.nameValuePair font.pname (makeNerdFont font)) (
lib.trivial.importJSON ./fonts.json
)
)
1 change: 1 addition & 0 deletions pkgs/data/fonts/nerd-fonts/fonts.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkgs/data/fonts/nerd-fonts/release-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"3.2.1"
61 changes: 61 additions & 0 deletions pkgs/data/fonts/nerd-fonts/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const FORMER_RELEASE_VERSION = JSON.parse(std.in.getline());
const RELEASE_INFO_URL = "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest";
const PKGS_INFO_FILENAME = "fonts.json";

const getFontsInfoUrl = (tag) => `https://raw.githubusercontent.com/ryanoasis/nerd-fonts/refs/tags/${tag}/bin/scripts/lib/fonts.json`;

const fetchJSON = (url) => JSON.parse(std.urlGet(url).split("\r\n").at(-1));

const convertFontName = (formerName) => {
if (/^\d/u.test(formerName)) {
return "_" + formerName;
} else {
return formerName;
}
};

const convertFontVersion = (formerVersion, dateString) => {
if (/^\d/u.test(formerVersion)) {
return formerVersion;
} else {
return "0-unstable-" + dateString;
}
};

let releaseInfo = fetchJSON(RELEASE_INFO_URL);
let tag = releaseInfo.tag_name;
let releaseVersion = tag.split("v")[1];

if (releaseVersion === FORMER_RELEASE_VERSION) {
std.exit(1);
} else {
let fonts = fetchJSON(getFontsInfoUrl(tag)).fonts;
let pkgsInfo = fonts.map(font => {
let url = releaseInfo.assets
.find(asset => asset.name === font.folderName + ".tar.xz")
.browser_download_url;

let sha256 = (() => {
let f = std.popen(`nix-prefetch-url ${url} --unpack`, "r");
let result = f.getline();
f.close();
return result;
})();

return ({
pname: convertFontName(font.caskName),
version: convertFontVersion(font.version, releaseInfo.published_at.split("T")[0]),
description: font.description,
license: font.licenseId,
url: url,
sha256: sha256,
});
});

(() => {
let f = std.open(PKGS_INFO_FILENAME, "w");
f.puts(JSON.stringify(pkgsInfo));
f.close();
})()
print(JSON.stringify(releaseVersion));
}
8 changes: 8 additions & 0 deletions pkgs/data/fonts/nerd-fonts/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /usr/bin/env bash
set -eu -o pipefail
dir_name="$(dirname "$0")"
update_script_filename=update.js
release_version_filename=release-version.json

cd "$dir_name"
cat $release_version_filename | qjs --script --std $update_script_filename | tail -n 1 > $release_version_filename
1 change: 1 addition & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10102,6 +10102,7 @@ with pkgs;

nerdfix = callPackage ../tools/text/nerdfix { };

nerd-fonts = callPackage ../data/fonts/nerd-fonts { };
nerdfonts = callPackage ../data/fonts/nerd-fonts/legacy { };

netatalk = callPackage ../tools/filesystems/netatalk { };
Expand Down

0 comments on commit efb304d

Please sign in to comment.