From d9392b0fa2093f68b716a9e71e91f8eebf4839b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Mekina?= Date: Tue, 2 Dec 2025 13:17:29 +0100 Subject: [PATCH] add target for building theses to nix flake --- Makefile | 20 ++++++++++---- flake.nix | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index e63aab3..b776e06 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +TYPST_PACKAGES ?= + BUILD_DIR := target PACKS_ROOT := $(BUILD_DIR)/pack PACKDIR := $(PACKS_ROOT)/tultemplate2 @@ -77,6 +79,14 @@ define replace_with_file_line sed "s/$(1)/$$(sed '$(3)q;d' $(2))/g" endef +define typst_pkgs +$(if $(TYPST_PACKAGES), --package-cache-path $(TYPST_PACKAGES)) +endef + +define typst_compile + typst compile --font-path template/fonts$(call typst_pkgs) --root . +endef + # == LIBS == $(LIBDIR): @@ -94,7 +104,7 @@ template/lib/much_pdf_tools/much_pdf_tools.wasm: | $(LIB_MUCHPDFTOOLS) # == DOCUMENTATION == $(BUILD_DIR)/documentation.pdf: documentation.typ $(TEMPLATE_SRCS) | $(BUILD_DIR) - typst compile --font-path template/fonts $< $@ + $(call typst_compile) $< $@ # == THESES EXAMPLES == @@ -118,10 +128,10 @@ $(BUILD_DIR)/%.typ: $(BUILD_DIR)/header_%.txt $(BUILD_DIR)/content_%.txt | $(BUI cat $^ > $@ $(BUILD_DIR)/%.pdf: $(BUILD_DIR)/%.typ $(TEMPLATE_SRCS) | $(BUILD_DIR) - typst compile --font-path template/fonts --root . $< $@ + $(call typst_compile) $< $@ template/example_appendix.pdf: theses/example_appendix.typ - typst compile --font-path template/fonts --root . $< $@ + $(call typst_compile) $< $@ # == PACKS - clean builds for direct use == @@ -182,10 +192,10 @@ $(BUNDLEDIR)/%.typ: $(BUILD_DIR)/content_%.txt | $(BUNDLEDIR) sed 's/\.\.\/template\//template\//' $< > $@ $(BUNDLEDIR)/title-pages.pdf: theses/title_pages.typ | $(BUNDLEDIR) - typst compile --root . --font-path template/fonts $< $@ + $(call typst_compile) $< $@ $(BUNDLEDIR)/assignment.pdf: theses/assignment.typ | $(BUNDLEDIR) - typst compile --root . --font-path template/fonts $< $@ + $(call typst_compile) $< $@ # == TESTS == diff --git a/flake.nix b/flake.nix index 083d4a0..8a1c9c1 100644 --- a/flake.nix +++ b/flake.nix @@ -21,26 +21,92 @@ chmod +w $LIB/much_pdf_tools fi ''; - build_with_target = target: buildOutput: pkgs.stdenv.mkDerivation { - inherit buildInputs target buildOutput; - name = name + "-" + target; + typstPkgs = [ + { + name = "alchemist"; + version = "0.1.8"; + hash = "18ckw65wq7q8ksayp1g86c9j6d9l1kfg9m100q0gddaw7ksqjqqq"; + } + { + name = "cetz"; + version = "0.4.1"; + hash = "18xinq5agk6zi0r064l6qg09far170n9965a9z2zznz3zsv1h6is"; + } + { + name = "oxifmt"; + version = "1.0.0"; + hash = "0bqc5ahiavjds966a8v5llw9imqqaa43x89ha9dl5nlp51vqmla6"; + } + ]; + pull_typst_package = pkg: pkgs.stdenv.mkDerivation { + name = "typst_package-${pkg.name}-${pkg.version}"; + src = fetchTarball { + url = "https://packages.typst.org/preview/${pkg.name}-${pkg.version}.tar.gz"; + sha256 = pkg.hash; + }; + installPath = "${pkg.name}/${pkg.version}"; + installPhase = '' + mkdir -p $out/$installPath + cp -R . $out/$installPath + ''; + }; + pull_typst_packages = pkgList: pkgs.stdenv.mkDerivation { + name = "typst-packages"; + src = null; + dontUnpack = true; + buildInputs = builtins.map (pkg: pull_typst_package pkg) pkgList; + installPhase = '' + mkdir -p $out/preview + for input in $buildInputs + do + ln -s $input/$(ls $input) $out/preview + done + ''; + }; + build_with_targets = id: targets: buildOutputs: typstPkgs: pkgs.stdenv.mkDerivation { + inherit buildInputs targets buildOutputs; + name = name + "-" + id; src = ./.; buildPhase = '' ${envSetup} - make $target + ${ + if builtins.length typstPkgs > 0 + then + "ln -s ${pull_typst_packages typstPkgs} typst_packages" + else "" + } + for target in $targets + do + make $target ${ + if builtins.length typstPkgs > 0 then "TYPST_PACKAGES=typst_packages" else "" + } + done ''; installPhase = '' mkdir $out - cp -R $buildOutput $out + for buildOutput in $buildOutputs + do + cp -R $buildOutput $out + done ''; }; + build = id: targets: typstPkgs: (let + targetFiles = builtins.map (target: "target/${target}") targets; + in + build_with_targets id [targetFiles] [targetFiles] typstPkgs); in { devShell = with pkgs; mkShell { inherit buildInputs; shellHook = envSetup; }; - packages.bundle = build_with_target "bundle" "target/pack/bundle/."; + packages.bundle = build_with_targets "bundle" ["bundle"] ["target/pack/bundle/."] []; + packages.theses = build "theses" (builtins.map (file: "${file}.pdf") [ + "bp_cs" "bp_en" + "dp_cs" "dp_en" + "prj_cs" "prj_en" + "sp_cs" "sp_en" + ]) typstPkgs; } ); }