Skip to content

Commit

Permalink
flake: refactor and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 19, 2025
1 parent 2459e16 commit ffa8996
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 90 deletions.
24 changes: 12 additions & 12 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 10 additions & 78 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
imports = [
inputs.pkgs-by-name-for-flake-parts.flakeModule
./nix/imports/pkgs.nix
inputs.pkgs-by-name-for-flake-parts.flakeModule
];

perSystem =
Expand All @@ -34,90 +33,23 @@
# Change here to typst-dev if needed
typst = pkgs.nixpkgs-unstable.typst;

fontsConf = pkgs.symlinkJoin {
name = "typst-fonts";
paths = with pkgs; [
inriafonts
fg-virgil
liberation_ttf
nerd-fonts.inconsolata
newcomputermodern
];
};

mkBuildDocumentDrv =
documentName:
pkgs.stdenvNoCC.mkDerivation {
name = "build-" + documentName;

src = pkgs.lib.cleanSource ./.;

buildInputs = [ typst ];

buildPhase = ''
runHook preBuild
${lib.getExe typst} \
compile \
--root ./. \
--input rev="${inputs.self.rev or ""}" \
--input shortRev="${inputs.self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (inputs.self.lastModified or "")})" \
--package-path ${inputs.typst-packages}/packages \
--font-path ${fontsConf} \
--ignore-system-fonts \
./src/${documentName}/main.typ \
${documentName}.pdf
runHook postBuild
'';

installPhase = ''
runHook preInstall
install -m640 -D ${documentName}.* -t $out
runHook postInstall
'';
};

mkBuildDocumentScript =
documentName:
pkgs.writeShellApplication {
name = "build-${documentName}";

runtimeInputs = [ typst ];

text = ''
${lib.getExe typst} \
compile \
--root ./. \
--input rev="${inputs.self.rev or ""}" \
--input shortRev="${inputs.self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (inputs.self.lastModified or "")})" \
--package-path ${inputs.typst-packages}/packages \
--font-path ${fontsConf} \
--ignore-system-fonts \
./src/${documentName}/main.typ \
${documentName}.pdf
'';
};

mkWatchDocumentScript =
mkTypstScript =
action:
documentName:
pkgs.writeShellApplication {
name = "watch-${documentName}";
name = "typst-${action}-${documentName}";

runtimeInputs = [ typst ];

text = ''
${lib.getExe typst} \
watch \
${action} \
--root ./. \
--input rev="${inputs.self.rev or ""}" \
--input shortRev="${inputs.self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (inputs.self.lastModified or "")})" \
--package-path ${inputs.typst-packages}/packages \
--font-path ${fontsConf} \
--font-path ${config.packages.typst-fonts} \
--ignore-system-fonts \
./src/${documentName}/main.typ \
${documentName}.pdf
Expand All @@ -126,7 +58,7 @@

documentDrvs = lib.genAttrs (lib.attrNames (
lib.filterAttrs (k: v: (v == "directory")) (builtins.readDir ./src)
)) (d: mkBuildDocumentDrv d);
)) (d: (mkTypstScript "compile" d));

scriptDrvs =
{
Expand All @@ -136,8 +68,8 @@
a: i:
a
// {
"build-${i}" = mkBuildDocumentScript i;
"watch-${i}" = mkWatchDocumentScript i;
"compile-${i}" = (mkTypstScript "compile" i);
"watch-${i}" = (mkTypstScript "watch" i);
}
) { } (lib.attrNames documentDrvs);
in
Expand All @@ -158,11 +90,11 @@
echo "Typst version: ${typst.version}"
echo "Typst bin: ${lib.getExe typst}"
echo "Typst packages directory: ${config.packages.typst-packages}"
echo "Typst fonts directory: ${fontsConf}"
echo "Typst fonts directory: ${config.packages.typst-fonts}"
'';

env = {
TYPST_FONT_PATHS = fontsConf;
TYPST_FONT_PATHS = config.packages.typst-fonts;
};
};
};
Expand Down
19 changes: 19 additions & 0 deletions nix/pkgs/typst-fonts/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
symlinkJoin,
inriafonts,
fg-virgil,
liberation_ttf,
nerd-fonts,
newcomputermodern,
}:

symlinkJoin {
name = "typst-fonts";
paths = [
inriafonts
fg-virgil
liberation_ttf
nerd-fonts.inconsolata
newcomputermodern
];
}

0 comments on commit ffa8996

Please sign in to comment.