fonts/nix/buildLocalFont.nix

59 lines
1.4 KiB
Nix
Raw Normal View History

2024-07-15 19:53:04 +00:00
{ lib, stdenvNoCC }:
{
name,
files ? [ ],
dir ? null,
license,
author,
}:
let
types = {
ttf = "truetype";
otf = "opentype";
};
pathEndsWith =
suffix: path:
let
str = toString path;
sufLen = lib.stringLength suffix;
sLen = lib.stringLength str;
in
sufLen <= sLen && suffix == lib.substring (sLen - sufLen) sufLen str;
copyType =
extension: pathName:
let
filteredFiles = lib.filter (pathEndsWith ("." + extension)) files;
filename = file: lib.lists.last (lib.strings.splitString "/" (toString file));
in
(lib.optionalString (filteredFiles != [ ]) ''
mkdir -p $out/share/fonts/${pathName}
'')
+ lib.concatStringsSep "\n" (
map (file: "cp -v ${file} $out/share/fonts/${pathName}/${filename file}") filteredFiles
);
copyTypeDir = extension: pathName: ''
if [ -n "$(shopt -s nullglob; echo ${dir}/*.${extension})" ]; then
mkdir -p $out/share/fonts/${pathName}
cp -v -t $out/share/fonts/${pathName} ${dir}/*.${extension}
fi
'';
in
stdenvNoCC.mkDerivation {
inherit name;
dontUnpack = true;
installPhase =
lib.mapAttrsToList copyType types
++ lib.optional (dir != null) (lib.mapAttrsToList copyTypeDir types);
passthru.font = true;
meta = with lib; {
inherit license;
platforms = platforms.all;
maintainers = [ maintainers.artemist ];
description = "${name} font by ${author}";
};
}