-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
181 lines (153 loc) · 5.41 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
typst-dev.url = "github:typst/typst";
typst-packages = {
flake = false;
url = "github:typst/packages";
};
pkgs-by-name-for-flake-parts.url = "github:drupol/pkgs-by-name-for-flake-parts";
};
outputs =
inputs@{ ... }:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;
imports = [
./nix/imports/pkgs.nix
inputs.pkgs-by-name-for-flake-parts.flakeModule
];
perSystem =
{
pkgs,
lib,
config,
...
}:
let
# Change here to typst-dev if needed
typst = pkgs.nixpkgs-unstable.typst;
fontsConf = pkgs.symlinkJoin {
name = "typst-fonts";
paths = with pkgs; [
inriafonts
fg-virgil
liberation_ttf
inconsolata-nerdfont
newcomputermodern
];
};
typst-wrapper-factory =
typstDrv: typst-packages: typstFontPaths:
pkgs.writeShellApplication {
name = "typst-wrapper";
runtimeInputs = [
typstDrv
typst-packages
];
text = ''
TYPST_FONT_PATHS=${typstFontPaths} XDG_CACHE_HOME=${typst-packages} ${lib.getExe typstDrv} "$@"
'';
};
typst-wrapper = typst-wrapper-factory typst config.packages.typst-packages fontsConf;
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="${inputs.self.rev or ""}" \
--input shortRev="${inputs.self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (inputs.self.lastModified or "")})" \
--font-path ${fontsConf} \
./src/${documentName}/main.typ \
${documentName}.pdf
runHook postBuild
'';
installPhase = ''
runHook preInstall
install -m640 -D ${documentName}.* -t $out
runHook postInstall
'';
};
mkBuildDocumentScript =
documentName:
pkgs.writeShellApplication {
name = "build-${documentName}";
runtimeInputs = [ typst-wrapper ];
text = ''
${lib.getExe typst-wrapper} \
compile \
--root ./. \
--input rev="${inputs.self.rev or ""}" \
--input shortRev="${inputs.self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (inputs.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="${inputs.self.rev or ""}" \
--input shortRev="${inputs.self.shortRev or ""}" \
--input builddate="$(date -u -d @${toString (inputs.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 =
{
"sign-pdf" = config.packages.sign-pdf;
}
// lib.foldl' (
a: i:
a
// {
"build-${i}" = mkBuildDocumentScript i;
"watch-${i}" = mkWatchDocumentScript i;
}
) { } (lib.attrNames documentDrvs);
in
{
pkgsDirectory = ./nix/pkgs;
packages = documentDrvs;
devShells.default = pkgs.mkShellNoCC {
packages = (lib.attrValues scriptDrvs) ++ [
typst
typst-wrapper
pkgs.gnuplot
pkgs.pympress
];
shellHook = ''
echo "Typst version: ${typst.version}"
echo "Typst bin: ${lib.getExe typst}"
echo "Typst wrapper bin: ${lib.getExe typst-wrapper}"
echo "Typst packages directory: ${config.packages.typst-packages}"
echo "Typst fonts directory: ${fontsConf}"
'';
env = {
TYPST_FONT_PATHS = fontsConf;
};
};
};
};
}