buildTypstProjectLocal

Returns a derivation for compiling a Typst project and copying the output to the current directory.

This is essentially a script which wraps buildTypstProject.

Using this derivation requires allow-import-from-derivation to be true (which is the default at the time of writing).

More info: https://nixos.org/manual/nix/stable/language/import-from-derivation

Invoking the script produced by this derivation directly is currently unsupported. Instead, use nix run.

See this issue for more information.

Parameters

All parameters accepted by buildTypstProject are also accepted by buildTypstProjectLocal. They are repeated below for convenience.

src

Source containing all local files needed in your Typst project.

emojiFont optional

Specify which emoji font to use. If emojiFont is null, no emoji font will be included.

May be any of the following:

Note about difference between "twemoji" and "twemoji-cbdt"

The default Twemoji font displays color emojis using the SVG font format, which may not be supported by some systems. If emojis aren't displaying properly, using "twemoji-cbdt" may fix it.

fontPaths optional

List of sources specifying paths to font files that will be made available to your Typst project. With this, you can compile Typst projects even when the fonts it uses are not available on your system.

Used for setting TYPST_FONT_PATHS (see text#font).

Example

{
  outputs = { nixpkgs, typix }: let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    inherit (pkgs) lib;

    build-script = typix.lib.${system}.buildTypstProjectLocal {
      fontPaths = [
        "${pkgs.roboto}/share/fonts/truetype"
      ];
    };
  in {
    apps.${system}.default = {
      type = "app";
      program = lib.getExe build-script;
    };
  };
}

installPhaseCommand optional

Command (or commands) to run during installPhase.

scriptName optional

Name of the script that will be added to the Nix store. You can use this name after entering a development shell to invoke the script.

Default is typst-build.

typstCompileCommand optional

Base Typst command to run to compile the project. Other arguments will be appended based on the parameters you supply.

Default is typst compile.

typstOpts optional

Attrset specifying command-line options to pass to the typst command.

These are in addition to any options you manually pass in typstCompileCommand.

Default:

{
  format = "pdf";
}

Example

{
  format = "png";
  ppi = 300;
}

...will result in a command like:

typst compile --format png --ppi 300 <source> <output>

typstOutput optional

Destination path for Typst output.

If omitted, it will be inferred from typstSource — for example, page.typ will become page.pdf for PDF output.

typstSource optional

Typst input file to compile.

Default is main.typ.

unstable_typstPackages optional

This is an unstable feature. Its API does not follow Semantic Versioning, and derivations relying on this feature may break at any time, even if you don't update Typst or Typix.

When this feature reaches stability, it will be renamed to typstPackages. (The old name will be kept as an undocumented alias, to avoid unnecessary breakage.)

List of Typst packages to use in your Typst project, fetched from the official Typst packages CDN at https://packages.typst.org.

Each element of the list is an attribute set with the following keys:

  • name: the package's identifier in its namespace
  • version: the package's version as a full major-minor-patch triple
  • namespace (optional): the package's namespace (defaults to preview)
  • hash: hash of the downloaded package tarball

hash must be manually updated to match the hash of the package tarball downloaded from the registry, using the "fake hash method". See Updating source hashes for how to do this.

Sometimes, you might encounter a Typst compilation error that looks like this:

> help: error occurred while importing this module
>   ┌─ @preview/cetz:0.3.4/src/canvas.typ:3:8
>   │
> 3 │ #import "util.typ"
>   │         ^^^^^^^^^^
> help: error occurred while importing this module
>   ┌─ @preview/cetz:0.3.4/src/lib.typ:3:8
>   │
> 3 │ #import "canvas.typ": canvas
>   │         ^^^^^^^^^^^^
> help: error occurred while importing this module
>   ┌─ main.typ:1:8
>   │
> 1 │ #import "@preview/cetz:0.3.4"
>   │         ^^^^^^^^^^^^^^^^^^^^^
>
For full logs, run 'nix log /nix/store/6jhjxbl7glmy4adpr5wzfgn9jvsyyipf-typst.drv'.

In this example, Nix is hiding too much output for us to diagnose the issue. Run the command again with the -L flag (in our case, nix run .#build -L):

> downloading @preview/oxifmt:0.2.1
> error: failed to download package (Network Error: OpenSSL error)
>   ┌─ @preview/cetz:0.3.4/src/deps.typ:1:8
>   │
> 1 │ #import "@preview/oxifmt:0.2.1"
>   │         ^^^^^^^^^^^^^^^^^^^^^^^
> help: error occurred while importing this module
>   ┌─ @preview/cetz:0.3.4/src/util.typ:1:8
>   │
> 1 │ #import "deps.typ"
>   │         ^^^^^^^^^^
> help: error occurred while importing this module
>   ┌─ @preview/cetz:0.3.4/src/canvas.typ:3:8
>   │
> 3 │ #import "util.typ"
>   │         ^^^^^^^^^^
> help: error occurred while importing this module
>   ┌─ @preview/cetz:0.3.4/src/lib.typ:3:8
>   │
> 3 │ #import "canvas.typ": canvas
>   │         ^^^^^^^^^^^^
> help: error occurred while importing this module
>   ┌─ main.typ:1:8
>   │
> 1 │ #import "@preview/cetz:0.3.4"
>   │         ^^^^^^^^^^^^^^^^^^^^^

We can see that cetz is trying to import oxifmt 0.2.1, but Typst can't download it because Nix derivations are (by design) run in an environment which does not support networking. To fix this, add oxifmt 0.2.1 to unstable_typstPackages alongside your direct dependencies. (Make sure to match the exact version shown in your error message!!)

There is currently no official support for unpublished Typst packages. However, there is a workaround.

Example

{
  outputs = { nixpkgs, typix }: let
    inherit (nixpkgs) lib;
    system = "x86_64-linux";

    build-script = typix.lib.${system}.buildTypstProjectLocal {
      unstable_typstPackages = [
        {
          name = "cetz";
          version = "0.3.4";
          hash = "sha256-5w3UYRUSdi4hCvAjrp9HslzrUw7BhgDdeCiDRHGvqd4=";
        }
        # Transitive dependencies must be manually specified
        # `oxifmt` is required by `cetz`
        {
          name = "oxifmt";
          version = "0.2.1";
          hash = "sha256-8PNPa9TGFybMZ1uuJwb5ET0WGIInmIgg8h24BmdfxlU=";
        }
      ];
    };
  in {
    apps.${system}.default = {
      type = "app";
      program = lib.getExe build-script;
    };
  };
}

Then you can import and use the package in Typst:

#import "@preview/cetz:0.3.4"

#cetz.canvas({
  import cetz.draw: *

  circle((0, 0))
  line((0, 0), (2, 1))
})

virtualPaths optional

List of sources that will be made virtually available to your Typst project. Useful for projects which rely on remote resources, such as images or data.

Each element of the list is an attribute set with the following keys:

  • src: path to source file or directory
  • dest (optional): path where file(s) will be made available (defaults to .)
    • If src is a directory, dest will be a directory containing the files in that directory.
      • Specifying the same dest for multiple src directories will merge them.
    • If src is a file, dest will be a copy of that file.

Instead of an attrset, you may use a path which will be interpreted the same as if you had specified an attrset with just src.

Example

You can specify dependencies in your flake input, and then use them in your project with something like:

{
  inputs = {
    font-awesome = {
      url = "github:FortAwesome/Font-Awesome";
      flake = false;
    };
  };

  outputs = { nixpkgs, typix, font-awesome }: let
    system = "x86_64-linux";
    inherit (nixpkgs) lib;

    build-script = typix.lib.${system}.buildTypstProjectLocal {
      virtualPaths = [
        {
          dest = "icons";
          src = "${font-awesome}/svgs/regular";
        }
      ];
    };
  in {
    apps.${system}.default = {
      type = "app";
      program = lib.getExe build-script;
    };
  };
}

Then, reference the files in Typst:

#image("icons/heart.svg")

Source