|
| 1 | +{ |
| 2 | + description = "ustc-proposal"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; |
| 6 | + |
| 7 | + typix = { |
| 8 | + url = "github:loqusion/typix"; |
| 9 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 10 | + }; |
| 11 | + |
| 12 | + flake-utils.url = "github:numtide/flake-utils"; |
| 13 | + |
| 14 | + typst-packages = { |
| 15 | + url = "github:typst/packages"; |
| 16 | + flake = false; |
| 17 | + }; |
| 18 | + |
| 19 | + fonts-popular-fonts = { |
| 20 | + url = "github:chengda/popular-fonts"; |
| 21 | + flake = false; |
| 22 | + }; |
| 23 | + |
| 24 | + fonts-scp_zh = { |
| 25 | + url = "github:StellarCN/scp_zh"; |
| 26 | + flake = false; |
| 27 | + }; |
| 28 | + |
| 29 | + fonts-pytorch = { |
| 30 | + url = "github:siaimes/pytorch"; |
| 31 | + flake = false; |
| 32 | + }; |
| 33 | + }; |
| 34 | + |
| 35 | + outputs = |
| 36 | + inputs@{ |
| 37 | + nixpkgs, |
| 38 | + typix, |
| 39 | + flake-utils, |
| 40 | + ... |
| 41 | + }: |
| 42 | + flake-utils.lib.eachDefaultSystem ( |
| 43 | + system: |
| 44 | + let |
| 45 | + pkgs = nixpkgs.legacyPackages.${system}; |
| 46 | + typixLib = typix.lib.${system}; |
| 47 | + |
| 48 | + src = typixLib.cleanTypstSource ./.; |
| 49 | + commonArgs = { |
| 50 | + typstSource = "main.typ"; |
| 51 | + |
| 52 | + fontPaths = [ |
| 53 | + "${pkgs.font-awesome}/share/fonts/opentype" |
| 54 | + "${inputs.fonts-popular-fonts}" |
| 55 | + "${inputs.fonts-scp_zh}/fonts" |
| 56 | + "${inputs.fonts-pytorch}/fonts" |
| 57 | + ]; |
| 58 | + |
| 59 | + virtualPaths = [ |
| 60 | + # Add paths that must be locally accessible to typst here |
| 61 | + # { |
| 62 | + # dest = "icons"; |
| 63 | + # src = "${inputs.font-awesome}/svgs/regular"; |
| 64 | + # } |
| 65 | + ]; |
| 66 | + }; |
| 67 | + typstPackagesSrc = pkgs.symlinkJoin { |
| 68 | + name = "typst-packages-src"; |
| 69 | + paths = [ |
| 70 | + "${inputs.typst-packages}/packages" |
| 71 | + # More Typst packages can be added here |
| 72 | + ]; |
| 73 | + }; |
| 74 | + typstPackagesCache = pkgs.stdenvNoCC.mkDerivation { |
| 75 | + name = "typst-packages-cache"; |
| 76 | + src = typstPackagesSrc; |
| 77 | + dontBuild = true; |
| 78 | + installPhase = '' |
| 79 | + mkdir -p "$out/typst/packages" |
| 80 | + cp -LR --reflink=auto --no-preserve=mode -t "$out/typst/packages" "$src"/* |
| 81 | + ''; |
| 82 | + }; |
| 83 | + |
| 84 | + # Compile a Typst project, *without* copying the result |
| 85 | + # to the current directory |
| 86 | + build-drv = typixLib.buildTypstProject ( |
| 87 | + commonArgs |
| 88 | + // { |
| 89 | + inherit src; |
| 90 | + XDG_CACHE_HOME = typstPackagesCache; |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + # Compile a Typst project, and then copy the result |
| 95 | + # to the current directory |
| 96 | + build-script = typixLib.buildTypstProjectLocal ( |
| 97 | + commonArgs |
| 98 | + // { |
| 99 | + inherit src; |
| 100 | + XDG_CACHE_HOME = typstPackagesCache; |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + # Watch a project and recompile on changes |
| 105 | + watch-script = typixLib.watchTypstProject commonArgs; |
| 106 | + in |
| 107 | + { |
| 108 | + checks = { |
| 109 | + inherit build-drv build-script watch-script; |
| 110 | + }; |
| 111 | + |
| 112 | + packages.default = build-drv; |
| 113 | + |
| 114 | + apps = rec { |
| 115 | + default = watch; |
| 116 | + build = flake-utils.lib.mkApp { |
| 117 | + drv = build-script; |
| 118 | + }; |
| 119 | + watch = flake-utils.lib.mkApp { |
| 120 | + drv = watch-script; |
| 121 | + }; |
| 122 | + }; |
| 123 | + |
| 124 | + devShells.default = typixLib.devShell { |
| 125 | + inherit (commonArgs) fontPaths virtualPaths; |
| 126 | + packages = [ |
| 127 | + # WARNING: Don't run `typst-build` directly, instead use `nix run .#build` |
| 128 | + # See https://github.com/loqusion/typix/issues/2 |
| 129 | + # build-script |
| 130 | + watch-script |
| 131 | + # More packages can be added here, like typstfmt |
| 132 | + # pkgs.typstfmt |
| 133 | + ]; |
| 134 | + }; |
| 135 | + } |
| 136 | + ); |
| 137 | +} |
0 commit comments