-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflake.nix
More file actions
196 lines (164 loc) · 5.37 KB
/
flake.nix
File metadata and controls
196 lines (164 loc) · 5.37 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{
description = "Typst - a modern typesetting system";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
flake-compat = {
url = "github:NixOS/flake-compat";
flake = false;
};
typst = {
url = "github:typst/typst/0.14";
flake = false;
};
crane.url = "github:ipetkov/crane";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-manifest = {
url = "https://static.rust-lang.org/dist/channel-rust-1.91.0.toml";
flake = false;
};
};
outputs =
inputs@{
flake-parts,
crane,
nixpkgs,
fenix,
rust-manifest,
typst,
self,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = inputs.nixpkgs.lib.systems.flakeExposed;
imports = [
inputs.flake-parts.flakeModules.easyOverlay
];
perSystem =
{
self',
pkgs,
lib,
system,
...
}:
let
root = inputs.typst;
cargoToml = lib.importTOML "${root}/Cargo.toml";
pname = "typst";
version = cargoToml.workspace.package.version;
rust-toolchain = fenix.packages.${system}.fromManifestFile rust-manifest;
# Upstream defaults to lld on x86_64-unknown-linux-gnu, we need to use the system linker
rustflags = lib.concatStringsSep " " (
lib.optionals (pkgs.stdenv.hostPlatform.rust.rustcTargetSpec == "x86_64-unknown-linux-gnu") [
"-Clinker-features=-lld"
"-Clink-self-contained=-linker"
]
);
# Crane-based Nix flake configuration.
# Based on https://github.com/ipetkov/crane/blob/master/examples/trunk-workspace/flake.nix
craneLib = (crane.mkLib pkgs).overrideToolchain rust-toolchain.defaultToolchain;
# Typst files to include in the derivation.
# Here we include Rust files, docs and tests.
sourcePaths = [
"Cargo.toml"
"Cargo.lock"
"rustfmt.toml"
"crates"
"docs"
"tests"
];
src = lib.sources.cleanSourceWith {
src = root;
filter = path: type:
builtins.any (accepted: lib.strings.hasPrefix "${root}/${accepted}" path) sourcePaths;
};
# Typst derivation's args, used within crane's derivation generation
# functions.
commonCraneArgs = {
inherit src pname version;
buildInputs = [
pkgs.openssl
];
nativeBuildInputs = [
pkgs.pkg-config
pkgs.openssl.dev
];
env = {
RUSTFLAGS = rustflags;
};
};
# Derivation with just the dependencies, so we don't have to keep
# re-building them.
cargoArtifacts = craneLib.buildDepsOnly commonCraneArgs;
typst = craneLib.buildPackage (
commonCraneArgs
// {
inherit cargoArtifacts;
nativeBuildInputs = commonCraneArgs.nativeBuildInputs ++ [
pkgs.installShellFiles
];
postInstall = ''
installManPage crates/typst-cli/artifacts/*.1
installShellCompletion \
crates/typst-cli/artifacts/typst.{bash,fish} \
--zsh crates/typst-cli/artifacts/_typst
'';
GEN_ARTIFACTS = "artifacts";
TYPST_VERSION = cargoToml.workspace.package.version;
TYPST_COMMIT_SHA = self.shortRev or "dirty";
meta.mainProgram = "typst";
}
);
in
{
formatter = pkgs.nixfmt-tree;
packages = {
default = typst;
typst-dev = self'.packages.default;
};
overlayAttrs = builtins.removeAttrs self'.packages [ "default" ];
apps.default = {
type = "app";
program = lib.getExe typst;
};
checks = {
typst-fmt = craneLib.cargoFmt commonCraneArgs;
typst-clippy = craneLib.cargoClippy (
commonCraneArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--workspace -- --deny warnings";
}
);
typst-test = craneLib.cargoTest (
commonCraneArgs
// {
inherit cargoArtifacts;
cargoTestExtraArgs = "--workspace";
}
);
};
devShells.default = craneLib.devShell {
checks = self'.checks;
inputsFrom = [ typst ];
buildInputs = [
rust-toolchain.rust-analyzer
rust-toolchain.rust-src
];
RUST_SRC_PATH = "${rust-toolchain.rust-src}/lib/rustlib/src/rust/library";
RUSTFLAGS = rustflags;
packages = [
# A script for quickly running tests.
# See https://github.com/typst/typst/blob/main/tests/README.md#making-an-alias
(pkgs.writeShellScriptBin "testit" ''
cargo test --workspace --test tests -- "$@"
'')
];
};
};
};
}