Skip to content

Instantly share code, notes, and snippets.

@drupol

drupol/flake.nix Secret

Created May 11, 2024 21:16
Show Gist options
  • Save drupol/3a05991527cdf40da6b46629fd9f5089 to your computer and use it in GitHub Desktop.
Save drupol/3a05991527cdf40da6b46629fd9f5089 to your computer and use it in GitHub Desktop.
Flake for Typst
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-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;
imports = [
./pkgs.nix
./overlay.nix
];
perSystem =
{
config,
self',
inputs',
pkgs,
system,
lib,
...
}:
let
# Change here to typst-dev if needed
typst = pkgs.typst;
typst-wrapper = pkgs.typst-wrapper pkgs.typst-packages typst;
typstyle = pkgs.typstyle;
fontsConf = pkgs.symlinkJoin {
name = "typst-fonts";
paths = with pkgs; [
inriafonts
fg-virgil
helvetica-neue-lt-std
inconsolata-nerdfont
];
};
mkBuildDocumentDrv =
documentName:
pkgs.stdenvNoCC.mkDerivation {
name = "build-" + documentName;
src = pkgs.lib.cleanSource ./.;
buildInputs = [ typst-wrapper ];
buildPhase = ''
runHook preBuild
${lib.getExe typst-wrapper} \
compile \
--root ./. \
--input rev="${self.rev or ""}" \
--input shortRev="${self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (self.lastModified or "")})" \
--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-wrapper ];
text = ''
${lib.getExe typst-wrapper} \
compile \
--root ./. \
--input rev="${self.rev or ""}" \
--input shortRev="${self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (self.lastModified or "")})" \
--font-path ${fontsConf} \
./src/${documentName}/main.typ \
${documentName}.pdf
'';
};
mkWatchDocumentScript =
documentName:
pkgs.writeShellApplication {
name = "watch-${documentName}";
runtimeInputs = [ typst-wrapper ];
text = ''
${lib.getExe typst-wrapper} \
watch \
--root ./. \
--input rev="${self.rev or ""}" \
--input shortRev="${self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (self.lastModified or "")})" \
--font-path ${fontsConf} \
./src/${documentName}/main.typ \
${documentName}.pdf
'';
};
documentDrvs = lib.genAttrs (lib.attrNames (lib.filterAttrs (k: v: (v == "directory")) (builtins.readDir ./src))) (d: mkBuildDocumentDrv d);
scriptDrvs = lib.foldl' (
a: i:
a
// {
"build-${i}" = mkBuildDocumentScript i;
"watch-${i}" = mkWatchDocumentScript i;
}
) { } (lib.attrNames documentDrvs);
in
{
packages = documentDrvs;
devShells.default = pkgs.mkShellNoCC {
name = "typstshell";
buildInputs = (lib.attrValues scriptDrvs) ++ [
typst
typst-wrapper
typstyle
pkgs.gnuplot
pkgs.typst-preview
];
shellHook = ''
echo "Typst version: ${typst.version}"
echo "Typst bin: ${lib.getExe typst}"
echo "Typst wrapper bin: ${lib.getExe typst-wrapper}"
echo "Tinymist bin: ${lib.getExe pkgs.nixpkgs-unstable.tinymist}"
echo "Typst packages directory: ${pkgs.typst-packages}"
echo "Typst fonts directory: ${fontsConf}"
'';
};
};
};
}
{ inputs
, self
, lib
, ...
}:
{
flake = {
overlays.default = final: prev: {
typst-packages = prev.callPackage ../pkgs/typst-packages.nix { src = inputs.typst-packages; };
typst-wrapper = typst-packages: typstDrv: prev.writeShellApplication {
name = "typst-wrapper";
runtimeInputs = [
typstDrv
typst-packages
];
text = ''
XDG_CACHE_HOME=${typst-packages} ${lib.getExe typstDrv} "$@"
'';
};
};
};
}
{
inputs,
self,
...
}:
{
perSystem = { config, self', inputs', pkgs, system, lib, ... }: {
_module.args.pkgs = import self.inputs.nixpkgs {
inherit system;
overlays = [
inputs.typst-dev.overlays.default
inputs.self.overlays.default
(final: prev: {
nixpkgs-unstable = import inputs.nixpkgs-unstable {
inherit system;
};
})
];
config = {
allowUnfree = true;
};
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment