typst-nix/lib/typst-package.nix
Dzuchun 7ede7576d4 fix: typst package derivation -> package
package does not have to be defined via derivation, but as "something
that can be converted into string", which is a much broader concept.
thus, `lib.isDerivation` assertion should not be made.
2026-10-24 16:53:58 +01:00

154 lines
4.5 KiB
Nix

{
lib,
hasSystem,
pkgs,
flakeInputs,
...
}:
let
toToml = flakeInputs.self.lib.toToml;
# generates the typst manifest TEXT for you
mkTypstManifest =
{
# name of your package
name,
# version of your package. note that `Typst` package versions _must_ be of three
# components, u32 each
version ? "0.0.1",
# entrypoint, i.e. the module that get imported/included when you target the
# package
entrypoint ? "lib.typ",
authors ? [ ],
license ? null,
description ? null,
homepage ? null,
repository ? null,
keywords ? [ ],
categories ? [ ],
disciplines ? [ ],
# extra manifest data, uncovered by the args. overrides the other args
extraManifest ? { },
}:
let
explicitManifest = {
package = {
inherit
name
version
entrypoint
;
}
// lib.optionalAttrs (builtins.length authors > 0) { inherit authors; }
// lib.optionalAttrs (license != null) { inherit license; }
// lib.optionalAttrs (description != null) { inherit description; }
// lib.optionalAttrs (homepage != null) { inherit homepage; }
// lib.optionalAttrs (repository != null) { inherit repository; }
// lib.optionalAttrs (builtins.length keywords > 0) { inherit keywords; }
// lib.optionalAttrs (builtins.length categories > 0) { inherit categories; }
// lib.optionalAttrs (builtins.length disciplines > 0) { inherit disciplines; };
};
fullManifest = lib.recursiveUpdate explicitManifest extraManifest;
in
toToml fullManifest;
# generates the typst package for you
# output type: { name: String, version: String, package: toString }
mkTypstPackage =
{
# whether the builder should ignore an existing manifest, and use the arguments of this
# function to generate it instead
forceManifest ? false,
# root of your Typst package data
root,
# name of your package
name ? builtins.baseNameOf root,
# version of your package. note that `Typst` package versions _must_ be of three
# components, u32 each
version ? "0.1.0",
# entrypoint, i.e. the module that get imported/included when you target the
# package
entrypoint ? "lib.typ",
authors ? [ ],
license ? null,
description ? null,
homepage ? null,
repository ? null,
keywords ? [ ],
categories ? [ ],
disciplines ? [ ],
# extra manifest data, uncovered by the args. overrides the other args
extraManifest ? { },
}:
let
rootManifest = "${root}/typst.toml";
inherit
(
if builtins.pathExists rootManifest && !forceManifest then
{
manifestFile = rootManifest;
manifestText = builtins.readFile rootManifest;
}
else
let
text = mkTypstManifest {
inherit
name
version
entrypoint
authors
license
description
homepage
repository
keywords
categories
disciplines
extraManifest
;
};
in
{
manifestText = text;
manifestFile = builtins.toFile "typst.toml" text;
}
)
manifestFile
manifestText
;
manifestValue = builtins.fromTOML manifestText;
manifestName = manifestValue.package.name;
manifestVersion = manifestValue.package.version;
package = pkgs.stdenv.mkDerivation {
name = manifestName;
src = root;
buildInputs = with pkgs; [
coreutils
];
buildPhase = ''
#!/bin/bash
set -euo pipefail
TMP="$(mktemp -d)"
cd "$TMP"
export HOME="$TMP"
cp --no-preserve=all --recursive $src $out
cp --no-preserve=all ${manifestFile} $out/typst.toml
'';
};
in
assert lib.assertMsg (lib.pathIsDirectory root)
"`root` path must be a directory containing your typst package";
{
name = manifestName;
version = manifestVersion;
inherit package;
};
in
if hasSystem then
{
config.lib = { inherit mkTypstPackage; };
}
else
{
config.lib = { inherit mkTypstManifest; };
}