Skip to content

Add more environment control parameters to CLI - #4227

Merged
laurmaedje merged 21 commits into
typst:mainfrom
LuizAugustoPapa:development
Jun 6, 2024
Merged

laurmaedje merged 21 commits into
typst:mainfrom
LuizAugustoPapa:development

Conversation

@LuizAugustoPapa

@LuizAugustoPapa LuizAugustoPapa commented May 22, 2024 •

Copy link
Copy Markdown
Contributor

Closes #3892

PR co-authored by @PgBiel , @tulio240 , @PepinhoJp

This PR adds a few parameters to the Typst CLI which lets you control the environment accessed by Typst:

  1. --package-cache-path or (env) TYPST_PACKAGE_CACHE_PATH: Set this to change where downloaded Typst packages are installed to. For example, #import "@preview/cetz:0.2.2" would download cetz:0.2.2 to $TYPST_PACKAGE_CACHE_PATH/preview/cetz/0.2.2 (instead of the usual $XDG_CACHE_HOME/typst/packages/preview/cetz/0.2.2, on Linux as an example)

  2. --package-path or (env) TYPST_PACKAGE_PATH: Set this to change the root folder where local packages are searched. For example, #import "@local/tablex:0.0.8" would search for tablex:0.0.8 at $TYPST_PACKAGE_PATH/local/tablex/0.0.8 (instead of the usual $XDG_DATA_HOME/typst/packages/local/tablex/0.0.8, on Linux as an example)

  3. --ignore-system-fonts (no env): Specify this to disable searching for system fonts, only use embedded fonts or fonts in specified in --font-path

  4. (for typst update only) --backup-path or (env) TYPST_UPDATE_BACKUP_PATH: Set this to change the file path where the Typst binary is backed up to before an update

We suggest giving these features a quick test to ensure they work in different environments.

