Created
January 19, 2024 12:18
-
-
Save drupol/13f7f2f982735c4a78950a0333aa0627 to your computer and use it in GitHub Desktop.
Flake.nix for typst dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
description = "Typst documents"; | |
inputs = { | |
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; | |
systems.url = "github:nix-systems/default"; | |
typst-dev.url = "github:typst/typst"; | |
typst-packages = { | |
flake = false; | |
url = "github:typst/packages"; | |
}; | |
}; | |
outputs = inputs @ { self, flake-parts, typst-dev, ... }: flake-parts.lib.mkFlake { inherit inputs; } { | |
systems = import inputs.systems; | |
perSystem = { config, self', inputs', pkgs, system, lib, ... }: | |
let | |
# Change here to typst-dev | |
localTypst = pkgs.typst; | |
typst-packages = pkgs.callPackage ./nix/typst-packages.nix { src = inputs.typst-packages; }; | |
fontsConf = pkgs.symlinkJoin { | |
name = "typst-fonts"; | |
paths = with pkgs; [ | |
fg-virgil | |
helvetica-neue-lt-std | |
inconsolata-nerdfont | |
]; | |
}; | |
typst = pkgs.writeShellApplication { | |
name = "typst"; | |
runtimeInputs = [ | |
localTypst | |
typst-packages | |
]; | |
text = '' | |
XDG_CACHE_HOME=${typst-packages} OSFONTDIR=${fontsConf}/share/fonts ${lib.getExe localTypst} "$@" | |
''; | |
}; | |
mkBuildDocumentDrv = documentName: pkgs.stdenvNoCC.mkDerivation { | |
name = "build-" + documentName; | |
src = pkgs.lib.cleanSource ./.; | |
buildInputs = [ | |
typst | |
]; | |
buildPhase = '' | |
runHook preBuild | |
${lib.getExe typst} \ | |
compile \ | |
--root ./. \ | |
--font-path ${fontsConf} \ | |
./src/${documentName}/main.typ \ | |
${documentName}.pdf | |
runHook postBuild | |
''; | |
installPhase = '' | |
runHook preInstall | |
mkdir -p $out | |
cp ${documentName}.* $out/ | |
runHook postInstall | |
''; | |
}; | |
mkBuildDocumentScript = documentName: pkgs.writeShellApplication { | |
name = "build-${documentName}"; | |
runtimeInputs = [ | |
typst | |
]; | |
text = '' | |
${lib.getExe typst} \ | |
compile \ | |
--root ./. \ | |
--font-path ${fontsConf} \ | |
./src/${documentName}/main.typ \ | |
${documentName}.pdf | |
''; | |
}; | |
mkWatchDocumentScript = documentName: pkgs.writeShellApplication { | |
name = "watch-${documentName}"; | |
runtimeInputs = [ | |
typst | |
]; | |
text = '' | |
${lib.getExe typst} \ | |
watch \ | |
--root ./. \ | |
--font-path ${fontsConf} \ | |
./src/${documentName}/main.typ \ | |
${documentName}.pdf | |
''; | |
}; | |
documentNames = (lib.attrNames (lib.filterAttrs (k: v: (v == "directory")) (builtins.readDir ./src))); | |
documentDrvs = lib.foldl' | |
(a: i: a // { | |
"${i}" = mkBuildDocumentDrv i; | |
}) | |
{ } | |
documentNames; | |
scriptDrvs = lib.foldl' | |
(a: i: a // { | |
"build-${i}" = mkBuildDocumentScript i; | |
"watch-${i}" = mkWatchDocumentScript i; | |
}) | |
{ } | |
documentNames; | |
in | |
{ | |
_module.args.pkgs = import self.inputs.nixpkgs { | |
inherit system; | |
overlays = [ | |
typst-dev.overlays.default | |
]; | |
config = { | |
allowUnfree = true; | |
}; | |
}; | |
formatter = pkgs.nixpkgs-fmt; | |
packages = documentDrvs; | |
devShells.default = pkgs.mkShellNoCC { | |
name = "typstshell"; | |
buildInputs = (lib.attrValues scriptDrvs) ++ [ | |
typst | |
typst-packages | |
pkgs.gnuplot | |
pkgs.pdfpc | |
pkgs.prettypst | |
pkgs.typst-lsp | |
pkgs.typst-preview | |
pkgs.typst-fmt | |
]; | |
shellHook = '' | |
echo "Typst version: ${localTypst.version}" | |
echo "Typst bin: ${lib.getExe localTypst}" | |
echo "Typst packages directory: ${typst-packages}" | |
echo "Typst fonts directory: ${fontsConf}" | |
''; | |
}; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment