A dead-simple function to build your typst packages, including those that require typst packages.
This repo contains a mkTypstDerivation function. It's a simple mkDerivation wrapper, with a buildPhase that calls typst compile:
mkTypstDerivation {
name = "paper-im-procrastinating-on";
# Directory containing the typst project
src = ./.;
# Main file name
mainFile = "main.typ";
}Besides the usual mkDerivation arguments, it also takes (all of them optional):
mainFile: the main typst file name (relative tosrc). Defaults tomain.typ.outFormat: the chosen output format. At the time of writing, typst supportspdf,svg, andpng. Default topdf.outputFile: file name to output. Defaults tomainFilewith.typreplaced by.${outFormat}(i.e.main.pdf).typstPackages: an attribute set of package repositories. Defaults to{ preview = "${typst-packages}/packages/preview"; }extraFonts: extra fonts typst should have access to.extraCompileFlags: extra arguments to pass to typst compile.typst: typst package used to compile, in case you want to use a different one.
The function's available at default.nix and also exposed via flake lib and overlays.default outputs.
Minimal example:
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
typst-nix.url = "github:misterio77/typst-nix";
typst-nix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { nixpkgs, typst-nix, ... }: {
packages.x86_64-linux = {
default = typst-nix.lib.x86_64-linux.mkTypstDerivation {
name = "ending-world-hunger-with-nix";
src = ./.;
};
});
};
}Do check example/flake.nix for a slightly better example. Also available as a nix flake template:
nix flake init -t github:misterio77/typst-nixlet
mkTypstDerivation = import (fetchTarball "https://github.com/misterio77/typst-nix") {};
in
mkTypstDerivation {
name = "destroying-capitalism-with-nix";
src = ./.;
}Do note that by, default, you'll use this repo's locked typst-packages version.
If that bothers you (maybe I forgor to update the lock idk), either:
- Use flake inputs follows (see example/flake.nix); or
- Set
mkTypstDerivation'stypstPackages.previewargument (e.g. to afetchTarballcall), like so:
mkTypstDerivation {
name = "hi-there";
src = ./.;
typstPackages = {
preview = "${fetchTarball "https://github.com/typst/packages"}/packages/preview";
};
}