A quick note: We used an Arc in the FileSlot field with package storage path information to avoid having to use a reference with a lifetime generic parameter in the FileSlot struct (since this information is shared between all FileSlot, so didn't want to clone). Let us know if this should be changed

Credit to @bluebear94 for the idea of grouping up arguments related to font settings together.

@dblsaiko

Copy link
Copy Markdown

Tested TYPST_LOCAL_PACKAGES_PATH (rebased on top of 0.11.1) for use in a nix shell, works great!

@bluebear94

Copy link
Copy Markdown
Contributor

My original PR also changed the documentation for TextElem::font to note that system fonts are searched only if --no-system-fonts (your --ignore-system-fonts) is not passed. We might also want to make that change here.

Comment thread crates/typst-cli/src/args.rs Resolved
Comment thread crates/typst-cli/src/package.rs Resolved
Comment thread crates/typst-cli/src/update.rs Resolved
Comment thread crates/typst-cli/src/world.rs Resolved
Comment thread crates/typst/src/text/mod.rs Resolved
Comment thread crates/typst-cli/src/update.rs Resolved
@pachi

pachi commented May 30, 2024

Copy link
Copy Markdown

I wonder if --packages-path adding a new search path for packages and having --ignore-default-packages-path as a way to avoid the default path would be interesting.

The usecase would be adding organization wide packages or templates using shared folders, while keeping the user's default package path for other packages. The ignore-default-packages-path, on the other hand, would allow some form of isolation and ease testing.

@PgBiel

PgBiel commented May 30, 2024

Copy link
Copy Markdown
Contributor

I wonder if --packages-path adding a new search path for packages and having --ignore-default-packages-path as a way to avoid the default path would be interesting.

The usecase would be adding organization wide packages or templates using shared folders, while keeping the user's default package path for other packages. The ignore-default-packages-path, on the other hand, would allow some form of isolation and ease testing.

Currently, the default path is no longer searched. We considered adding a way to have multiple search paths, which seems useful in principle, but we think this is probably out of scope for this particular PR. Perhaps there could be some per-namespace option, e.g. use @project for the project's local packages and @local for system-wide local packages.

@pachi

pachi commented Jun 4, 2024

Copy link
Copy Markdown

Currently, the default path is no longer searched. We considered adding a way to have multiple search paths, which seems useful in principle, but we think this is probably out of scope for this particular PR. Perhaps there could be some per-namespace option, e.g. use @project for the project's local packages and @local for system-wide local packages.

I'm not sure if distinguishing between those two namespaces is worth it. That would mean that the template import changes not due to network use but due to local path. IMHO, @Local meaning no network access vs @Preview is interesting, but more finegrained control over where the local packages would be fetched should done with the env vars.

@PgBiel

PgBiel commented Jun 4, 2024 •

Copy link
Copy Markdown
Contributor

Sorry, I didn't mean to imply that those namespace choices I mentioned would be hardcoded or available by default - I meant that you'd be able to pick whichever namespace names you wanted for your own directories of local packages, as is possible today by creating folders other than "local" under (DATA)/typst/packages. With an additional parameter, you could define a namespace named @supercoolproject pointing to a custom folder in your computer for example.

@pachi

pachi commented Jun 4, 2024

Copy link
Copy Markdown

Sorry, I didn't mean to imply that those namespace choices I mentioned would be hardcoded or available by default - I meant that you'd be able to pick whichever namespace names you wanted for your own directories of local packages, as is possible today by creating folders other than "local" under (DATA)/typst/packages. With an additional parameter, you could define a namespace named @supercoolproject pointing to a custom folder in your computer for example.

Oh, I misunderstood! Now I undestand you, and indeed, having access to the root gives the user the option to choose any namespace.

@laurmaedje

Copy link
Copy Markdown
Member

Though with that configuration we're nearing typical compiler flags for linking external packages, rather than typical package manager style configuration for external packages. I wonder whether such path dependencies aren't better put into a typst.toml.

@pachi

pachi commented Jun 4, 2024

Copy link
Copy Markdown

Though with that configuration we're nearing typical compiler flags for linking external packages, rather than typical package manager style configuration for external packages. I wonder whether such path dependencies aren't better put into a typst.toml.

Being able to configure in a typst.toml the paths for fonts, (extra)packages, etc could be great for some projects.

Being able to use env vars IMHO is still convenient when using typst from another app, which would probably send the content to the compiler using stdin, so no document specific files would be used on disk, only resources (fonts, packages, images, etc).

@laurmaedje
laurmaedje added this pull request to the merge queue Jun 6, 2024
@laurmaedje

Copy link
Copy Markdown
Member

Thank you all!

Merged via the queue into typst:main with commit cc3e9c8 Jun 6, 2024
@drupol

drupol commented Jun 6, 2024

Copy link
Copy Markdown
Contributor

Excellent !!! Looking forward to using this in Nix :)

CC @loqusion (context: https://discourse.nixos.org/t/typix-a-nix-library-for-compiling-typst-projects/40257)

@matthiasbeyer

Copy link
Copy Markdown

I came across while searching for a solution to the problem that typst seems to ignore the XDG_DATA_HOME and always tries ~/.local/share first.

This would solve that issue, from what I read. Cannot wait to have it available in a release!

@PgBiel

PgBiel commented Oct 18, 2024

Copy link
Copy Markdown
Contributor

typst seems to ignore the XDG_DATA_HOME and always tries ~/.local/share first.

That would be a bug; if you can still reproduce it in Typst 0.12-rc2 (the latest release candidate), please open an issue.

@matthiasbeyer

matthiasbeyer commented Oct 19, 2024 •

Copy link
Copy Markdown

Just to be sure: With XDG_CACHE_HOME, everything seems to work. Above I tried XDG_DATA_HOME, which didn't work.
Would that still be a bug?


Edit: It seems 0.12.0 is out. 🎉 I will update and see whether the behaviour is still as I described.

Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Co-authored-by: PgBiel <[email protected]>
Co-authored-by: Tulio Martins <[email protected]>
Co-authored-by: PepinhoJp <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More control over typst compilation environment

10 participants